0% found this document useful (0 votes)
16 views5 pages

C Inheritance

Uploaded by

Ali Qasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

C Inheritance

Uploaded by

Ali Qasim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4/22/2020 C++ Inheritance

C++ Inheritance

Reusability is one of the important characteristics of Object Oriented Programming (OOP). Instead of trying to write programs repeatedly,
using existing code is a good practice for the programmer to reduce development time and avoid mistakes. In C++, reusability is possible by
using Inheritance.

Table of Contents
1. What is Inheritance?
2. What are Base class and Derived class?
3. Forms of Inheritance
4. Single Inheritance
5. Multiple Inheritance
6. Hierarchical Inheritance
7. Program for Hierarchical Inheritance
8. Multilevel Inheritance
9. Program for Multilevel Inheritance
10. Hybrid Inheritance

What is Inheritance?
The technique of deriving a new class from an old one is called inheritance. The old class is referred to as base class and the new class is
referred to as derived class or subclass. Inheritance concept allows programmers to de ne a class in terms of another class, which makes
creating and maintaining application easier. When writing a new class, instead of writing new data member and member functions all over
again, programmers can make a bonding of the new class with the old one that the new class should inherit the members of the existing
class. A class can get derived from one or more classes, which means it can inherit data and functions from multiple base classes.

Here is the syntax how inheritance is performed in C++:

class derived-class: visibility-mode base-class

Visibility mode is used in the inheritance of C++ to show or relate how base classes are viewed with respect to derived class. When one class
gets inherited from another, visibility mode is used to inherit all the public and protected members of the base class. Private members never
get inherited and hence do not take part in visibility. By default, visibility mode remains "private".

What are Base class and Derived class?


The existing class from which the derived class gets inherited is known as the base class. It acts as a parent for its child class and all its
properties i.e. public and protected members get inherited to its derived class.

A derived class can be de ned by specifying its relationship with the base class in addition to its own details, i.e. members.

The general form of de ning a derived class is:

class derived-class_name : visivility-mode base-class_name


{
. . . . // members of the derived class
. . . .
};

Forms of Inheritance
C++ offers ve types of Inheritance. They are:

● Single Inheritance

● Multiple Inheritance

● Hierarchical Inheritance

● Multilevel Inheritance

● Hybrid Inheritance (also known as Virtual Inheritance)

https://www.w3schools.in/cplusplus-tutorial/inheritance/#What_is_Inheritance 1/5
4/22/2020 C++ Inheritance

Single Inheritance
In single inheritance, there is only one base class and one derived class. The Derived class gets inherited from its base class. This is the
simplest form of inheritance. In the above gure, g(a) is the diagram for single inheritance.

Multiple Inheritance
In this type of inheritance, a single derived class may inherit from two or more base classes. In the above list of gures, g(b) is the structure
of Multiple Inheritance.

Program for Multiple Inheritance:

Example:
public:
void getsm()
{
cout << "\nEnter the mark for Extra Curriculam Activities: "; cin >> xm;
}
};
class output : public stud, public extracurriculam {
int tot, avg;

public:
void display()

{
tot = (m1 + m2 + xm);
avg = tot / 3;
cout << "\n\n\tRoll No : " << roll << "\n\tTotal : " << tot;
cout << "\n\tAverage : " << avg;
}
};
int main()
{
output O;
O.get();
O.getsm();
O.display();
}

Output:

https://www.w3schools.in/cplusplus-tutorial/inheritance/#What_is_Inheritance 2/5
4/22/2020 C++ Inheritance

Hierarchical Inheritance
In this type of inheritance, multiple derived classes get inherited from a single base class. In the above list of gures, g(c) is the structure of
Hierarchical Inheritance.

Syntax:

class base_classname {
properties;
methods;
};
class derived_class1 : visibility_mode base_classname {
properties;
methods;
};
class derived_class2 : visibility_mode base_classname {
properties;
methods;
};
... ... ...
... ... ...
class derived_classN : visibility_mode base_classname {
properties;
methods;
};

Program for Hierarchical Inheritance


Example:

#include <iostream>
#include <string.h>
using namespace std;

class member {
char gender[10];
int age;

public:
void get()
{
cout << "Age: "; cin >> age;
cout << "Gender: "; cin >> gender;
}
void disp()
{
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
}
};
class stud : public member {
char level[20];

public:
void getdata()

Output:

https://www.w3schools.in/cplusplus-tutorial/inheritance/#What_is_Inheritance 3/5
4/22/2020 C++ Inheritance

Multilevel Inheritance
The classes can also be derived from the classes that are already derived. This type of inheritance is called multilevel inheritance.

Program for Multilevel Inheritance


Example:

#include <iostream>
using namespace std;

class base {
public:
void display1()
{
cout << "\nBase class content.";
}
};
class derived : public base {
public:
void display2()
{
cout << "1st derived class content.";
}
};

class derived2 : public derived {


void display3()
{
cout << "\n2nd Derived class content.";
}
};

Output:

Hybrid Inheritance
This is a Mixture of two or More Inheritance and in this Inheritance, a Code May Contains two or Three types of inheritance in Single Code. In
the above gure, the g(5) is the diagram for Hybrid inheritance.

Next Chapter : C++ Friend Function ❯

https://www.w3schools.in/cplusplus-tutorial/inheritance/#What_is_Inheritance 4/5
4/22/2020 C++ Inheritance

© w3schools.in - All Rights Reserved. About Us Contact Us Copyright Privacy Policy

https://www.w3schools.in/cplusplus-tutorial/inheritance/#What_is_Inheritance 5/5

You might also like