Table of Contents
Single Level inheritance in C++ language
In this tutorial, we will discuss the OOP concept of single-level inheritance in C++ language.
Single level inheritance is easy to understand as it is the most simple process compared to all other inheritances. It is when a class extends only to another class (Single child is derived from a single parent) .
Single level inheritance only has one parent and one child class. Child class has its own property and it’s authorized to access the property of parent class. The diagram below shows that B extends only to one class which is A. Here, A is the parent class or base class.
So, when a single class is derived from a single parent class, it is called a single inheritance or simple inheritance.
Single level inheritance only has one parent and one child class. Child class has its own property and it’s authorized to access the property of parent class. The diagram below shows that B extends only to one class which is A. Here, A is the parent class or base class.
So, when a single class is derived from a single parent class, it is called a single inheritance or simple inheritance.
Syntex of inheritance in C++
class base_A{ datamembers.... methods ..... }; class derived_B: public base _A{ datamembers.... methods ..... };
Program 1
Find the sum of two numbers using single inheritance in C++
In the above program, class base has protected members and class derived has public members. The class derived inherits all the members of base class
Program 2
Find the cube of a value using single inheritance in C++
In the above program, class value and class cobe has protected and public memebers. The class cube inherits all the producted and public members of value class
Program 3
When the above code is executed, it produces the following result
Explanation for the above program
#include <iostream>
using namespace std;
class student //parant class
{// start of student class
public:
int index_no;
char name[20]; //assign the variable in class student
void getdata() // create a method for get data
{
cout << “Enter your index no:- t”<< endl;
cin>>index_no;
cout << “Enter your name:- t”<< endl;
cin>>name;
}
}; // end of student class
class marks:public student //class marks inherit properties from class students
{ //start of class marks
public:
int m1,m2,m3,m4,total;
float percentage;//assign variables in class marks
void getmarks() //create a method for get marks
{
getdata();
cout << “Enter your marks 1:- t”<< endl;
cin>>m1;
cout << “Enter your marks 2:- t”<< endl;
cin>>m2;
cout << “Enter your marks 3:- t”<< endl;
cin>>m3;
cout << “Enter your marks 4:- t”<< endl;
cin>>m4;
}
void display()
{
cout<<“index notnametmarks 1tmarks 2tmarks 3tmarks 4ttpercentage”<<endl;
cout<<index_no<<“tt”<<name<<“t”<< m1<<“t”<< m2<<“t”<<m3<<“t”<<m4<<“t”<<“t”<<percentage;
}
}; //end of class marks
int main()
{
marks std; //create a object for class marks
std.getmarks(); //call method using object
std.display(); //call method using object
return 0;
}