Using the virtual function in base class in order the derived (inherited) class function to override the similar base class function of the similar signature C++ polymorphism programming

 

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

To show: How to use the virtual function in the base class in order the inherited/derived class function can override it for C++ polymorphism object oriented programming

 

 

// C++ polymorphism, pointer variables, new keyword with based class virtual function

#include <iostream>

using namespace std;

// base class declaration and implementation

class vehicle

{

int wheels;

float weight;

public:

// with virtual keyword, any derived (inherited) class function with similar signature can override this base class function

virtual void message(void)

// first message()

{

cout<<"Vehicle message() from vehicle, the base class virtual function/method"<<endl;

}

};

// derived class declaration and implementation

class car : public vehicle

{

int passenger_load;

public:

// second message()

void message(void)

{

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

}

};

class truck : public vehicle

{

int passenger_load;

float payload;

public:

int passengers(void)

{

return passenger_load;

}

};

class boat : public vehicle

{

int passenger_load;

public:

int passengers(void)

{

return passenger_load;

}

// third message()

void message(void)

{

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

}

};

// the main program

int main(void)

{

// all pointer variables here

vehicle *unicycle;

car *sedan_car;

truck *trailer;

boat *sailboat;

cout<<"Using the virtual keyword for base class function with pointer variables,"<<endl;

cout<<"new keyword and overriding the derived (inherited) function/method"<<endl;

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

unicycle = new vehicle;

unicycle->message();

sedan_car = new car;

sedan_car->message();

trailer = new truck;

trailer->message();

sailboat = new boat;

sailboat->message();

unicycle = sedan_car;

unicycle->message();

return 0;

}

 

Output example:

 

Using the virtual keyword for base class function with pointer variables,

new keyword and overriding the derived (inherited) function/method

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

Vehicle message() from vehicle, the base class virtual function/method

Car message() from car, the vehicle derived class (overrode base class function)

Vehicle message() from vehicle, the base class virtual function/method

Boat message() from boat, the vehicle derived class (overrode base class function)

Car message() from car, the vehicle derived class (overrode base class function)

Press any key to continue . . .

 

 

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