The C++ class inheritance with an array of object instantiations 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: Printing some of the base class data and the derived/inherited class with an array of object in C++ programming

To show: How to instantiate an array of object by inherited/derived class in C++ programming

 

// C++ class inheritance code sample with an array of object

#include <iostream>

using namespace std;

 

// base and derived class declarations

// vehicle base class

class vehicle

{

protected:

int wheels;

double weight;

public:

vehicle(void)

{

wheels = 7; weight = 11111.0;

cout<<"Constructor #1, own by base class"<<endl;

}

vehicle(int input_wheels, double input_weight)

{

wheels = input_wheels; weight = input_weight;

cout<<"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<<"Constructor #3, derived class, car"<<endl<<endl;

}

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

{

cout<<"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)

{ }

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)

{

// instantiate the base class object

vehicle unicycle;

// used for counter

int index;

 

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;

 

// an array of object with 3 elements

car sedan_car[3];

// count and execute

for (index = 0 ; index < 3 ; index++)

{

sedan_car[index].initialize(4, 3500.0, 5);

cout<<"Count no. #" <<index<<endl;

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

cout<<"The sedan car weighs "<<sedan_car[index].get_weight()<<" kg."<<endl;

cout<<"The sedan car's wheel load is "<<sedan_car[index].wheel_load()<<" kg per tire."<<endl<<endl;

}

 

// instantiate the derived class object

truck *trailer;

 

// initialize to point to something, pointing to an object (allocate memory)

trailer = new truck;

if (trailer == NULL)

{

cout<<"Memory allocation failed!"<<endl;

exit(EXIT_FAILURE);

}

else

{

cout<<"Memory allocation for object is OK!"<<endl;

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 "<<trailer->efficiency()<<" %."<<endl;

 

//deallocate the object

delete trailer;

}

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:

 

Constructor #1, own by base class

The unicycle has 1 wheel.

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

The unicycle's weight 12.5 kg.

Constructor #1, own by base class

Constructor #3, derived class, car

Constructor #1, own by base class

Constructor #3, derived class, car

Constructor #1, own by base class

Constructor #3, derived class, car

Count no. #0

The sedan car carries 5 passengers.

The sedan car weighs 3500 kg.

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

Count no. #1

The sedan car carries 5 passengers.

The sedan car weighs 3500 kg.

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

Count no. #2

The sedan car carries 5 passengers.

The sedan car weighs 3500 kg.

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

Constructor #1, own by base class

Memory allocation for object is OK!

The trailer's weight 12500 kg.

The trailer's efficiency is 0.729291 %.

Press any key to continue . . .

 

 

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