Inheritance is one of the key features of Object-Oriented Programming (OOP) that allows a class (called the derived class) to inherit properties and behaviors (attributes and methods) from another class (called the base class or parent class). It helps in reusing code, enhancing the modularity of the system, and promoting the idea of generalization and specialization.
Inheritance represents the "is-a" relationship between classes. For example, a Dog is a type of Animal, so Dog is a derived class of Animal.
public, protected, private).Single Inheritance: A derived class inherits from a single base class.
Example:
class Base { ... };
class Derived : public Base { ... };
Multiple Inheritance: A derived class inherits from more than one base class.
Example:
class Base1 { ... };
class Base2 { ... };
class Derived : public Base1, public Base2 { ... };
Multilevel Inheritance: A derived class inherits from a base class, and then another class inherits from the derived class, forming a chain.
Example:
class Base { ... };
class Derived : public Base { ... };
class SubDerived : public Derived { ... };
Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
Example:
class Base { ... };
class Derived1 : public Base { ... };
class Derived2 : public Base { ... };
Hybrid Inheritance: A combination of two or more types of inheritance (e.g., multiple and multilevel inheritance).
Example:
class Base1 { ... };
class Base2 { ... };
class Derived : public Base1, public Base2 { ... };
class SubDerived : public Derived { ... };
class Base {
public:
// Base class code
};
class Derived : access_modifier Base {
public:
// Derived class code
};
public, protected, or private).Public Inheritance: Public and protected members of the base class become public and protected members of the derived class, respectively. This is the most commonly used type of inheritance.
class Derived : public Base { ... };
Protected Inheritance: Public and protected members of the base class become protected members in the derived class. It restricts access to the inherited members outside the derived class.
class Derived : protected Base { ... };
Private Inheritance: Public and protected members of the base class become private members in the derived class, making them inaccessible from outside the derived class.
class Derived : private Base { ... };
Constructor: When a derived class object is created, the constructor of the base class is called first, followed by the constructor of the derived class. The base class constructor is automatically called unless specified otherwise.
Destructor: The destructor of the derived class is called first, and then the destructor of the base class is called. If the base class has a virtual destructor, it ensures that the derived class's destructor is properly called when the object is deleted through a base class pointer.
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
int main() {
Dog dog;
dog.eat(); // Inherited from Animal class
dog.bark(); // Defined in Dog class
return 0;
}
Explanation:
Dog class inherits the eat() function from the Animal class and also defines its own function bark().dog of type Dog and call both eat() (inherited) and bark() (defined in Dog).Output:
Eating...
Barking...
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Bird {
public:
void fly() {
cout << "Flying..." << endl;
}
};
class Bat : public Animal, public Bird {
public:
void sleep() {
cout << "Sleeping..." << endl;
}
};
int main() {
Bat bat;
bat.eat(); // Inherited from Animal
bat.fly(); // Inherited from Bird
bat.sleep(); // Defined in Bat
return 0;
}
Explanation:
Bat class inherits from both Animal and Bird, so it can access both eat() and fly(). Additionally, sleep() is defined within Bat.Output:
Eating...
Flying...
Sleeping...
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Mammal : public Animal {
public:
void walk() {
cout << "Walking..." << endl;
}
};
class Dog : public Mammal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
int main() {
Dog dog;
dog.eat(); // Inherited from Animal
dog.walk(); // Inherited from Mammal
dog.bark(); // Defined in Dog
return 0;
}
Explanation:
Dog class inherits from Mammal, which in turn inherits from Animal.dog can access the methods from all three classes: eat(), walk(), and bark().Output:
Eating...
Walking...
Barking...
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
class Cat : public Animal {
public:
void meow() {
cout << "Meowing..." << endl;
}
};
int main() {
Dog dog;
Cat cat;
dog.eat(); // From Animal class
dog.bark(); // From Dog class
cat.eat(); // From Animal class
cat.meow(); // From Cat class
return 0;
}
Explanation:
Dog and Cat classes inherit from Animal. As a result, both classes have access to the eat() method from Animal.Output:
Eating...
Barking...
Eating...
Meowing...
A class that contains at least one pure virtual function is called an abstract class. An abstract class cannot be instantiated directly, and it forces derived classes to provide an implementation for the pure virtual function.
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() = 0; // Pure virtual function
};
class Dog : public Animal {
public:
void sound() override {
cout << "Bark" << endl;
}
};
int main() {
// Animal a; // Error: cannot instantiate abstract class
Dog dog;
dog.sound(); // Bark
return 0;
}
Explanation:
sound() function is pure virtual in the Animal class. This forces any derived class, like Dog, to implement this function.Output:
Bark
public, protected, or private inheritance).Open this section to load past papers