5. How do you implement inheritance in C++?

Basic

5. How do you implement inheritance in C++?

Overview

Inheritance in C++ is a fundamental concept that allows the creation of new classes from existing ones. It enhances code reusability, enables polymorphism, and helps in the creation of hierarchical class structures. Understanding inheritance is crucial for designing efficient and modular C++ applications.

Key Concepts

  • Base and Derived Classes: The class being inherited is called the base class, and the class that inherits is the derived class.
  • Types of Inheritance: Single, multiple, multilevel, hierarchical, and hybrid inheritance.
  • Access Specifiers: Public, protected, and private inheritance impact the accessibility of base class members in the derived class.

Common Interview Questions

Basic Level

  1. What is inheritance and why is it used in C++?
  2. How do you create a simple class hierarchy using public inheritance in C++?

Intermediate Level

  1. How does protected inheritance differ from private inheritance in C++?

Advanced Level

  1. Discuss the implications of multiple inheritance in C++ and how ambiguity can be resolved.

Detailed Answers

1. What is inheritance and why is it used in C++?

Answer: Inheritance allows a class (derived class) to inherit properties and behaviors (methods and variables) from another class (base class). It's used in C++ for code reusability, to implement polymorphism, and to establish a relationship between classes that share common attributes and methods, thereby reducing redundancy and increasing clarity.

Key Points:
- Promotes code reusability.
- Helps in implementing polymorphism.
- Facilitates the creation of a class hierarchy.

Example:

class Animal {                          // Base class
public:
    void eat() {
        cout << "Eating..." << endl;
    }
};

class Dog : public Animal {             // Derived class
public:
    void bark() {
        cout << "Barking..." << endl;
    }
};

2. How do you create a simple class hierarchy using public inheritance in C++?

Answer: Public inheritance in C++ is used to derive a class from another class, allowing the derived class to access public and protected members of the base class as if they were its own. A simple class hierarchy involves creating a base class followed by a derived class that extends the base class using the public keyword.

Key Points:
- Derived class inherits all public and protected members of the base class.
- Private members of the base class are not accessible directly by the derived class.
- Public inheritance signifies an "is-a" relationship.

Example:

class Vehicle {                  // Base class
public:
    void move() {
        cout << "Moving..." << endl;
    }
};

class Car : public Vehicle {     // Derived class
public:
    void accelerate() {
        cout << "Accelerating..." << endl;
    }
};

3. How does protected inheritance differ from private inheritance in C++?

Answer: In C++, protected inheritance makes the public and protected members of the base class protected in the derived class, while private inheritance makes them private in the derived class. Protected inheritance is used when you want the derived classes to have access to the inherited members, but not the objects of those classes. Private inheritance is used when you want to restrict access to the base class members to the derived class only, effectively treating the base class as a private implementation detail.

Key Points:
- Protected inheritance allows derived classes further down the hierarchy to access the inherited members.
- Private inheritance hides the base class members from even derived classes, treating them as private members of the derived class.
- Both protected and private inheritance do not allow objects of the derived class to access the inherited members directly.

Example:

class Base {};

class ProtectedDerived : protected Base {};    // Protected inheritance
class PrivateDerived : private Base {};        // Private inheritance

4. Discuss the implications of multiple inheritance in C++ and how ambiguity can be resolved.

Answer: Multiple inheritance in C++ allows a derived class to inherit from more than one base class. This can lead to ambiguity if the same member exists in more than one base class. Ambiguity can be resolved by explicitly specifying the base class from which the member is to be used.

Key Points:
- Can lead to the "Diamond problem" where the derived class inherits the same member from multiple paths.
- Use of virtual inheritance can mitigate some issues by sharing common base classes.
- Ambiguities must be resolved explicitly by the programmer using the scope resolution operator.

Example:

class Base1 {
public:
    void func() { cout << "From Base1" << endl; }
};

class Base2 {
public:
    void func() { cout << "From Base2" << endl; }
};

class Derived : public Base1, public Base2 {
public:
    void callFunc() {
        Base1::func();  // Resolving ambiguity by specifying the base class
    }
};

This guide covers the basics of implementing inheritance in C++, providing a solid foundation for understanding more complex inheritance scenarios and their implications in software design.