Categories: C++Learn C++

Single Level inheritance in C++ language

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.
Syntex of inheritance in C++
class base_A{
datamembers....
methods .....
};
class derived_B: public base _A{
datamembers....
methods .....
};
Diagram

Program 1
Find the sum of two numbers using single inheritance in C++
Example
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++
Example

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
Example

When the above code is executed, it produces the following result

Output
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;
}
Inheritance in Java                         Multiple Inheritance in C++
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.