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
1 class child_class_name : Access_specifier Parent_class_name
2 {
3 Body 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:

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

Answer - Click Here:
.

Fazal Rehman Shamil
Welcome to all friends. The reason for our success is only your love for T4Tutorials. Our team is always available to answer your queries regarding any kind of confusions or discussion regarding your study and career matters. For discussion with us please join our facebook group "T4Tutorials.com". The link of the group is mentioned below. Thanks and love to all for connecting with us. We are nothing without you. Love you all.....
https://web.facebook.com/groups/2066136233601097/