Inheritance in C++ Programming
OOPs in C++
In this lesson, we will understand what is Inheritance in C++ and how to use it in class object programming.
What is Inheritance in C++?
In C++, when we create a new class by utilizing the code of an existing class, then this process is known as Inheritance.
In inheritance, we don't copy the code from an existing class to a new class. Instead, we include the existing class in a new class when we create the new class.
Let's take an example to clear the concept of inheritance. Suppose we have a class called Information with two instance variables: roll, name, and two instance methods as inputdata() and displaydata(). See the code given below.
Code for Information Class
#include <iostream>
#include <stdio.h>
using namespace std;
class Information
{
private:
int roll;
char name[30];
public:
void inputinfo()
{
cout<<"Enter Roll: ";
cin>>roll;
fflush(stdin); // clear the input buffer
cout<<"Enter Name: ";
gets(name);
}
void displayinfo()
{
cout<<"Roll: "<<roll<<endl;
cout<<"Name: "<<name<<endl;
}
};
We want to create a new class called Result that will store roll, name, marks of three subjects (English, Maths, Computer), total marks, and percentage obtained.
As the class Information already exists that can store roll and name, we will create our new class Result by inheriting all the codes from the Information class without copying and pasting the codes from the Information class into the Result class. See the complete example given below.
Example of Inheritance
#include <iostream>
#include <stdio.h>
using namespace std;
class Information
{
private:
int roll;
char name[30];
public:
void inputinfo()
{
cout<<"Enter Roll: ";
cin>>roll;
fflush(stdin); // clear the input buffer
cout<<"Enter Name: ";
gets(name);
}
void displayinfo()
{
cout<<"Roll: "<<roll<<endl;
cout<<"Name: "<<name<<endl;
}
};
// Inherit the class Information into Result
class Result : public Information
{
private:
int eng;
int math;
int comp;
int total;
float per;
public:
void inputdata()
{
inputinfo(); // calling the inputinfo() method of the parent class Information
cout<<"Enter English: ";
cin>>eng;
cout<<"Enter Math: ";
cin>>math;
cout<<"Enter Computer: ";
cin>>comp;
total=eng+math+comp;
per=(total/300.0)*100;
}
void displaydata()
{
displayinfo(); // calling the displayinfo() method of the parent class Information
cout<<"English: "<<eng<<endl;
cout<<"Math: "<<math<<endl;
cout<<"Computer: "<<comp<<endl;
cout<<"Total: "<<total<<endl;
cout<<"Percentage: "<<per<<endl;
}
};
int main()
{
Result x;
x.inputdata();
x.displaydata();
return 0;
}
Output
Enter Roll: 1 Enter Name: Peter Enter English: 75 Enter Math: 84 Enter Computer: 92 Roll: 1 Name: Peter English: 75 Math: 84 Computer: 92 Total: 251 Percentage: 83.6667
In the above program, We can see that we have created a new class Result by inheriting the class Information.
The class Result is created by inheriting the class Information. So we can call the Information class as Base class or Parent class and the Result class as Derived class or Subclass or Child class.
Initialize Base Class Constructor from Derived Class Constructor
The example below will show how to initialize the base class constructor from the derived class constructor.
Example
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class Information
{
private:
int roll;
char name[30];
public:
// Default Constructor
Information()
{
roll=0;
strcpy(name,"--");
}
// Parameterized Constructor
Information(int r, char n[])
{
roll=r;
strcpy(name,n);
}
void show()
{
cout<<"Roll: "<<roll<<endl;
cout<<"Name: "<<name<<endl;
}
};
// Inherit the class Information into Result
class Result : public Information
{
private:
int eng;
int math;
public:
// Default Constructor
Result()
{
eng=0;
math=0;
}
// Parameterized Constructor
Result(int r,char n[],int e,int m) : Information(r, n) // Initializing parameterized constructor of base class using colon :
{
eng=e;
math=m;
}
void display()
{
show();
cout<<"English: "<<eng<<endl;
cout<<"Math: "<<math<<endl;
}
};
int main()
{
Result x;
Result y(1,"William",87,91);
x.display();
y.display();
return 0;
}
Output
Roll: 0 Name: -- English: 0 Math: 0 Roll: 1 Name: William English: 87 Math: 91
In the above program, we created two Result class objects, x and y. While creating the object y, we initialized it by passing values to its parameterized constructor. The parameterized constructor of the Result class initialized the private variables of the Information class by passing the values to its parameterized constructor using a colon, as shown in the above example.
Access Base Class Method using Scope Resolution Operator
When there is a method in the derived class whose name is the same as the name of a method in its base class, then we will use the scope resolution operator to access the base class method from the derived class. See the example given below.
Example
#include <iostream>
#include <stdio.h>
using namespace std;
class Information
{
int roll;
char name[30];
public:
void inputinfo()
{
cout<<"Enter Roll: ";
cin>>roll;
fflush(stdin); // clear the input buffer
cout<<"Enter Name: ";
gets(name);
}
void displayinfo()
{
cout<<"Roll: "<<roll<<endl;
cout<<"Name: "<<name<<endl;
}
};
// Inherit the class Information into Result
class Result : public Information
{
int eng;
int math;
int comp;
int total;
float per;
public:
void inputinfo()
{
/*calling the inputinfo() method of the parent class
Information using the scope resolution operator*/
Information::inputinfo();
cout<<"Enter English: ";
cin>>eng;
cout<<"Enter Math: ";
cin>>math;
cout<<"Enter Computer: ";
cin>>comp;
total=eng+math+comp;
per=(total/300.0)*100;
}
void displayinfo()
{
/*calling the displayinfo() method of the parent class
Information using the scope resolution operator*/
Information::displayinfo();
cout<<"English: "<<eng<<endl;
cout<<"Math: "<<math<<endl;
cout<<"Computer: "<<comp<<endl;
cout<<"Total: "<<total<<endl;
cout<<"Percentage: "<<per<<endl;
}
};
int main()
{
Result x;
x.inputinfo();
x.displayinfo();
return 0;
}
Output
Enter Roll: 1 Enter Name: Thomas Enter English: 87 Enter Math: 59 Enter Computer: 67 Roll: 1 Name: Thomas English: 87 Math: 59 Computer: 67 Total: 213 Percentage: 71