Can anyone help me understand function polymorphism ?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 40
Reputation: sham is an unknown quantity at this point 
Solved Threads: 0
sham sham is offline Offline
Light Poster

Can anyone help me understand function polymorphism ?

 
0
  #1
Apr 15th, 2005
Can anyone help me understand function polymorphism ?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Can anyone help me understand function polymorphism ?

 
0
  #2
Apr 15th, 2005
It's pretty simple, really, kinda sorta. You have a base class with a virtual function, and a derived class that redefines the virtual function. By creating a reference to an object of the base class and initializing it with an object of the derived class, the virtual function will call the derived class version instead of the base class version even though you're calling on behalf of a reference to the base class:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Base {
  6. public:
  7. virtual void func() { cout<<"Base class"<<endl; }
  8. };
  9.  
  10. class Derived: public Base {
  11. public:
  12. void func() { cout<<"Derived class"<<endl; }
  13. };
  14.  
  15. int main()
  16. {
  17. Base *a = new Derived;
  18.  
  19. a->func();
  20. }
The reason this works is because the function is declared as virtual (ie. polymorhpic). The compiler "knows" to use the actual type of the object rather than the declared type. The declared (static) type of the a in the example is a pointer to Base, but the actual (dynamic) type of a is a pointer to Derived. Because f is virtual, the compiler makes the connection and calls Derived::func instead of Base::func. If you remove virtual then the compiler doesn't make the connection and calls Base::func:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Base {
  6. public:
  7. void func() { cout<<"Base class"<<endl; }
  8. };
  9.  
  10. class Derived: public Base {
  11. public:
  12. void func() { cout<<"Derived class"<<endl; }
  13. };
  14.  
  15. int main()
  16. {
  17. Base *a = new Derived;
  18.  
  19. a->func();
  20. }
The code is identical except "virtual" has been removed.

That's all a there is to polymorphism. It's simply a fancy way of saying that instead of using functions of the declared type, the compiler is smart enough to use functions of the actual type when you tell it to.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 77
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

Re: Can anyone help me understand function polymorphism ?

 
0
  #3
Sep 19th, 2008
hi all,
i have a doubt...please clarify
Instead of having a base class pointer and pointing it to derived class...can we not have a derived class pointer itself to call the virtual function..how are the two cases different and what is the benefit of one over the other?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Can anyone help me understand function polymorphism ?

 
0
  #4
Sep 19th, 2008
What if you want to hold the pointers in a container i.e. std::vector and call the virtual functions for all elements pointed to by each container member?
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 77
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

Re: Can anyone help me understand function polymorphism ?

 
0
  #5
Sep 19th, 2008
sorry, i don't have much idea about vectors and containers.. can you please elaborate on what you are trying to tell
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Can anyone help me understand function polymorphism ?

 
1
  #6
Sep 19th, 2008
Suppose you have (for explanation unnecessary features stripped!):

  1. class Animal
  2. {
  3. virtual void output() = 0;
  4. };
  5.  
  6. class Dog : public Animal
  7. {
  8. virtual void output() {bark();}
  9. }
  10.  
  11. class Bird : public Animal
  12. {
  13. virtual void output() {tweet();}
  14. }

A container can only hold objects of same type, so you have to use
  1. std::vector<Animal *> animals;
You can't use
  1. std::vector<Animal> animals;
because polymorphism needs pointers or references to work

Now you can do something like:

  1. animals.push_back(new Dog);
  2. animals.push_back(new Bird);
  3. std::vector<Animal *>::iterator it = animals.begin();
  4. for (; it != animals.end(); ++it)
  5. {
  6. (*it)->output();
  7. }
Last edited by jencas; Sep 19th, 2008 at 10:22 am.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 18
Reputation: digitaldesperad is an unknown quantity at this point 
Solved Threads: 1
digitaldesperad digitaldesperad is offline Offline
Newbie Poster

Re: Can anyone help me understand function polymorphism ?

 
0
  #7
Sep 21st, 2008
Function overloading is also called function polymorphism. Poly means 'any' and morph means 'form': a polymorphic function is many-formed.

Function polymorphism refers to the capability to 'overload' a function with more than one meaning. By changing the number or type of parameters, you can give two or more functions the same function name, and the right one will be called by matching the parameters used. This enables you to create a function that can average integers, doubles and other values without having to create individual names for each function.

Suppose you write a function the doubles whatever input you give it. You would like to be able to pass in an int, a long, a float or a double. Without function overloading, you would have to create four function names:
  1. int DoubleInt(int);
  2.  
  3. long DoubleLong(long);
  4.  
  5. float DoubleFloat(float);
  6.  
  7. double DoubleDouble(double);
With function overloading, you make this declaration:
  1. int Double(int);
  2.  
  3. long Double(long);
  4.  
  5. float Double(float);
  6.  
  7. double Double(double);
This is easier to read and to use. You do not have to worry about which one to call, you just pass in a variable and the right function is called automatically.

The following code demonstrates the use of function overloading:
  1. // example of function polymorphism
  2. #include <iostream.h>
  3.  
  4. int Double(int);
  5. long Double(long);
  6. float Double(float);
  7. double Double(double);
  8.  
  9. int main()
  10. {
  11. int myInt = 6500;
  12. long myLong = 65000;
  13. float myFloat = 6.5F;
  14. double myDouble = 6.5e20;
  15.  
  16. int doubledInt;
  17. long doubledLong;
  18. float doubledFloat;
  19. double doubledDouble;
  20.  
  21. cout << "myInt = " << myInt << endl;
  22. cout << "myLong = " << myLong << endl;
  23. cout << "myFloat = " << myFloat << endl;
  24. cout << "myDouble = " << myDouble << endl;
  25.  
  26. doubledInt = Double(myInt);
  27. doubledLong = Double(myLong);
  28. doubledFloat = Double(myFloat);
  29. doubledDouble = Double(myDouble);
  30.  
  31. cout << "doubledInt = " << doubledInt << endl;
  32. cout << "doubledLong = " << doubledLong << endl;
  33. cout << "doubledFloat = " << doubledFloat << endl;
  34. cout << "doubledDouble = " << doubledDouble << endl;
  35.  
  36.  
  37. return 0;
  38. }
  39.  
  40. int Double (int original)
  41. {
  42. return 2 * original;
  43. }
  44.  
  45. long Double (long original)
  46. {
  47. return 2 * original;
  48. }
  49.  
  50. float Double (float original)
  51. {
  52. return 2 * original;
  53. }
  54.  
  55. double Double (double original)
  56. {
  57. return 2 * original;
  58. }
Last edited by Narue; Sep 21st, 2008 at 10:10 am. Reason: Added code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC