The C++ class inheritance, accessing the base and derived class member functions/methods and the order constructor and destructor execution C++ code example

 

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: Accessing and displaying the base and derived/inherited class for C++ class inheritance programming

To show: How to access the base and derived/inherited class member variables and functions/methods in C++ programming

 

// C++ class inheritance base and derived class constructors and destructors

#include <iostream>

using namespace std;

 

// base and derived class declaration

// vehicle base class

class vehicle

{

protected:

int wheels;

double weight;

public:

vehicle(void)

{

wheels = 7; weight = 11111.0;

cout<<"It is me!, Constructor #1, own by base class"<<endl;

}

vehicle(int input_wheels, double input_weight)

{

wheels = input_wheels; weight = input_weight;

cout<<"It is me!, Constructor #2, own by base class"<<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 derived class

class car : public vehicle

{

int passenger_load;

public:

car(void)

{

passenger_load = 4; cout<<"It is me!, Constructor #3, derived class, car"<<endl;

}

car(int people, int input_wheels, double input_weight):vehicle(input_wheels, input_weight), passenger_load(people)

{

cout<<"It is me!, Constructor #4, derived class, car"<<endl;

}

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

int passengers(void) {return passenger_load;}

};

 

// truck derived class

class truck : public vehicle

{

int passenger_load;

double payload;

public:

truck(void)

{

passenger_load = 3;

payload = 22222.0;

}

 

truck(int people, double load, int input_wheels, double input_weight):vehicle(input_wheels, input_weight), passenger_load(people), payload(load)

{

cout<<"It is me!, Constructor #5, derived class, car"<<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(1, 12.5);

// unicycle.initialize(1, 12.5);

 

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

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

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

 

// constructor in the car class called to construct an object, after base class constructor called

car sedan_car(5, 4, 3500.0);

// constructor in the car class called to construct object 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 load is "<<sedan_car.wheel_load()<<" kg per tire."<<endl<<endl;

 

// constructor in the base class called to construct an object

truck trailer(1, 33675.0, 18, 12500.0);

// 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;

}

 

// base and derived class implementation

// initialize to any desired data

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

{

wheels = input_wheels;

weight = input_weight;

}

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:

 

It is me!, Constructor #2, own by base class

The unicycle has 1 wheel.

The unicycle's wheel load is 12.5 kg on the single tire.

The unicycle weighs 12.5 kg.

It is me!, Constructor #2, own by base class

It is me!, Constructor #4, derived class, car

The sedan car carries 5 passengers.

The sedan car weighs 3500 kg.

The sedan car's wheel load is 875 kg per tire.

It is me!, Constructor #2, own by base class

It is me!, Constructor #5, derived class, car

The trailer weighs 12500 kg.

The trailer's efficiency is 72.9291 %.

Press any key to continue . . .

 

 

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