What is inheritance in C++?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Inheritance in C++ is a key feature of Object-Oriented Programming (OOP) that allows a class (called the derived class or child class) to inherit properties and behaviors (methods and data members) from another class (called the base class or parent class). This concept enables code reuse, extensibility, and hierarchical relationships among classes.

Key Concepts of Inheritance:

  1. Base Class (Parent Class): The class from which properties and methods are inherited.
  2. Derived Class (Child Class): The class that inherits the properties and methods from the base class.

Syntax:

class Base { // Members and methods of the base class }; class Derived : public Base { // Inherits members and methods from Base class };

Types of Inheritance in C++:

  1. Single Inheritance: A derived class inherits from only one base class.

    class A { public: void display() { cout << "Class A"; } }; class B : public A { // Class B inherits from class A };
  2. Multiple Inheritance: A derived class inherits from more than one base class.

    class A { // Class A definition }; class B { // Class B definition }; class C : public A, public B { // Class C inherits from both A and B };
  3. Multilevel Inheritance: A derived class inherits from another derived class, creating a chain of inheritance.

    class A { // Class A definition }; class B : public A { // Class B inherits from A }; class C : public B { // Class C inherits from B };
  4. Hierarchical Inheritance: Multiple derived classes inherit from the same base class.

    class A { // Class A definition }; class B : public A { // Class B inherits from A }; class C : public A { // Class C also inherits from A };
  5. Hybrid Inheritance: A combination of two or more types of inheritance, typically using multiple inheritance along with other forms.

Example of Inheritance:

#include <iostream> using namespace std; class Animal { public: void eat() { cout << "Eating..." << endl; } }; class Dog : public Animal { // Inheritance public: void bark() { cout << "Barking..." << endl; } }; int main() { Dog myDog; myDog.eat(); // Inherited from Animal class myDog.bark(); // Defined in Dog class return 0; }

Access Specifiers in Inheritance:

The relationship between the base class and the derived class depends on the access specifier (public, protected, or private) used when inheriting:

  • Public Inheritance: Members of the base class retain their access control in the derived class (public members remain public, protected members remain protected).
  • Protected Inheritance: Public and protected members of the base class become protected in the derived class.
  • Private Inheritance: Public and protected members of the base class become private in the derived class.

Benefits of Inheritance:

  • Code Reusability: Inherited classes can reuse the code from the base class, reducing redundancy.
  • Extensibility: New functionality can be added in derived classes without modifying the existing base class.
  • Modularity: Allows the program to be organized in a hierarchical manner, making it easier to maintain.

Sources:

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How can I practice coding effectively?
What is machine coding round in Uber?
How much time to prepare for Google?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.