The multiple classes which derived from the multiple base classes (multiple inheritance) C++ object oriented program development

 

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: Developing the C++ simple application that demonstrate the multiple derived/inherited classes from multiple base classes C++ program development

To show: How to use the multiple classes which derived from the base class with proper C++ program development

 

Create a new Win32 console mode application project named allvehicle. Add the following header file to your project, named vehicle.h. No need to be compiled.

 

// class declaration, vehicle.h, header file

// Add vehicle.h header file to your project, paste this code and save it, do not compile or run

//

//

// preprocessor directive to avoid the similar file re-inclusion

#ifndef VEHICLE_H

#define VEHICLE_H

 

// class declaration

class Cvehicle

{

// a protected class member can only be accessed or inherited by Cvehicle child or derived classes

protected:

int wheels;

float weight;

public:

void initialize(int input_wheels, float input_weight);

int get_wheels(void);

float get_weight(void);

float wheel_load (void);

};

#endif

 

Next add another source file for the class implementation part named vehicle.cpp. Compile it and make sure there is no error.

 

// program vehicle.cpp, implementation part, compile this file without error, generating object file, do not run

#include "vehicle.h"

 

// class implementation, initialize the input data

void Cvehicle::initialize(int input_wheels, float input_weight)

{

wheels = input_wheels;

weight = input_weight;

}

 

// get the number of wheels of this vehicle

int Cvehicle::get_wheels()

{

return wheels;

}

 

// return the weight of this vehicle

float Cvehicle::get_weight()

{

return weight;

}

 

// return the load on each wheel

float Cvehicle::wheel_load()

{

return (weight/wheels);

}

 

Add the following header file to your project, named car.h. No need to be compiled.

 

// another class declaration car.h

// save and include this file in your project

//

//

// To avoid similar header file re-inclusion

#ifndef CAR_H

#define CAR_H

#include "vehicle.h"

 

// derived class declaration

// Ccar class derived from Cvehicle class

class Ccar : public Cvehicle

{

int passenger_load;

public:

// this method will be used instead of the same method in Cvehicle class - overriding

void initialize(int input_wheels, float input_weight, int people = 4);

int passengers(void);

};

#endif

 

Next add another source file for the class implementation part named car.cpp. Compile it and make sure there is no error.

 

 

// add this car.cpp file to your project. This is an implementation for the car.h for derived Ccar class. Compile without error. Do not run

#include "car.h"

 

// implementation part of the derived class car.h, what the car class can do, have or provide

void Ccar::initialize(int input_wheels, float input_weight, int people)

{

passenger_load = people;

wheels = input_wheels;

weight = input_weight;

}

int Ccar::passengers(void)

{

return passenger_load;

}

 

Add the following header file to your project, named truck.h. No need to be compiled.

 

// aAnother class declaration, truck.h, save this file in your project. Do not compile or run

//

// to avoid the header file re-inclusion

#ifndef TRUCK_H

#define TRUCK_H

#include "vehicle.h"

 

// class declaration part of truck derived class

class Ctruck : public Cvehicle

{

int passenger_load;

float payload;

public:

void init_truck(int how_many = 2, float max_load = 24000.0);

float efficiency(void);

int passengers(void);

};

#endif

 

Next add another source file for the class implementation part named truck.cpp. Compile it and make sure there is no error

 

// add the implementation for the truck.h to your project, the derived class truck.cpp. Compile it but do not run

#include "truck.h"

 

// implementation of the Ctruck derived class

void Ctruck::init_truck(int how_many, float max_load)

{

passenger_load = how_many;

payload = max_load;

}

float Ctruck::efficiency(void)

{

return payload/(payload + weight);

}

int Ctruck::passengers(void)

{

return passenger_load;

}

 

Finally with three classes (one base class and another two are derived classes) , run the following main program.

 

// the new main program for inheritance, allvehicle.cpp

#include <iostream>

// the interface of the Cvehicle class

#include "vehicle.h"

// the interface of the Ccar class

#include "car.h"

// the interface of the Ctruck class

#include "truck.h"

using namespace std;

 

void main(void)

{

Cvehicle unicycle; // base class

 

cout<<"C++ class inheritance, multiple header files, re-inclusion,"<<endl;

cout<<" multiple implementation files code and program example"<<endl<<endl;

cout<<"Unicycle using the Cvehicle base class"<<endl;

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

 

unicycle.initialize(1,12.5);

 

cout<<"Unicycle has "<<unicycle.get_wheels()<<" tyre."<<endl;

cout<<"Unicycle wheel load is "<<unicycle.wheel_load()<<"kg on one tyre."<<endl;

cout<<"Unicycle weight is "<<unicycle.get_weight()<<" kg."<<endl<<endl;

 

Ccar sedan; // derived class

 

cout<<"Sedan car using the Ccar derived class"<<endl;

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

 

sedan.initialize(4,3500.0,5);

 

cout<<"Sedan car carries "<<sedan.passengers()<<" passengers."<<endl;

cout<<"Sedan car weight is "<<sedan.get_weight()<<" kg."<<endl;

cout<<"Sedan car wheel load is "<<sedan.wheel_load()<<"kg per tyre."<<endl<<endl;

 

Ctruck trailer; // derived class

 

cout<<"Trailer using the Ctruck derived class"<<endl;

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

 

trailer.initialize(18,12500.0);

trailer.init_truck(1,33675.0);

 

cout<<"Trailer weight is "<<trailer.get_weight()<<" kg."<<endl;

cout<<"Trailer efficiency is "<<100.00 * trailer.efficiency()<<"%."<<endl;

 

return;

}

 

Output example:

 

C++ class inheritance, multiple header files, re-inclusion,

multiple implementation files code and program example

 

Unicycle using the Cvehicle base class

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

Unicycle has 1 tyre.

Unicycle wheel load is 12.5kg on one tyre.

Unicycle weight is 12.5 kg.

 

Sedan car using the Ccar derived class

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

Sedan car carries 5 passengers.

Sedan car weight is 3500 kg.

Sedan car wheel load is 875kg per tyre.

 

Trailer using the Ctruck derived class

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

Trailer weight is 12500 kg.

Trailer efficiency is 72.9291%.

Press any key to continue . . .

 

 

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