Scroll Progress Bar

Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP), and C++ supports inheritance as a key feature. Inheritance allows to create a new class (the derived class or subclass) based on an existing class (the base class or superclass). The derived class inherits the properties (data members) and behaviors (member functions) of the base class and it can extend or modify them. Here's how inheritance works in C++:

1. Defining a Base Class:

start by defining a base class, which serves as the template for the derived class. The base class contains data members and member functions.

Program:

class Shape {
public:
    double area() {
        return 0.0;
    }

    void display() {
        // Display information about the shape
    }
};
2. Creating a Derived Class:

create a derived class by specifying the base class from which it should inherit. In C++, this is done using the class keyword followed by a colon : and the access specifier (public, private, or protected) for inheritance.

Program:

class Circle : public Shape {
public:
    double radius;

    double area() {
        return 3.14159 * radius * radius;
    }
};

In this example, Circle is a derived class that inherits from the Shape base class.

3. Accessing Base Class Members:

Derived classes inherit the members of the base class. it can access these inherited members using the dot . operator.

Program:

Circle myCircle;
myCircle.radius = 5.0;
double circleArea = myCircle.area(); // Calls Circle's area function
myCircle.display(); // Calls Shape's display function (inherited)
4. Overriding Base Class Members:

Derived classes it can override base class members (both data members and member functions) to provide their own implementations.

Program:

class Triangle : public Shape {
public:
    double base;
    double height;

    double area() {
        return 0.5 * base * height; // Override Shape's area function
    }
};
5. Access Control:

The public, private, and protected access specifiers in the base class affect how members are inherited and accessed by the derived class. By default, members of a base class with public access specifier are inherited as public in the derived class, members with protected access specifier are inherited as protected, and members with private access specifier are not inherited.

6. Constructors and Destructors:

Constructors and destructors of the base class are also inherited by the derived class. They are called automatically when objects of the derived class are created and destroyed.

7. Multiple Inheritance:

C++ allows a class to inherit from multiple base classes. This is called multiple inheritance.

Program:

class Child : public Parent1, public Parent2 {
    // ...
};
8. Access Control Modifiers:

it can use access control modifiers like public, protected, and private to control the visibility and access of base class members in the derived class.

Inheritance is a powerful mechanism in C++ that promotes code reuse and the creation of more specialized classes based on existing ones. It forms the foundation for building complex class hierarchies and polymorphism. A sample code for inheritance with sample output and programming comments.

Program:

#include <iostream>

// Base class
class Animal {
public:
    Animal(const std::string& name, int age) : name(name), age(age) {}

    void eat() {
        std::cout << name << " is eating." << std::endl;
    }

    void sleep() {
        std::cout << name << " is sleeping." << std::endl;
    }

    void makeSound() {
        std::cout << name << " makes a generic animal sound." << std::endl;
    }

protected:
    std::string name;
    int age;
};

// Derived class (inherits from Animal)
class Dog : public Animal {
public:
    Dog(const std::string& name, int age, const std::string& breed) : Animal(name, age), breed(breed) {}

    void makeSound() {
        std::cout << name << " barks: Woof! Woof!" << std::endl;
    }

    void fetch() {
        std::cout << name << " is fetching a ball." << std::endl;
    }

private:
    std::string breed;
};

int main() {
    // Create an Animal object
    Animal generiit canimal("Generic Animal", 5);
    std::cout << "Animal Info:" << std::endl;
    generiit canimal.eat();
    generiit canimal.sleep();
    generiit canimal.makeSound();
    std::cout << std::endl;

    // Create a Dog object (derived from Animal)
    Dog myDog("Buddy", 3, "Golden Retriever");
    std::cout << "Dog Info:" << std::endl;
    myDog.eat();      // Inherited from Animal
    myDog.sleep();    // Inherited from Animal
    myDog.makeSound(); // Overridden in Dog
    myDog.fetch();    // Specific to Dog
    std::cout << std::endl;

    return 0;
}
Output:

Animal Info:
Generic Animal is eating.
Generic Animal is sleeping.
Generic Animal makes a generic animal sound.

Dog Info:
Buddy is eating.
Buddy is sleeping.
Buddy barks: Woof! Woof!
Buddy is fetching a ball.
In this example:
  • A base class Animal with common attributes and behaviors.
  • The Dog class is derived from Animal and inherits its attributes and behaviors.
  • The Dog class overrides the makeSound function to provide a specialized behavior.
  • In the main function, we create both Animal and Dog objects and demonstrate how inheritance works, including method overriding and accessing base class members.
  • This example illustrates the concept of inheritance in C++ and how derived classes it can extend and modify the behavior of base classes.

question


answer

question2


answer2