943,900 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3161
  • C++ RSS
Apr 15th, 2005
0

Can anyone help me understand function polymorphism ?

Expand Post »
Can anyone help me understand function polymorphism ?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
sham is offline Offline
40 posts
since Mar 2005
Apr 15th, 2005
0

Re: Can anyone help me understand function polymorphism ?

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 19th, 2008
0

Re: Can anyone help me understand function polymorphism ?

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?
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006
Sep 19th, 2008
0

Re: Can anyone help me understand function polymorphism ?

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?
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Sep 19th, 2008
0

Re: Can anyone help me understand function polymorphism ?

sorry, i don't have much idea about vectors and containers.. can you please elaborate on what you are trying to tell
Reputation Points: 45
Solved Threads: 1
Junior Poster in Training
rati is offline Offline
78 posts
since Aug 2006
Sep 19th, 2008
1

Re: Can anyone help me understand function polymorphism ?

Suppose you have (for explanation unnecessary features stripped!):

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. std::vector<Animal *> animals;
You can't use
C++ Syntax (Toggle Plain Text)
  1. std::vector<Animal> animals;
because polymorphism needs pointers or references to work

Now you can do something like:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Sep 21st, 2008
0

Re: Can anyone help me understand function polymorphism ?

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
digitaldesperad is offline Offline
18 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help me with my code please....
Next Thread in C++ Forum Timeline: emergency!!help me...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC