Updated March 21, 2023
Introduction to Single Inheritance in C++
Single inheritance is one type of inheritance in which the derived class inherits only one base class. It provides reusability by allowing the derived class to inherit the features of the base class using objects. A class whose properties are inherited for reusability is called parent class or superclass or base class. The class that inherits the properties from this base or superclass is called a child class or derived class or subclass. Let’s learn further about the concept of single inheritance in C++.
Syntax of Single Inheritance in C++:
All the properties and methods can be inherited from the base class to the derived class.
class base_class
{
//code
};
class derived_class : public(access_modifier) base_class
{
//code
};
int main()
{
base_class object_1;
derived_class object_2;
//code
}
Examples of Single Inheritance in C++
Let us find different examples of single inheritance of C++ below:
Example #1
Code:
#include <iostream>
using namespace std;
class First
{
public :void display()
{
cout<<"This display is inside the first class" << endl;
}
};
class Second: public First
{
public: void show()
{
cout<<"This show is inside the second class which is derived from parent class" << endl;
}
};
int main()
{
First f;
f.display();
//f.show();
Second s;
s.display();
s.show();
}
Output:
If we uncomment the f.show() method, we will get the below error.
Code Explanation: Now, let us see how the code is actually functioning. We have created two classes, namely First and Second. We have derived the second class from the first. We have one function under the base class and another function in the derived class. In the main function, we have declared our objects for both the parent and the child class. With the child class object, we tried to access both the base and derived class methods, which would be absolutely successful.
But if we try to access the derived class method or variables through the base class object as seen in the second output, we get the error. It is obvious because the base class object cannot access the derived class methods/ variables, but vice versa.
Example #2
Now, we can check the output by giving the same methods in both classes. The code is written below.
Code:
#include <iostream>
using namespace std;
class First
{
public :void display()
{
cout<<"This display is inside the first class" << endl;
}
};
class Second: public First
{
public: void display()
{
cout<<"This show is inside the second class which is derived from parent class" << endl;
}
};
int main()
{
First f;
f.display();
f.display();
Second s;
s.display();
s.display();
}
Output:
Code Explanation: It is the same code, except for the method name in the derived class is now the same as the method name in the base class. All the methods would give the same output. With the concept of overriding, the classes and their respective objects would find their own method name and display the contents of the same class.
How Does Single Inheritance Work in C++?
Let us see the working of single inheritance in C++ with the help of the examples below.
Example #1
Code:
#include <iostream>
using namespace std;
class Sum_and_mul
{
public:
int c=10;
public :
void sum_1(int a, int b)
{
int result;
result=a+c;
cout<<" The result for sum of a and c is: "<<result<<endl;
}
void mul_1(int a,int b)
{
int result;
result=a*c;
cout<<" The result for multiplication of a and c is: "<<result<<endl;
}
};
class Mul_and_sum : public Sum_and_mul
{
int d=20;
public:
void sum_2()
{
int result;
result=c+d;
cout<<" The result for sum of c and d is: "<<result<<endl;
}
void mul_2()
{
int result;
result=c*d;
cout<<" The result for multiplication of c and d is: "<<result<<endl;
}
};
int main()
{
int a,b;
cout<<" Enter value for a: ";
cin>>a;
cout<<" Enter value for b: ";
cin>>b;
Sum_and_mul sam;
Mul_and_sum mas;
sam.sum_1(a,b);
sam.mul_1(a,b);
mas.sum_1(a,b);
mas.mul_1(a,b);
mas.sum_2();
mas.mul_2();
}
Output:
Program Explanation: In the above example that is given,
- We had two classes, Sum_and_mul and Mul_and_sum, as a base and derived classes, respectively.
- There are two methods and a single variable pertaining to those two classes.
- We then declared those variables and methods for each class.
- We had inherited the properties of base class to derived class by using ‘colon (:)’
- Here, it can be observed that the derived class methods have the variables of the base class in performing a few mathematical operations.
- The vice versa in using the derived class variable in the base class is not possible. Give it a try if you want to check out the error.
- Then we had created our objects for each class.
- With the created object for the derived class, we had handled both base class and derived class methods, and we have got the output perfectly.
Example #2
Let us see how we can handle methods between the base and derived classes in the below example.
Code:
#include <iostream>
using namespace std;
class AB
{
int a = 10;
int b = 20;
public:
int sub()
{
int r = b-a;
return r;
}
};
class BA : public AB
{
public:
void show()
{
int s = sub();
cout <<"Subtraction of b and a is : "<<s<< endl;
}
};
int main()
{
BA b;
b.show();
return 0;
}
Output:
Program Explanation: In the above example, we had done the code calculations in the base class and used the derived class method. This is the simple, basic and proper example of the correct usage of single inheritance. As an exercise, try having parameterized methods and usage of variables between the base and derived classes.
Conclusion
So, in an above-discussed manner, we can have one class’s properties in the other classes. We have seen simple examples with respect to a basic understanding of the usage. Make sure that the access modifiers also play a vital role in performing the inheritance. Try the same using private, public and protected variables, methods, classes and check the output for a better understanding through different examples.
Recommended Articles
This is a guide to Single Inheritance in C++. Here we discuss the Introduction and Working of single inheritance in C++ along with examples and its code implementation. You may also look at the following articles to learn more –