The C++ class inheritance: A simple C++ project development process
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 creating simple C++ object oriented project development process
To show: How to implement the C++ object oriented class inheritance in C++ programming properly
Create a new Win32 console mode application project named vehicleobject. Add the following header file to your project, named vehicle.h. No need to be compiled.
// the class declaration part, vehicle.h, header file
// add vehicle.h header file to your project, paste the code and save it, do not compile or run
// preprocessor directive to avoid the
// similar header file re-inclusion
// class declaration
// a protected class member can only be accessed or inherited by Cvehicle child or derived class
void initialize(int input_wheels, float input_weight);
float wheel_load (void);
Next add another source file for the class implementation part named vehicle.cpp. Compile it.
// program vehicle.cpp, implementation part, compile without error, generating object file, do not run
// class implementation
// initialize to the initial data
void Cvehicle::initialize(int input_wheels, float input_weight)
wheels = input_wheels;
weight = input_weight;
// get the number of wheels of this vehicle
// return the weight of this vehicle
// return the load on each wheel
Finally add and run the main() program, myvehicle.cpp
using namespace std;
// user define header file and put it in the same folder or project
// 4 objects instantiated of type Cvehicle class
Cvehicle car, motorcycle, truck, sedan_car;
// data initialization
// display the data
cout<<"The car has "<<car.get_wheels()<< " tyres."<<endl;
cout<<"Truck has load "<<truck.wheel_load()<<" kg per tyre."<<endl;
cout<<"Motorcycle weight is "<<motorcycle.get_weight()<<" kg."<<endl;
cout<<"Weight of sedan car is "<<sedan_car.get_weight()<<" kg, and has "<<sedan_car.get_wheels()<<" tyres."<<endl;
The car has 4 tyres.
Truck has load 1500 kg per tyre.
Motorcycle weight is 900 kg.
Weight of sedan car is 3000 kg, and has 4 tyres.
Press any key to continue . . .