By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,146 Members | 1,481 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,146 IT Pros & Developers. It's quick & easy.

Ouery

P: n/a
i
What is polymorphism?

Feb 23 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Feb 23, 9:28 am, "i" <imhskala...@yahoo.co.inwrote:
What is polymorphism?
http://www.google.com/search?lr=&ie=...q=polymorphism

--
Erik Wikström

Feb 23 '07 #2

P: n/a
On Feb 23, 1:28 pm, "i" <imhskala...@yahoo.co.inwrote:
What is polymorphism?
Maybe you could search for this question as is on google and am quite
sure you'll get the answer!

Polymorphism literally means: taking many forms

consider this example: (a rough outline, ignore specifics of C++)

Class Account is the parent of Class SavingsAccount and Class
CurrentAccount:

You can do this:

Account acc1=new SavingsAccount(); }...(X)
Account acc2=new CurrentAccount(); }

you have basically "upcasted" the objects of SavingsAccount and
CurrentAccount to that of type Account. When you call a function say
acc1.updateSavingsBalance(); which is in the class SavingsAccount,
you'll need to downcast to the appropriate subclass.

(SavingsAccount)acc1.updateSavingsBalance();

You may wonder why do I want to do such a thing as in (X)...lets say
you have a function adds interest to an account irrespective of the
account type:

addInterest(Account account)
{
//add interest
}

you just need to pass the base class reference and use polymorphism as
described above to add interest to the subclass. It's better
programming practice rather than creating an overloaded function for
each of the subclasses!!

Reading any text on C++ (or any OOP language for that matter) will
help you understand polymorphism.

Regards,

Nupul

Feb 23 '07 #3

P: n/a
On Feb 23, 11:03 am, "NUPUL" <nupul.kukr...@gmail.comwrote:
consider this example: (a rough outline, ignore specifics of C++)
We sure will. Especially since your code looks suspiciously like Java.
>
Class Account is the parent of Class SavingsAccount and Class
CurrentAccount:

You can do this:

Account acc1=new SavingsAccount(); }...(X)
Account acc2=new CurrentAccount(); }
/Peter

Feb 23 '07 #4

P: n/a
i
On Feb 23, 3:03 pm, "NUPUL" <nupul.kukr...@gmail.comwrote:
On Feb 23, 1:28 pm, "i" <imhskala...@yahoo.co.inwrote:
What is polymorphism?

Maybe you could search for this question as is on google and am quite
sure you'll get the answer!

Polymorphism literally means: taking many forms

consider this example: (a rough outline, ignore specifics of C++)

Class Account is the parent of Class SavingsAccount and Class
CurrentAccount:

You can do this:

Account acc1=new SavingsAccount(); }...(X)
Account acc2=new CurrentAccount(); }

you have basically "upcasted" the objects of SavingsAccount and
CurrentAccount to that of type Account. When you call a function say
acc1.updateSavingsBalance(); which is in the class SavingsAccount,
you'll need to downcast to the appropriate subclass.

(SavingsAccount)acc1.updateSavingsBalance();

You may wonder why do I want to do such a thing as in (X)...lets say
you have a function adds interest to an account irrespective of the
account type:

addInterest(Account account)
{
//add interest

}

you just need to pass the base class reference and use polymorphism as
described above to add interest to the subclass. It's better
programming practice rather than creating an overloaded function for
each of the subclasses!!

Reading any text on C++ (or any OOP language for that matter) will
help you understand polymorphism.

Regards,

Nupul
Can we create child class for the parent class? in c++. It is
possible only in Java, isn't it? Could you explain polymorphism
with a simple example please?
Feb 23 '07 #5

P: n/a
On Feb 23, 8:31 am, "i" <imhskala...@yahoo.co.inwrote:
On Feb 23, 3:03 pm, "NUPUL" <nupul.kukr...@gmail.comwrote:


On Feb 23, 1:28 pm, "i" <imhskala...@yahoo.co.inwrote:
What is polymorphism?
Maybe you could search for this question as is on google and am quite
sure you'll get the answer!
Polymorphism literally means: taking many forms
consider this example: (a rough outline, ignore specifics of C++)
Class Account is the parent of Class SavingsAccount and Class
CurrentAccount:
You can do this:
Account acc1=new SavingsAccount(); }...(X)
Account acc2=new CurrentAccount(); }
you have basically "upcasted" the objects of SavingsAccount and
CurrentAccount to that of type Account. When you call a function say
acc1.updateSavingsBalance(); which is in the class SavingsAccount,
you'll need to downcast to the appropriate subclass.
(SavingsAccount)acc1.updateSavingsBalance();
You may wonder why do I want to do such a thing as in (X)...lets say
you have a function adds interest to an account irrespective of the
account type:
addInterest(Account account)
{
//add interest
}
you just need to pass the base class reference and use polymorphism as
described above to add interest to the subclass. It's better
programming practice rather than creating an overloaded function for
each of the subclasses!!
Reading any text on C++ (or any OOP language for that matter) will
help you understand polymorphism.
Regards,
Nupul

Can we create child class for the parent class? in c++. It is
possible only in Java, isn't it? Could you explain polymorphism
with a simple example please?- Hide quoted text -

- Show quoted text -
Dear 'i',

Please, atleast make an attempt to search for answers, before asking
for help.
I am sure if you google your questions, you will find what you are
looking for.

Regards,
Manish

Feb 23 '07 #6

P: n/a
>
Can we create child class for the parent class? in c++. It is
possible only in Java, isn't it? Could you explain polymorphism
with a simple example please?
Gosh, how long have you been learning C++, and where from?

Of course it is possible in C++, in fact C++ has more possibilities than
Java because it has better support for multiple inheritance.

john

Feb 23 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.