| | |
Can anyone help me understand function polymorphism ?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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:
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class Base { public: virtual void func() { cout<<"Base class"<<endl; } }; class Derived: public Base { public: void func() { cout<<"Derived class"<<endl; } }; int main() { Base *a = new Derived; a->func(); }
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; class Base { public: void func() { cout<<"Base class"<<endl; } }; class Derived: public Base { public: void func() { cout<<"Derived class"<<endl; } }; int main() { Base *a = new Derived; a->func(); }
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.
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
Suppose you have (for explanation unnecessary features stripped!):
A container can only hold objects of same type, so you have to use
You can't use
because polymorphism needs pointers or references to work
Now you can do something like:
C++ Syntax (Toggle Plain Text)
class Animal { virtual void output() = 0; }; class Dog : public Animal { virtual void output() {bark();} } class Bird : public Animal { virtual void output() {tweet();} }
A container can only hold objects of same type, so you have to use
C++ Syntax (Toggle Plain Text)
std::vector<Animal *> animals;
C++ Syntax (Toggle Plain Text)
std::vector<Animal> animals;
Now you can do something like:
C++ Syntax (Toggle Plain Text)
animals.push_back(new Dog); animals.push_back(new Bird); std::vector<Animal *>::iterator it = animals.begin(); for (; it != animals.end(); ++it) { (*it)->output(); }
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
Please use code tags - Please mark solved threads as solved
•
•
Join Date: Sep 2008
Posts: 18
Reputation:
Solved Threads: 1
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:
With function overloading, you make this declaration:
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:
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)
int DoubleInt(int); long DoubleLong(long); float DoubleFloat(float); double DoubleDouble(double);
C++ Syntax (Toggle Plain Text)
int Double(int); long Double(long); float Double(float); double Double(double);
The following code demonstrates the use of function overloading:
C++ Syntax (Toggle Plain Text)
// example of function polymorphism #include <iostream.h> int Double(int); long Double(long); float Double(float); double Double(double); int main() { int myInt = 6500; long myLong = 65000; float myFloat = 6.5F; double myDouble = 6.5e20; int doubledInt; long doubledLong; float doubledFloat; double doubledDouble; cout << "myInt = " << myInt << endl; cout << "myLong = " << myLong << endl; cout << "myFloat = " << myFloat << endl; cout << "myDouble = " << myDouble << endl; doubledInt = Double(myInt); doubledLong = Double(myLong); doubledFloat = Double(myFloat); doubledDouble = Double(myDouble); cout << "doubledInt = " << doubledInt << endl; cout << "doubledLong = " << doubledLong << endl; cout << "doubledFloat = " << doubledFloat << endl; cout << "doubledDouble = " << doubledDouble << endl; return 0; } int Double (int original) { return 2 * original; } long Double (long original) { return 2 * original; } float Double (float original) { return 2 * original; } double Double (double original) { return 2 * original; }
Last edited by Narue; Sep 21st, 2008 at 10:10 am. Reason: Added code tags
![]() |
Similar Threads
- Help me understand function in c++ (C++)
- Stuck writing a Function (C++)
- can anyone assiat in getting my function to work? (C)
- How would i recode as a function? (Java)
Other Threads in the C++ Forum
- Previous Thread: help me with my code please....
- Next Thread: emergency!!help me...
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






