Introduction to Inheritance in C++

Inheritance in C++ is one of the major aspects of Object Oriented Programming
(OOP). It is the process by which one object can inherit or acquire the features
of another object.


In C++ class and structure can use inheritance. It means we can make one class
(known as derived class) to acquire or inherit the features of another class
(known as base class).


Base class has general features common to all the derived classes and derived
class (apart from having all the features of the base class) adds specific features.
This enables us to form a hierarchy of classes.


Ex. if we define a class ‘computer’ then it could serve as the
base class for defining other classes such as ‘laptops’, ‘desktops’
etc.. This is because as you know that laptops have all the features of computers
and so have desktops (and so should their classes) but they have their specific
features too. So, rather than defining all the features of such classes from
scratch, we make them inherit general features from other classes.


General Form of Inheritance:



class
derived-class:access-specifier base-class
{
...
...
...
};


Here access-specifier can be any one of the three private, public and protected.
These will not be discussed here.


For this article we’ll be using public for all the classes we define.
This means, all the public members of the base class will be public to the derived
class too.


Now let’s take the example of ‘computer’ class a bit further
by actually defining it.



class computer
{
int speed;
int main_memory;
int harddisk_memory;

public:
void set_speed(int);
void set_mmemory(int);
void set_hmemory(int);
int get_speed();
int get_mmemory();
int get_hmemory();
};



As you can see, the features (properties and functions) defined in the class
computer is common to laptops, desktops etc. so we make their classes inherit
the base class ‘computer’.



class laptop:public computer
{
int battery_time;
float weight;

public:
void set_battime(int);
void set_weight(float);
int get_battime();
float get_weight();
};



This way the class laptop has all the features of the base class ‘computer’
and at the same time has its specific features (battery_time, weight) which
are specific to the ‘laptop’ class.


If we didn’t use inheritance we would have to define the laptop class
something like this:



class laptop
{
int speed;
int main_memory;
int harddisk_memory;

int battery_time;
float weight;

public:
void set_speed(int);
void set_mmemory(int);
void set_hmemory(int);
int get_speed();
int get_mmemory();
int get_hmemory();

void set_battime(int);
void set_weight(float);
int get_battime();
float get_weight();
};



And then again we would have to do the same thing for desktops and any other
class that would need to inherit from the base class ‘computer’.


Thanks to inheritance, we don’t need to do all this!


I think now you’ve got the essence of what inheritance is, so taking
the example of the ‘computer’ class a bit more further, we implement
it into an example program listed below:



// Introduction to Inheritance in C++
// ------------------------
// An example program to
// demonstrate inheritance in C++
#include<iostream.h>

// base class for inheritance
class computer
{
float speed;
int main_memory;
int harddisk_memory;

public:
void set_speed(float);
void set_mmemory(int);
void set_hmemory(int);
float get_speed();
int get_mmemory();
int get_hmemory();
};

// -- MEMBER FUNCTIONS --
void computer::set_speed(float sp)
{
speed=sp;
}

void computer::set_mmemory(int m)
{
main_memory=m;
}

void computer::set_hmemory(int h)
{
harddisk_memory=h;
}

int computer::get_hmemory()
{
return harddisk_memory;
}

int computer::get_mmemory()
{
return main_memory;
}

float computer::get_speed()
{
return speed;
}
// -- ENDS --

// inherited class
class laptop:public computer
{
int battery_time;
float weight;

public:
void set_battime(int);
void set_weight(float);
int get_battime();
float get_weight();
};

// -- MEMBER FUNCTIONS --
void laptop::set_battime(int b)
{
battery_time=b;
}

void laptop::set_weight(float w)
{
weight=w;
}

int laptop::get_battime()
{
return battery_time;
}

float laptop::get_weight()
{
return weight;
}
// -- ENDS --

void main(void)
{
computer c;
laptop l;

c.set_mmemory(512);
c.set_hmemory(1024);
c.set_speed(3.60);

// set common features
l.set_mmemory(256);
l.set_hmemory(512);
l.set_speed(1.8);

// set specific features
l.set_battime(7);
l.set_weight(2.6);

// show details of base class object
cout<<"Info. of computer class\n\n";
cout<<"Speed:"<<c.get_speed()<<"\n";
cout<<"Main Memory:"<<c.get_mmemory()<<"\n";
cout<<"Hard Disk Memory:"<<c.get_hmemory()<<"\n";

//show details of derived class object
cout<<"\n\nInfo. of laptop class\n\n";
cout<<"Speed:"<<l.get_speed()<<"\n";
cout<<"Main Memory:"<<l.get_mmemory()<<"\n";
cout<<"Hard Disk Memory:"<<l.get_hmemory()<<"\n";
cout<<"Weight:"<<l.get_weight()<<"\n";
cout<<"Battery Time:"<<l.get_battime()<<"\n";
}

Check out this stream