12th Computer Science BCA CBSE JAC

Inheritance in C++ with Example Program

Inheritance in C++ with Example Program
Written by AIPS

In this informative topic, we will see best concept of Inheritance in C++ with Example and how it is important for OOP(Object Oriented Programming) Program.Hope this post will be very helpful for all students of Computer Science 11th,12th , BCA ,MCA and other computer science aspirants.

Inheritance

  • It is the method or technique by which objects of one class get the properties of objects of another class.
  • It is the most important feature of OOP.
  • It provides code reusability i.e. Code written once further can be used, No need to write again.
  • Redundant code can be removed.
  • It is a way for creating new classes from the existing classes.
  • It enables to derive a new class which is known as Derived / Child / Descendent/ Sub class from the existing classes which is known as Base / Parent / Ancestor / Super class.
  • Derived class can access members of base class according to rule of inheritance but base class can’t access member of derived class.
  • Base Class:- A class which allows to access data members and member functions to derived class is called base class.
  • Derived class:- A class which access or inherits the properties of base class  is called derived class.
  • A derived class may also be  base class of other derived class.

Rules for inheritance:

Base class can be inherited in two ways or mode.

  1. Public
  2. Private / protected

Public:– when a base class is inherited in public mode then Public members of base class become public member of derived class and protected members of base class become protected member of derived class.

Private:- When a base class is inherited in private mode the public and protected members of base class become private members of derived class.

Syntax for inheritance

class Derived_Class : Mode Base class

{

…………………

………………..

};

Example

Class nips

{

public:

int a,b;

};

class jac : public nips

{

………….

………….//a, b are accessible here

};

Types of inheritance

There are different types of inheritance in C++.

  1. Single inheritance / single level inheritance
  2. Multiple inheritance
  3. Multilevel inheritance
  4. Hierarchical inheritance
  5. Hybrid inheritance
  6. Multi path inheritance

Single inheritance / single level inheritance

When a Derived class inherits only one base class , is known as single inheritance or single level inheritance.

About the author

AIPS

Leave a Comment