The base class virtual function of the C++ polymorphism object oriented programming code sample

 

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 base class virtual function in C++ object oriented programming

To show: How the base class virtual function used in C++ object oriented polymorphism programming for function overridden

 

 

// C++ polymorphism and virtual function

#include <iostream>

using namespace std;

 

// base class declaration and implementation

class vehicle

{

int wheels;

float weight;

public:

// virtual keyword, base class message()

// a virtual function or method, if a function/method with the same signature declared in the inherited (derived) class, it will override the base class (this) version

virtual void message(void)

{

cout<<"Vehicle message(), from vehicle, the base class"<<endl;

}

};

 

// derived class declaration and implementation

class car : public vehicle

{

int passenger_load;

public:

// derived class, second message()

void message(void)

{

cout<<"Car message(), from car, the vehicle derived class"<<endl;;

}

};

 

// derived class, no message()

class truck : public vehicle

{

int passenger_load;

float payload;

public:

int passengers(void)

{

cout<<"No message()..."<<endl;

return passenger_load;

}

};

 

// derived class

class boat : public vehicle

{

int passenger_load;

public:

int passengers(void)

{

return passenger_load;

}

// derived class, third message()

void message(void)

{

cout<<"Boat message(), from boat, the vehicle derived class"<<endl;;

}

};

 

// the main program

int main(void)

{

// declare a pointer variable of type vehicle object

vehicle *unicycle;

 

cout<<"Using the virtual keyword for base class function."<<endl;

cout<<"Same derived class function signature as base class will have"<<endl;

cout<<"their own execution, others will use the base class..."<<endl;

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

// new is used to allocate memory for a vehicle class object, the object's constructor is called

// after the memory is allocated. Need to use the delete operator to deallocate the memory allocated with the new operator.

 

// will use the the base class message()

unicycle = new vehicle;

cout<<"vehicle object: ";

unicycle->message();

delete unicycle;

 

// will use the the car derived class message(), override the base class message()

unicycle = new car;

cout<<"car object: ";

unicycle->message();

delete unicycle;

unicycle = NULL;

 

// will use the the base class message()

unicycle = new truck;

cout<<"truck object: ";

unicycle->message();

delete unicycle;

unicycle = NULL;

 

// will use the the boat derived class message(), override the base class message()

unicycle = new boat;

cout<<"boat object: ";

unicycle->message();

delete unicycle;

unicycle = NULL;

 

cout<<endl<<"The real virtual function huh!"<<endl;

 

return 0;

}

 

Output example:

 

Using the virtual keyword for base class function.

Same derived class function signature as base class will have

their own execution, others will use the base class...

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

vehicle object: Vehicle message(), from vehicle, the base class

car object: Car message(), from car, the vehicle derived class

truck object: Vehicle message(), from vehicle, the base class

boat object: Boat message(), from boat, the vehicle derived class

The real virtual function huh!

Press any key to continue . . .

 

 

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