Metadata

  • πŸ“… Date :: 12-06-2025
  • 🏷️ Tags :: cpp

Notes

Inheritance in C++

Inheritance is one of the core concepts of Object-Oriented Programming (OOP). It allows a class to inherit attributes and methods from another class, helping in code reusability and organization. This concept is analogous to how children inherit certain characteristics from their parents in biology.

Key Concepts:

  1. Parent Class (Base Class): The class that is being inherited from.
  2. Child Class (Derived Class): The class that inherits from the parent class.
  3. Access Modifier (public/private/protected): Determines the visibility of inherited members.

Inheritance in C++ helps in organizing the code better by allowing shared attributes and methods to be placed in the parent class, thus reducing redundancy and enhancing maintainability.

Syntax for Inheritance:

To inherit from a parent class, you use the : symbol, followed by the access modifier (public, protected, or private), and then the name of the parent class.

class Dog : public Animal {
    // Dog class inherits from Animal class
};
 
  • Public inheritance means that the public and protected members of the parent class are accessible from the child class.

Basic Example:

Let’s break down the example of an Animal class and its derived classes Dog and Cat.

1. Animal Class (Parent Class)

This class contains a shared attribute (alive) and a shared method (eat) for all animals.

class Animal {
public:
    bool alive; // All animals are alive
    void eat() {
        std::cout << "This animal is eating." << std::endl;
    }
};
 

2. Dog Class (Child Class)

The Dog class inherits the attributes and methods of the Animal class. Additionally, it adds a method specific to dogs: bark.

class Dog : public Animal {
public:
    void bark() {
        std::cout << "The dog goes woof." << std::endl;
    }
};
 

When you create a Dog object, it has access to both the inherited alive attribute and eat() method, as well as its own method bark().

3. Cat Class (Child Class)

Similarly, the Cat class inherits from the Animal class but adds a method specific to cats: meow.

class Cat : public Animal {
public:
    void meow() {
        std::cout << "The cat goes meow." << std::endl;
    }
};
 

Usage Example:

int main() {
    Dog dog1;
    dog1.alive = true;
    dog1.eat();  // Inherited method from Animal
    dog1.bark(); // Dog-specific method
 
    Cat cat1;
    cat1.alive = true;
    cat1.eat();  // Inherited method from Animal
    cat1.meow(); // Cat-specific method
}
 

Output:

This animal is eating.
The dog goes woof.
This animal is eating.
The cat goes meow.

Benefits of Inheritance:

  1. Code Reusability: Instead of writing similar code in each class, you can write it once in the parent class and reuse it across multiple child classes.
  2. Maintaining Code: If a method (like eat()) needs to change, you only need to change it in the parent class, and all child classes will automatically have the updated method.
  3. Hierarchical Structure: Helps in organizing classes in a way that represents real-world relationships (e.g., Animals β†’ Dogs, Cats).

When to Use Inheritance:

  • Use inheritance when multiple classes share common functionality. Instead of duplicating code, place the shared code in a parent class.
  • Avoid inheritance when the relationship between the classes does not represent an β€œis-a” relationship. For instance, a Car class and Engine class should not inherit from each other because a car has-a engine, not an is-a relationship.

Further Example: Shape Class Hierarchy

Let’s look at another example with a Shape class and two child classes: Cube and Sphere.

Parent Class (Shape)

This class has two shared attributes: area and volume.

class Shape {
public:
    double area;
    double volume;
};
 

Cube Class (Child Class)

The Cube class will inherit from the Shape class and calculate its area and volume based on the side length.

class Cube : public Shape {
public:
    double side;
    Cube(double s) {
        side = s;
        area = 6 * side * side;  // Cube has 6 sides
        volume = side * side * side; // Cube's volume
    }
};
 

Sphere Class (Child Class)

The Sphere class will inherit from Shape and calculate its area and volume based on the radius.

class Sphere : public Shape {
public:
    double radius;
    Sphere(double r) {
        radius = r;
        area = 4 * 3.14 * radius * radius;  // Surface area of a sphere
        volume = (4.0 / 3.0) * 3.14 * radius * radius * radius;  // Volume of a sphere
    }
};
 

Usage of Shape, Cube, and Sphere Classes:

int main() {
    Cube cube1(10); // Cube with side 10
    std::cout << "Cube Area: " << cube1.area << " Volume: " << cube1.volume << std::endl;
 
    Sphere sphere1(5); // Sphere with radius 5
    std::cout << "Sphere Area: " << sphere1.area << " Volume: " << sphere1.volume << std::endl;
}
 

Output:

Cube Area: 600 Volume: 1000
Sphere Area: 314 Volume: 523

Key Points to Remember:

  1. Code Reusability: Inheritance reduces redundancy. Shared attributes and methods are defined in the parent class and inherited by the child class.
  2. Access Control: You can control the accessibility of inherited methods using access specifiers (public, protected, private).
  3. Overriding Methods: If the child class needs to modify a method from the parent class, you can override the inherited method.
  4. Constructor Inheritance: Constructors are not inherited directly, but they can be called using the constructor initializer list in the child class.

Conclusion:

Inheritance is a powerful feature of OOP that allows you to create a hierarchical relationship between classes. By inheriting from a parent class, child classes automatically gain its attributes and methods, which can be extended or overridden. This leads to more organized, reusable, and maintainable code.


References