Programming & Scripting Tutorials

C++: Polymorphism





Polymorphism is quite difficult to understand, so we are going to get there in slow steps.

First we need to understand what polymorphism is.
Polymorphism in C++ is when a derived class's virtual function overrides that of the base class.
This sounds very complicated but its not that difficult.
To explain this read through this example:


#include <iostream>
using namespace std;

class Base{
public:
	void NotVirtual(){
	cout << "Base, Not Virtual" << endl;
	}
	virtual void Virtual(){
	cout << "Base, Virtual" << endl;
	}
};

class Derived : public Base{
public:
	void NotVirtual(){
	cout << "Derived, Not Virtual" << endl;
	}
	virtual void Virtual(){
	cout << "Derived, Virtual" << endl;
	}
};

int main (){
Base BaseObject;
Derived DerivedObject;

Base* BasePtr;
Derived* DerivedPtr;

BasePtr = &BaseObject;
cout << "Base pointer to Base object:" << endl;
BasePtr->NotVirtual();
BasePtr->Virtual();
cout << endl;

DerivedPtr = &DerivedObject;
cout << "Derived pointer to Derived object:" << endl;
DerivedPtr->NotVirtual();
DerivedPtr->Virtual();
cout << endl;

BasePtr = &DerivedObject;
cout << "Base pointer to derived object:" << endl;
BasePtr->NotVirtual();
BasePtr->Virtual();
cout << endl;
system("PAUSE");
return 0;
}

When the base pointer is set to the base object is outputs the base functions as expected.
When the derived pointer is set to the derived object it outputs the derived functions as expected.
However when the base pointer is set to the derived object, it outputs the base nonvirtual function, but outputs the derived virtual function.
This is because the derived virtual function overrides the base virtual function.

It isn't yet clear why overriding virtual functions can be helpful, but hopefully the following program can show you:

#include <iostream>

using namespace std;

class Shape{
public:
	virtual double Area(){
	return 0.0;
	}
};

class Circle : public Shape{
public:
	Circle(double InRadius) : Radius(InRadius) {}
virtual double Area(){
	return 3.14159*Radius*Radius;
	}
private:
	double Radius;

};

class Square : public Shape{
public:
	Square(double InSide) : Side(InSide) {}
virtual double Area(){
	return Side*Side;
	}
private:
	double Side;

};


int main(){

	Shape* Shapes[5];

	Circle CircleObjectOne(3);
	Circle CircleObjectTwo(15);

	Square SquareObjectOne(12);
	Square SquareObjectTwo(33);
	Square SquareObjectThree(3);


	Shapes[0] = &CircleObjectOne;
	Shapes[1] = &CircleObjectTwo;
	Shapes[2] = &SquareObjectOne;
	Shapes[3] = &SquareObjectTwo;
	Shapes[4] = &SquareObjectThree;

	for(int counter = 0; counter<4;counter++){
	cout << Shapes[counter]->Area() << endl;
	}
system("PAUSE");
return 0;
}

The base class returns 0, as you cant find the area of a generalised shape; Then the other shapes areas are found via the virtual functions.
One of the lines you might not understand is:

Shape* Shapes[5];


All this does is create five class pointers, in this case our class is Shape.

As I said at the beginning polymorphism isn't an easy thing to understand, some people get it immediately, some people need to re-read code a few times to get it, and some people it can take weeks to figure it out.
If you don't get it just read through the lesson again, and if you still don't get it after that post a question on the forums, they are here to help you.


This C++ tutorial was written by


Back to C++

Advertisement: