Skip to main content

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.

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 sub class. Inheritance concept allows programmers to define 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 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 defined by specifying its relationship with the base class in addition to its own detains, i.e. members.
The general form of defining a derived class is:
class derived-class_name : visivility-mode base-class_name
{
. . . .  // members of the derived class
. . . .
};

Forms of Inheritance

C++ offers five types of Inheritance. They are:
  • Single Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Multilevel Inheritance
  • Hybrid Inheritance (also known as Virtual Inheritance)
inheritanc-cpp

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 figure, fig(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 figures, fig(b) is the structure of Multiple Inheritance.
Program for Multiple Inheritance:
Example:
#include <iostream>
using namespace std;

class stud {
protected:
int roll, m1, m2;

public:
void get()
{
cout
<< "Enter the Roll No.: "; cin >> roll;
cout
<< "Enter the two highest marks: "; cin >> m1 >> m2;
}
};
class extracurriculam {
protected:
int xm;

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:
multiple inheritance

Hierarchical Inheritance

In this type of inheritance, multiple derived classes get inherited from a single base class. In the above list of figures, fig(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()
{
member
::get();
cout
<< "Class: "; cin >> level;
}
void disp2()
{
member
::disp();
cout
<< "Level: " << level << endl;
}
};
class staff : public member {
float salary;

public:
void getdata()
{
member
::get();
cout
<< "Salary: Rs."; cin >> salary;
}
void disp3()
{
member
::disp();
cout
<< "Salary: Rs." << salary << endl;
}
};
int main()
{
member M
;
staff S
;
stud s
;
cout
<< "Student" << endl;
cout
<< "Enter data" << endl;
s
.getdata();
cout
<< endl
<< "Displaying data" << endl;
s
.disp();
cout
<< endl
<< "Staff Data" << endl;
cout
<< "Enter data" << endl;
S
.getdata();
cout
<< endl
<< "Displaying data" << endl;
S
.disp();
}
Output:
Hierarchical 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.";
}
};

int main()
{
derived2 D
;
//D.display3();
D
.display2();
D
.display1();
}
Output:
Multilevel Inheritance

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 figure, the fig(5) is the diagram for Hybrid inheritance.

Comments

For Programs Click Here

Popular posts from this blog

C++ Constructors and Destructors

C++ Constructors and Destructors Providing the initial value as described in the earlier chapters of C++ does not conform to the philosophy of C++. So C++ provides a special member function called the constructor which enables an object to initialize itself at the time of its creation. This is known as automatic initialization of objects. This concept of C++ also provides another member function called destructor which is used to destroy the objects when they are no longer required. In this chapter, you will learn about how constructors and destructors work, types of constructors and how they can be implemented within C++ program. What are constructors? The process of creating and deleting objects in C++ is vital task. Each time an instance of a class is created the constructor method is called. Constructors is a special member functions of class and it is used to initialize the objects of its class. It is treated as a special member function because its name is the same as the cla...

Python Tutorial

               Python Tutorials             Python Tutorial Python is a general purpose object-oriented programming language with high-level programming capabilities. This Python tutorial series will help you to get started in Python programming language from scratch. Prerequisites To learn Python Programming Language you haven’t required any previous programming knowledge, but the basic understanding of any other programming languages will help you to understanding the Python Programming Concepts quickly. Python Programming Example A quick look at the example of  Hello, World!  in Python programming, and detailed description is given in the  Python Basics  page. Example: #!/usr/bin/python print "Hello, World!" Output: Hello, World! The above example has been used to print  Hello, World!  text on the screen.

Java Method Overriding

Java Method Overriding Declaring a method in the subclass which already exists there in the parent class is known as method overriding. When a class is inheriting a method from a superclass of its own, then there is an option of overriding the method provided it is not declared as final. The advantage of using overriding is the ability to classify a behavior that’s specific to the child class and the child class can implement a parent class method based on its necessity. There are certain rules that a programmer should follow in order to implement overriding. These are: In Java, a method can only be written in the child class and not in same class. Argument list should be exactly the same as that of the overridden method of that class. Instance methods can also be overridden if they are inherited by the child class. A constructor cannot be overridden. Final – declared methods cannot be overridden. Any method that is static cannot be used to override. The return type must have to be the...