Using the private base class member variables with public class inheritance C++ program 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: Removing the protected keyword of the base class member variable (become private) and using the public class inheritance for the derived class in C++ programming

To show: How to use the private base class member variable and the public class inheritance for the derived class in C++ programming

 

 

// using the C++ private base class member variables and public class inheritance

#include <iostream>

using namespace std;

 

// base and derived class declaration

// vehicle base class

class vehicle

{

// protected:

// note this is removed, so it is private now

int wheels;

double weight;

public: // public specifier

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 with public inheritance

class car : public vehicle

{

int passenger_load;

public:

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

int passengers(void) {return passenger_load;}

};

 

// truck derived class with public inheritance

class truck : public vehicle

{

int passenger_load;

double payload;

public:

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;

 

unicycle.initialize(1, 12.5);

 

cout<<"Using the vehicle base class"<<endl;

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

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

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

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

 

car sedan_car;

 

sedan_car.initialize(4, 3500.0, 5);

 

cout<<"Using car derived class with public inheritance"<<endl;

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

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;

trailer.initialize(18, 12500.0);

trailer.init_truck(1, 33675.0);

 

cout<<"Using truck derived class with public inheritance"<<endl;

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

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 part

// initialize to any desired data, this method own by base class

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

{

wheels = input_wheels;

weight = input_weight;

}

 

// this method own by derived class

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

{

passenger_load = people;

// this variable are invalid anymore because both wheels and weight are private now.

// wheels = input_wheels;

// weight = input_weight;

// an added statement, using base class method instead of derived class

vehicle::initialize(input_wheels, input_weight);

}

 

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

{

passenger_load = how_many;

payload = max_load;

}

double truck::efficiency(void)

{

// changed from the previous example, from weight() to get_weight()

return payload / (payload + get_weight());

}

 

Output example:

 

Using the vehicle base class

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

The unicycle has 1 wheel.

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

The unicycle's weight 12.5 kg.

Using car derived class with public inheritance

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

The sedan car carries 5 passengers.

The sedan car's weight 3500 kg.

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

Using truck derived class with public inheritance

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

The trailer's weight 12500 kg.

The trailer's efficiency is 72.9291 %.

Press any key to continue . . .

 

 

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