Inheritance in C++

inheritance

Inheritance

On this  page we will discuss about inheritance in C++ .Inheritance is basically the ability for a  class to be able to derive its properties from a different class. It is the most important feature of object oriented programming . So we will discuss inheritance in detail along.

Inheritance in C++

Inheritance is a process in which one object acquires all the properties and behaviour of it’s parent object automatically . It is the most important feature of object oriented programming and allows –

  • High code re-use
  • Lesser time to code
  • Easier to maintain application and edit codes
  • Re-use pre-defined properties and data
Inheritance in c++

Syntax for Inheriting Declaration –

class NameOfDerivedClass : (Visibility mode) NameOfBaseClass
// Data Members and functions
}

In terms of Parent and Child nomenclature -
class NameOfChildClass : (Visibility mode) NameOfParentClass{
// Data Members and functions
}

Different Modes of Inheritance in C++

There are three different ways in which we can define relationship between base and derived class while defining inheritance –

  1. Public mode:
    1. Public members of base class become public in derived class
    2. Protected members of base class become Protected in derived class
    3. Private members are inaccessible in derived class
  2. Protected mode:
    1. Public members of base class become protected in derived class
    2. Protected members of base class become Protected in derived class
    3. Private members are inaccessible in derived class
  3. Private mode:
    1. Public members of base class become private in derived class
    2. Protected members of base class become private in derived class
    3. Private members are inaccessible in derived class
Different mode of inheritance

Example:

Run
#include <iostream>
using namespace std;

// base class
class Animal {

   public:
    void jump() {
        cout << "I can jump!" << endl;
    }

    void sit() {
        cout << "I can sit!" << endl;
    }
};

// derived class
class Dog : public Animal {
 
   public:
    void bark() {
        cout << "I can bark! Woof woof!!" << endl;
    }
};

int main() {
    // Create object of the Dog class
    Dog doggy;

    // Calling members of the base class
    doggy.jump();
    doggy.sit();

    // Calling member of the derived class
    doggy.bark();

    return 0;
}

Output:

I can jump!
I can sit!
I can bark! Woof woof!!

Benefits of Inheritance

  • This helps in reduce cost for projects
  • Saves time in coding
  • Decreases complexity of the program
  • Increases reliability

Types of inheritance in C++

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Multiple Inheritance
  4. Hierarchical Inheritance
  5. Hybrid or Multipath Inheritance

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription