Class 8  >  C++ Programming for Beginners  >  C++ Inheritance

C++ Inheritance - Notes | Study C++ Programming for Beginners - Class 8

1 Crore+ students have signed up on EduRev. Have you?

Inheritance

In C++, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories:

  • derived class (child) - the class that inherits from another class
  • base class (parent) - the class being inherited from

To inherit from a class, use the : symbol.
In the example below, the Car class (child) inherits the attributes and methods from the Vehicle class (parent):
Example

// Base class

class Vehicle {

  public:

    string brand = "Ford";

    void honk() {

      cout << "Tuut, tuut! \n" ;

    }

};


// Derived class

class Car: public Vehicle {

  public:

    string model = "Mustang";

};


int main() {

  Car myCar;

  myCar.honk();

  cout << myCar.brand + " " + myCar.model;

  return 0;

}

Why And When To Use "Inheritance"?
It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.

C++ Multilevel Inheritance

Multilevel Inheritance
A class can also be derived from one class, which is already derived from another class.
In the following example, MyGrandChild is derived from class MyChild (which is derived from MyClass).
Example

// Base class (parent)

class MyClass {

  public:

    void myFunction() {

      cout << "Some content in parent class." ;

    }

};


// Derived class (child)

class MyChild: public MyClass {

};


// Derived class (grandchild)

class MyGrandChild: public MyChild {

};


int main() {

  MyGrandChild myObj;

  myObj.myFunction();

  return 0;

}

Multiple Inheritance

A class can also be derived from more than one base class, using a comma-separated list:
Example

// Base class

class MyClass {

  public:

    void myFunction() {

      cout << "Some content in parent class." ;

    }

};


// Another base class

class MyOtherClass {

  public:

    void myOtherFunction() {

      cout << "Some content in another class." ;

    }

};


// Derived class

class MyChildClass: public MyClass, public MyOtherClass {

};


int main() {

  MyChildClass myObj;

  myObj.myFunction();

  myObj.myOtherFunction();

  return 0;

}

Access Specifiers

You learned from the Access Specifiers chapter that there are three specifiers available in C++. Until now, we have only used public (members of a class are accessible from outside the class) and private (members can only be accessed within the class). The third specifier, protected, is similar to private, but it can also be accessed in the inherited class:
Example

// Base class

class Employee {

  protected: // Protected access specifier

    int salary;

};


// Derived class

class Programmer: public Employee {

  public:

    int bonus;

    void setSalary(int s) {

      salary = s;

    }

    int getSalary() {

      return salary;

    }

};


int main() {

  Programmer myObj;

  myObj.setSalary(50000);

  myObj.bonus = 15000;

  cout << "Salary: " << myObj.getSalary() << "\n";

  cout << "Bonus: " << myObj.bonus << "\n";

  return 0;

}

The document C++ Inheritance - Notes | Study C++ Programming for Beginners - Class 8 is a part of the Class 8 Course C++ Programming for Beginners.
All you need of Class 8 at this link: Class 8
15 videos|20 docs|13 tests
Download as PDF

Download free EduRev App

Track your progress, build streaks, highlight & save important lessons and more!

Related Searches

Objective type Questions

,

MCQs

,

mock tests for examination

,

Semester Notes

,

Extra Questions

,

Sample Paper

,

Exam

,

ppt

,

Previous Year Questions with Solutions

,

Summary

,

study material

,

shortcuts and tricks

,

C++ Inheritance - Notes | Study C++ Programming for Beginners - Class 8

,

practice quizzes

,

Free

,

past year papers

,

video lectures

,

Viva Questions

,

C++ Inheritance - Notes | Study C++ Programming for Beginners - Class 8

,

C++ Inheritance - Notes | Study C++ Programming for Beginners - Class 8

,

Important questions

,

pdf

;