Inheritance in C++

Inheritance in C++

Inheritance in C++

Inheritance in C++ is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit properties and methods from another class. In C++, you can create a new class that is a modified version of an existing class by deriving it from the existing class. The new class is called the derived class, and the existing class is called the base class.

To create a derived class in C++, you use the class keyword followed by the derived class name and the base class name in parentheses, like this:

				
					#include <iostream>

class Animal {
public:
    void eat() {
        std::cout << "The animal is eating." << std::endl;
  }

    virtual void speak() {
        std::cout << "The animal makes a sound." << std::endl;
    }
};

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

    virtual void speak() override {
        std::cout << "The dog says woof." << std::endl;
    }
};

int main() {
    Dog myDog;
    myDog.eat();
    myDog.bark();
    myDog.speak();
   return 0;
}

				
			

In this example, we have a base class called Animal with a eat method and a speak method. The speak method is declared as virtual, which means that it can be overridden by derived classes.

We also have a derived class called Dog that inherits from Animal. Dog adds its own bark method and overrides the speak method to make the dog say “woof”.

In the main function, we create an instance of Dog and call its eat, bark, and speak methods. The speak method is called using dynamic dispatch, which means that the version of the method that is called depends on the actual type of the object at runtime. In this case, since myDog is a Dog object, the Dog version of speak is called, and the output is “The dog says woof.”

There are five types of inheritance:

  1. Single inheritance: A class inherits properties from a single base class.

Example:

				
					class Animal {
public: void eat() {
    		    cout << "Animal is eating" << endl;
   			 }
};

class Dog : public Animal {
public:
    void bark() {
        cout << "Dog is barking" << endl;
    }
};
int main() {
    Dog dog;
    dog.eat(); // Animal is eating
    dog.bark(); // Dog is barking
    return 0;
}

				
			

In this example, Dog is derived from Animal using single inheritance. Dog inherits the eat() function from Animal, and also has its own bark() function.

  1. Multiple inheritance: A class inherits properties from multiple base classes.

Example:

				
					class Person {
public: void walk() {
        			cout << "Person is walking" << endl;
    } };
class Student {
public:
    void study() {
        cout << "Student is studying" << endl;
    }
};

class Scholar : public Person, public Student {
public:
    void research() {
        cout << "Scholar is researching" << endl;
    }
};
int main() {
    Scholar scholar;
    scholar.walk(); // Person is walking
    scholar.study(); // Student is studying
    scholar.research(); // Scholar is researching
    return 0;
}

				
			

In this example, Scholar is derived from both Person and Student using multiple inheritance. Scholar inherits the walk() function from Person and the study() function from Student, and also has its own research() function.   

  1. Hierarchical inheritance: Multiple derived classes inherit properties from a single base class.

Example:

				
					class Animal {
public: void eat() {
        cout << "Animal is eating" << endl;
    }
};

class Cat : public Animal {
public:
    void meow() {
        cout << "Cat is meowing" << endl;
    }
};

class Dog : public Animal {
public:
    void bark() {
        cout << "Dog is barking" << endl;
    }
};

int main() {
    Cat cat;
    cat.eat(); // Animal is eating
    cat.meow(); // Cat is meowing
    Dog dog;
    dog.eat(); // Animal is eating
    dog.bark(); // Dog is barking
    
    return 0;
}

				
			

In this example, both Cat and Dog are derived from Animal using hierarchical inheritance. Cat and Dog both inherit the eat() function from Animal, and also have their own meow() and bark() functions, respectively.

  1. Multilevel inheritance: A class is derived from another derived class.

Example:

				
					class Animal {
public:
    void eat() {
        cout << "Animal is eating" << endl;
    }
};

class Mammal : public Animal {
public:
    void nurse() {
        cout << "Mammal is nursing" << endl;
    }
};
class Dog : public Mammal {
public:
    void bark() {
        cout << "Dog is barking" << endl;
    } };

   int main() {
    Dog dog;
    dog.eat(); // Animal is eating
    dog.nurse(); // Mammal is nursing
    dog.bark(); // Dog is barking
    return 0;
}

				
			

In this example, Dog is derived from Mammal, which is itself derived from Animal. Dog inherits the eat() function from Animal, the nurse() function from Mammal, and also has its own bark() function.

  1. Hybrid inheritance: Combination of multiple and hierarchical inheritance.

Example:

				
					class Animal {
public:
    void eat() {
        cout << "I can eat." << endl;
    }
};

class Mammal : public Animal {
public:
    void walk() {
        cout << "I can walk." << endl;
    }
};
class Bird {
public:
    void fly() {
        cout << "I can fly." << endl;
    }};
class Bat : public Mammal, public Bird {
public:
    void nocturnal() {
        cout << "I am nocturnal." << endl;
    }
};

int main() {
    Bat myBat;
    myBat.eat(); // inherited from Animal through Mammal
    myBat.walk();
    myBat.fly(); // inherited from Bird
    myBat.nocturnal();
    return 0;
}

				
			
Join To Get Our Newsletter
Spread the love