So imagine that you have this class
Code:
class Base
{
int x;
public:
Base (int xx=0): x(xx){}
virtual int getNum() {return x;};
void PrintNum( cout<<x;);
};
class Derivated
{
int newX;
public:
Derivated (int xx=1, int y=5): Base(xx){newX=y;}
int getNum() {return newX;}
void PrintNum() {cout<<NewX;}
};
void main()
{
Base num(5);
Derivated num2(5,10);
Base *ptr;
int y=num.getNum(); //just call the getNum from the base
cout<<y; //y=5
num.PrintNum(); //print 5
int w=num.getNum(); //Now it will call the getNum from the derivated classe because this function is virtual
cout<<w; //w=10
//Now came the nice part
ptr=new Derivated(5,20);
int z=ptr.getNum(); //it will call the getNum from the derivated
cout<<z; //z=20
ptr.printNum(); //it will print 5
}
i hope that i didn't say any stupid thing
. And i hope that you understand a bit more from classes...