The private inheritance of the derived class with protected base class member variable 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: Using the protected keyword for base class member variable and the private inheritance in C++ object oriented programming

To show: How to use the protected member variable of the base class and the private inheritance in C++ object oriented programming




// re-inclusion of the protected keyword for member variables of the base class and private inheritance

#include <iostream>

using namespace std;


// base and derived class declaration

// vehicle base class

class vehicle



int wheels;

double weight;


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 class

class car :private vehicle


int passenger_load;


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

int passengers(void) {return passenger_load;}



// public keyword change to private - private inheritance

class truck :private vehicle


int passenger_load;

double payload;


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 vehicle base class public methods"<<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)"<<endl;

cout<<"and sedan_car.passengers()"<<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 tire."<<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<<"\nOthers are private..."<<endl;

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

// cout<<"The trailer's weights "<<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, 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 vehicle base class 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