Inheritance in C++ (with example)

Inheritance in C++ (with example)

Inheritance in C++ is a process in which one object acquires all the properties and behaviors of parent object. The class which is inherited is called derived class’ or ‘child class’. And the class from which it is derived is called ‘base class’ or ‘parent class’. Inheritance is one of the most important feature of Object Oriented Programming (OOP).

In C++, the default mode of visibility is private. The private members of the base class are never inherited. So the members which need to be inherited are declared as public. The members declared as protected will be accessible to all the member functions inside that class and the class immediately derived from it.

Syntax of derived class:-

class derived_class_name : access_specifier base_class_name
{
      // body of derived class
}

Types of Inheritance in C++:

1. Single inheritance – Deriving 1 class from 1 base class.
2. Multiple inheritance – Deriving 1 class from 2 or more classes.
3. Multilevel inheritance – Deriving a class from another derived class.
4. Hierarchical inheritance – Deriving 2 or more classes from 1 base class.
5. Hybrid inheritance – Combination of 2 or more types of inheritance.

types of inheritance
Types of inheritance

1. E.g. Program showing use of Inheritance in C++

#include<iostream>
using namespace std;
class student {
  protected:
    int rno;
  public:
    void getroll() {
      cout << "\n Enter your roll no: ";
      cin >> rno;
    }

  void putroll() {
    cout << "\n Roll no: " << rno;
  }
};
class test: public student {
  protected: int p, c, m;
  public: void gettest() {
    cout << "\n Enter Maths marks: ";
    cin >> m;
    cout << "\n Enter Physics marks: ";
    cin >> p;
    cout << "\n Enter Chemistry marks: ";
    cin >> c;
  }
  void puttest() {
    cout << "\n Marks in Maths= " << m;
    cout << "\n Marks in Physics= " << p;
    cout << "\n Marks in Chemistry= " << c;
  }
};
class sports {
  protected:
    int marks;
  public:
    void getsp() {
      cout << "\n Enter sports marks: ";
      cin >> marks;
    }
  void putsp() {
    cout << "\n Marks in sports= " << marks;
  }
};
class result: public test, public sports {
  protected: 
       float avg,  total;
  public: 
       void putresult();
};
void result::putresult() {
  total = (p + c + m + marks);
  avg = total / 4.0;
  putroll();
  cout << "\n Result=>";
  puttest();
  putsp();
  cout << "\n Total marks= " << total;
  cout << "\n Average marks= " << avg;
}
int main() {
  result r;
  r.getroll();
  r.gettest();
  r.getsp();
  r.putresult();
  return 0;
}

2. Output-

Inheritance in C++ output

3. Explanation-

In this program we are calculating the result of a student. It will accept values for student’s roll number and marks in three subjects and sports. Here we first create a class student which is a base class. This will simply store the roll number of student. Then we derive class test from student class. Since test is derived from only one base class this is called as single inheritance. In this class we accept marks of student in three subjects. And display the same.

Then we define a new class sports to accept marks in sports. This class will accept and display marks obtained in sports by a student. Then we create a final class result which will calculate total marks and average marks obtained by student. This class is derived from two classes namely test and sports. This type of inheritance is called as multiple inheritance.

At last main function, in main function we are creating object of result class. This is because in inheritence we don’t need to create object of each class. The derived class object can access all the functions of base class. We derive class result from test and sports. And test is derived from student. Thus result class object can access each class functions.

4. Parts of program-

1. #include is a pre-processor directive. It is used to include header files.
2. iostream is header file which has certain commands that c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int, float are data types.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. cout is the keyword use to print a message.
7. cin is the keyword use to store values in variables.
8. return statement is use to return a value. If any error occurs the program will return 0.

Conclusion-

This was the program showing use of inheritance in C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

2 COMMENTS

comments user
Clorinda Koopman

Nice article!!!

Leave a Reply

Your email address will not be published. Required fields are marked *