The C++ inheritance code sample with public inheritance demonstrating the default (base class) constructors and override constructors data of the base and derived/inherited classes

 

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: Printing some data of the inherited/derived classes that use the default base/inherited class constructors and the override base class constructors data in C++ inheritance programming

To show: How to use the default constructors data and override constructors data in derived/inherited and base classes in C++ public inheritance programming

 

// C++ inheritance, the public inheritance, the default and override constructors

// data of the base and derived/inherited classes

#include <iostream>

using namespace std;

 

// base and derived class declaration

// vehicle the base class

class vehicle

{

protected:

int wheels;

double weight;

public:

vehicle(void)

{

wheels = 7; weight = 11111.0;

cout<<"Constructor's value of the base class, vehicle"<<endl;

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

}

void initialize(int input_wheels, double input_weight);

int get_wheels(void) {return wheels;}

double get_weight(void) {return weight;}

double wheel_load(void) {return (weight/wheels);}

};

 

// car the derived/inherited class, public inheritance

class car : public vehicle

{

int passenger_load;

public:

car(void)

{

passenger_load = 4;

cout<<"Constructor's value of the derived class, car"<<endl;

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

}

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

int passengers(void) {return passenger_load;}

};

 

// truck the inherited class, public inheritance

class truck : public vehicle

{

int passenger_load;

double payload;

public:

truck(void)

{

passenger_load = 3;payload = 22222.0;

cout<<"Constructor's value of the derived class, truck"<<endl;

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

}

void init_truck(int how_many = 4, double max_load = 24000.0);

double efficiency(void);

int passengers(void) {return passenger_load;}

};

 

// main program

int main(void)

{

vehicle unicycle;

// use default constructor value, so no need the

// initialization code for object unicycle anymore.

// unicycle.initialize(1, 12.5);

 

cout<<"The unicycle has "<<unicycle.get_wheels()<<" wheel."<<endl;

cout<<"The unicycle's wheel loading is "<<unicycle.wheel_load()<<" kg on the single tyre."<<endl;

cout<<"The unicycle's weight "<<unicycle.get_weight()<<" kg."<<endl<<endl;

 

car sedan_car;

// use base class initialize() method

// sedan_car.initialize(4, 3500.0, 5);

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

cout<<"The sedan car's weight "<<sedan_car.get_weight()<<" kg."<<endl;

cout<<"The sedan car's wheel loading is "<<sedan_car.wheel_load()<<" kg per tyre."<<endl<<endl;

 

truck trailer;

// use base class initialize() method with default data

// trailer.initialize(18, 12500.0);

// trailer.init_truck(1, 33675.0);

cout<<"The trailer's weight "<<trailer.get_weight()<<" kg."<<endl;

cout<<"The trailer's efficiency is "<<100.0 * trailer.efficiency()<<" %."<<endl;

 

return 0;

}

 

// class implementation

// initialize to any desired data

// the base class method

void vehicle::initialize(int input_wheels, double input_weight)

{

wheels = input_wheels;

weight = input_weight;

}

 

// derived class method

void car::initialize(int input_wheels, double input_weight, int people)

{

passenger_load = people;

wheels = input_wheels;

weight = input_weight;

}

 

void truck::init_truck(int how_many, double max_load)

{

passenger_load = how_many;

payload = max_load;

}

double truck::efficiency(void)

{

return (payload / (payload + weight));

}

 

Output example:

 

Constructor's value of the base class, vehicle

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

The unicycle has 7 wheel.

The unicycle's wheel loading is 1587.29 kg on the single tyre.

The unicycle's weight 11111 kg.

Constructor's value of the base class, vehicle

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

Constructor's value of the derived class, car

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

The sedan car carries 4 passengers.

The sedan car's weight 11111 kg.

The sedan car's wheel loading is 1587.29 kg per tyre.

Constructor's value of the base class, vehicle

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

Constructor's value of the derived class, truck

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

The trailer's weight 11111 kg.

The trailer's efficiency is 66.6667 %.

Press any key to continue . . .

 

 

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