Single inheritance in C++ OOP, Syntax of single inheritance, Example of singe inheritance 

Single inheritance in C++ OOP, Syntax of single inheritance, Example of single inheritance 

In this tutorial, we will learn about the followings;

  1. Single inheritance in C++ OOP
  2. The syntax of single inheritance in C++ OOP
  3. Example of single inheritance in C++ OOP

What is single inheritance?

In single inheritance child class is derived from only and only one parent class.

Syntax to inherit the class from the parent class in single inheritance?

Serial#Code
1class child_class_name : Access_specifier Parent_class_name
2{
3Body of the class;
4};

Example of singe inheritance

Serial#Code
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

#include<iostream>

using namespace std;

class sum

{

protected:

int n1;

int n2;

};

class child : public sum

{

public:

int sum()

{

cout<<“enter n1″<<endl;

cin>>n1;

cout<<“enter n2″<<endl;

cin>>n2;

cout<<“sum=”<<n1+n2<<endl;

}

};

int main()

{

child myobject;

myobject.sum();

}

 

Line 9:

Line 9 shows single inheritance where the child has only one parent as a sum. Variable declaration of n1 and n2 in line 6 and 7 is inherited from the parent class and used in child class”show”.

 

Test Your Understandings

1.In single inheritance multiple child can have multiple parent classes? YES / NO

Answer - Click Here:
NO

2.class child_class_name : Access_specifier parent_class_name is the correct syntax for creating child class? YES / NO

Answer - Click Here:
Yes
.