The protected base class member variables and public inheritance C++ code sample


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 base class member variables and the public inheritance of the derived class in C++ programming

To show:How to use or access the protected base class member variables and the public inheritance of the derived/inherited class in C++ programming




// the C++ class public inheritance program example

#include <iostream>

using namespace std;


// class declaration

// vehicle base class

class vehicle


// with protected keyword, the variables will be automatically inherited by all the derived class but not outside the

// vehicle base and its derived class(es)


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



// derived class declaration part

// car derived class with public inheritance

class car :public vehicle


int passenger_load;


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;


void init_truck(int how_many = 4, double max_load = 24000.0);

double efficiency(void);

int passengers(void) {return passenger_load;}



// the main program

int main(void)


vehicle unicycle;

unicycle.initialize(1, 12.5);


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


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

cout<<"The unicycle's wheel loading 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<<"The sedan car carries "<<sedan_car.passengers()<<" passengers."<<endl;

cout<<"The sedan car's weight "<<sedan_car.get_weight()<<" kg."<<endl;

cout<<"The sedan's car 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<<"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, own by base class

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


wheels = input_wheels;

weight = input_weight;



// initialize() method own by derived car class

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


// class base variables used by derived car class, 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 the vehicle base class


The unicycle has 1 wheel.

The unicycle's wheel loading 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's car 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