I just went to the tutorials but i still have a hard time understanding how to polymorph.
From what i understand so far, polymorphing is basically a function that accepts a class object in its parameters and in the body of the function, you can call any function from the object and display any member. Please correct me if i'm wrong
1 2 3 4 5 6 7 8
void polymorph (Rottweiler & object1) //Rottweiler is the name of a class
{
object1.displayData();
object1.displayMembers();
}
The code segment above is part of another program that I am doing. Is that how you polymorph?
ok so basically, the right time to use virtual functions is when you have 2 functions that have the same return type, name and parameter list in both derived and base classes, right?. And you use virtual functions so that the compiler knows which function to call?
One really handy thing is the ability to make functions take a reference or pointer to a base class as an parameter, then one sends it a reference to a derived class as an argument. The code does the right thing : it knows which derived function to call because it has a derived class reference, but the beauty is that it can greatly reduce how many functions one needs. By having a virtual or pure virtual function high up in the inheritance tree, one can greatly reduce the number of functions required lower down in the tree.
Here is an example about making pies, that I wrote awhile back: