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

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.