The polymorphism in C++ programming: the virtual function of the base class

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Displaying some data to demonstrate the use of the virtual function in the base class for C++ object oriented programming

To show: How to use of the virtual function in the base class for C++ object oriented programming

 

// polymorphic functions with the virtual keyword

#include <iostream>

using namespace std;

 

// class declaration and implementation part

// the base class

class Shape

{

// protected member variables should be available for derived classes

protected:

char* Color;

public:

// constructor, set the object's data

Shape()

{

cout<<"In base class Shape's constructor"<<endl;

Color = "No Color!";

}

~Shape()

{

cout<<"In base class Shape's destructor"<<endl;

};

// virtual base member function, return the object's data

virtual char* GetColor()

{

return Color;

}

};

 

// the derived class

class Rectangle:public Shape

{

// notice the same variable name, it is OK, it is local to this class

char* Color;

public:

Rectangle(){

cout<<"In derived class Rectangle's constructor"<<endl;

Color = "bLue SkY!";}

~Rectangle(){ cout<<"In derived class Rectangle's destructor"<<endl;}

// derived class member function, should also be virtual

char* GetColor()

{

return Color;

}

};

 

// the derived class

class Square:public Shape

{

char* Color;

public:

Square()

{

cout<<"In derived class Square's constructor"<<endl;

Color = "yEllOw!";}

~Square()

{

cout<<"In derived class Square's destructor"<<endl;

}

char* GetColor()

{

return Color;

}

};

 

// the derived class

class Triangle:public Shape

{

char* Color;

public:

Triangle()

{

cout<<"In derived class Triangle's constructor"<<endl;

Color = "GrEEn!";

}

~Triangle()

{

cout<<"In derived class Triangle's destructor"<<endl;

}

char* GetColor()

{

return Color;

}

};

 

// the derived class

class Circle:public Shape

{

char* Color;

public:

Circle()

{

cout<<"In derived class Circle's constructor"<<endl;

Color = "aMbEr!";

}

~Circle()

{

cout<<"In derived class Circle's destructor"<<endl;

}

// let set different function name but same functionality

char* GetMyColor()

{

return Color;

}

};

 

// the main program

int main(void)

{

// instantiate objects of the previously defined class types

Shape ObjOne;

Rectangle ObjTwo;

Square ObjThree;

Triangle ObjFour;

Circle ObjFive;

 

cout<<endl;

cout<<"Non-polymorphic, early binding:"<<endl;

cout<<"----------------------------------"<<endl;

cout<<"Shape color: "<<ObjOne.GetColor()<<endl;

cout<<"Rectangle color: "<<ObjTwo.GetColor()<<endl;

cout<<"Square color: "<<ObjThree.GetColor()<<endl;

cout<<"Triangle color: "<<ObjFour.GetColor()<<endl;

// notice the different function name as previous function

cout<<"Circle color: "<<ObjFive.GetMyColor()<<endl;

 

cout<<endl;

cout<<"\nPolymorphic, late binding:"<<endl;

cout<<"--------------------------"<<endl;

 

// pointer variable of type Shape base class

Shape *VirtualPtr;

// object allocation of type Shape size

VirtualPtr = new Shape;

cout<<"Shape color: "<<VirtualPtr->GetColor()<<endl;

cout<<" VirtualPtr pointer address = "<<VirtualPtr<<endl;

// de-allocate, clean up

delete VirtualPtr;

 

VirtualPtr = new Rectangle;

cout<<"Rectangle color: "<<VirtualPtr->GetColor()<<endl;

cout<<" VirtualPtr pointer address = "<<VirtualPtr<<endl;

delete VirtualPtr;

 

VirtualPtr = new Square;

cout<<"Square color: "<<VirtualPtr->GetColor()<<endl;

cout<<" VirtualPtr pointer address = "<<VirtualPtr<<endl;

delete VirtualPtr;

 

VirtualPtr = new Triangle;

cout<<"Triangle color: "<<VirtualPtr->GetColor()<<endl;

cout<<" VirtualPtr pointer address = "<<VirtualPtr<<endl;

delete VirtualPtr;

 

// no GetColor() in this derived class, so use the GetColor() from the base class

VirtualPtr = new Circle;

cout<<"Circle color: "<<VirtualPtr->GetColor()<<endl;

cout<<" VirtualPtr pointer address = "<<VirtualPtr<<endl<<endl;

delete VirtualPtr;

 

// retest

VirtualPtr = new Triangle;

cout<<"Triangle color: "<<VirtualPtr->GetColor()<<endl;

cout<<" VirtualPtr pointer address = "<<VirtualPtr<<endl;

delete VirtualPtr;

 

return 0;

}

 

Output example:

 

In base class Shape's constructor

In base class Shape's constructor

In derived class Rectangle's constructor

In base class Shape's constructor

In derived class Square's constructor

In base class Shape's constructor

In derived class Triangle's constructor

In base class Shape's constructor

In derived class Circle's constructor

 

Non-polymorphic, early binding:

----------------------------------

Shape color: No Color!

Rectangle color: bLue SkY!

Square color: yEllOw!

Triangle color: GrEEn!

Circle color: aMbEr!

 

Polymorphic, late binding:

--------------------------

In base class Shape's constructor

Shape color: No Color!

    VirtualPtr pointer address = 00346210

In base class Shape's destructor

In base class Shape's constructor

In derived class Rectangle's constructor

Rectangle color: bLue SkY!

    VirtualPtr pointer address = 00346210

In base class Shape's destructor

In base class Shape's constructor

In derived class Square's constructor

Square color: yEllOw!

    VirtualPtr pointer address = 00346210

In base class Shape's destructor

In base class Shape's constructor

In derived class Triangle's constructor

Triangle color: GrEEn!

VirtualPtr pointer address = 00346210

In base class Shape's destructor

In base class Shape's constructor

In derived class Circle's constructor

Circle color: No Color!

    VirtualPtr pointer address = 00346210

 

In base class Shape's destructor

In base class Shape's constructor

In derived class Triangle's constructor

Triangle color: GrEEn!

    VirtualPtr pointer address = 00346210

In base class Shape's destructor

In derived class Circle's destructor

In base class Shape's destructor

In derived class Triangle's destructor

In base class Shape's destructor

In derived class Square's destructor

In base class Shape's destructor

In derived class Rectangle's destructor

In base class Shape's destructor

In base class Shape's destructor

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index