The C++ private class inheritance of the derived/inherited 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: Displaying some data from the derived/inherited class which inherit privately from the base class in C++ programming

To show: What is the effect when the derived/inherited class inherit the base class privately in C++ programming

 

// C++ inheritance, protected, private and public keywords in class inheritance functionalities

#include <iostream>

using namespace std;

 

// base and derived class declaration

// vehicle base class

class vehicle

{

protected:

int wheels;

double weight;

public:

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);}

};

 

// public keyword changed to private - private inheritance

// car derived/inherited class

class car : private vehicle

{

int passenger_load;

public:

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

int passengers(void) {return passenger_load;}

};

 

// public keyword change to private - private inheritance

// truck derived/inherited class

class truck : private 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 base class, vehicle with public methods"<<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;

 

car sedan_car;

sedan_car.initialize(4, 3500.0, 5);

 

cout<<"\nThese two are public, sedan_car.initialize(4,3500.0,5) and sedan_car.passengers()"<<endl;

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

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

 

// methods get_weight() and wheel_load() not available because we use private inheritance

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

// this method is private now

trailer.init_truck(1, 33675.0);

 

cout<<"\nThese are public, trailer.init_truck(1, 33675.0),"<<endl;

cout<<"trailer.efficiency() and trailer.passengers()"<<endl;

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

cout<<"\nOthers are private..."<<endl;

 

// methods get_weight() and efficiency() not available because we use private inheritance

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

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

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

{

wheels = input_wheels;

weight = input_weight;

}

 

// method own by derived class

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

{ // wheels and weight still available because of the protected keyword

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:

 

Using base class, vehicle with public methods

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

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.

These two are public, sedan_car.initialize(4,3500.0,5) and sedan_car.passengers()

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

The sedan car carries 5 passengers.

These are public, trailer.init_truck(1, 33675.0),

trailer.efficiency() and trailer.passengers()

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

Others are private...

Press any key to continue . . .

 

 

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