C++ object oriented programming, the polymorphism without the virtual function 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 C++ object oriented programming, the polymorphism without the virtual function in C++ programming
To show: The C++ object oriented programming, the polymorphism without the virtual function
// C++ polymorphism without virtual function
#include
<iostream>using
namespace std;
// base class declaration and implementation
class
vehicle{
int wheels;
float weight;
public
:// first message(), no virtual modifier use here. Any derived (inherited) class function
// that have the same function/method signature cannot override this (base class) message() version 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
://second message()
void message(void){
cout<<"Car message(), from car, the vehicle derived class"<<endl;
}
};
class
truck : public vehicle{
int passenger_load;
float payload;
public
:int passengers(void)
{
return passenger_load;
}
// no message()
};
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"<<endl;
}
};
// the main program
int
main(){
cout<<"Without the virtual keyword for the base class function"<<endl;
cout<<"any derived (inherited) class function cannot override the base class function,"<<endl;
cout<<"all derived (inherited) class function with same signature as the base class"<<endl;
cout<<"will use the base class function version..."<<endl;
cout<<"---------------------------------------------------------------------------"<<endl;
vehicle *unicycle;
// allocate memory for vehicle class object
unicycle =new vehicle;
unicycle->message();
delete unicycle;
unicycle = NULL;
// will use the base class message(), cannot override
unicycle =new car;
unicycle->message();
delete unicycle;
unicycle = NULL;
// no message(), use the base class message()
unicycle =new truck;
unicycle->message();
delete unicycle;
unicycle = NULL;
// will use the base class message(), cannot override
unicycle =new boat;
unicycle->message();
delete unicycle;
unicycle = NULL;
// unicycle = sedan_car;
// unicycle->message(); return 0;}
Output example:
Without the virtual keyword for the base class function
any derived (inherited) class function cannot override the base class function,
all derived (inherited) class function with same signature as the base class
will use the base class function version...
---------------------------------------------------------------------------
Vehicle message(), from vehicle, the base class
Vehicle message(), from vehicle, the base class
Vehicle message(), from vehicle, the base class
Vehicle message(), from vehicle, the base class
Press any key to continue . . .
C and C++ Programming Resources |
C & C++ Code Example Index