Inheritance in C++
Inheritance is one of the fundamental concepts of object-oriented programming (OOP) in C++. It allows one class (called the derived or child class) to inherit attributes and methods from another class (called the base or parent class).
1. What is Inheritance?​
Inheritance enables the creation of new classes based on existing ones. The derived class inherits the properties (attributes and behaviors) of the base class and can add its own properties or override the inherited ones.
2. Types of Inheritance in C++​
C++ supports various types of inheritance:
a. Single Inheritance:​
A derived class inherits from a single base class.
class Base {
// Base class code
};
class Derived : public Base {
// Derived class code
};
b. Multiple Inheritance:​
A derived class can inherit from more than one base class.
class Base1 {
// Base class 1 code
};
class Base2 {
// Base class 2 code
};
class Derived : public Base1, public Base2 {
// Derived class code
};
c. Multilevel Inheritance:​
A class is derived from another derived class.
class Base {
// Base class code
};
class Intermediate : public Base {
// Intermediate derived class code
};
class Derived : public Intermediate {
// Final derived class code
};
d. Hierarchical Inheritance:​
Multiple derived classes inherit from the same base class.
class Base {
// Base class code
};
class Derived1 : public Base {
// Derived class 1 code
};
class Derived2 : public Base {
// Derived class 2 code
};
e. Hybrid Inheritance:​
Combination of two or more types of inheritance.
class Base {
// Base class code
};
class Derived1 : public Base {
// Derived class 1 code
};
class Derived2 : public Derived1 {
// Derived class 2 code
};
3. Access Specifiers in Inheritance​
When a class is derived, the accessibility of base class members depends on the access specifier used. The most common access specifiers are:
- public: Members of the base class remain in the same access level in the derived class.
- protected: Public members of the base class become protected in the derived class.
- private: All members of the base class become private in the derived class.
4. Syntax of Inheritance​
Basic Syntax:​
class Base {
// Base class definition
};
class Derived : public Base {
// Derived class definition
};
5. Example of Single Inheritance​
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "The dog barks." << endl;
}
};
int main() {
Dog myDog;
myDog.eat(); // Inherited from Animal class
myDog.bark(); // Defined in Dog class
return 0;
}
Output:​
This animal eats food.
The dog barks.
6. Overriding Inherited Methods​
A derived class can override the methods of the base class to provide its own implementation.
Example:​
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() {
cout << "Animals make sound." << endl;
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "Dogs bark." << endl;
}
};
int main() {
Animal* animalPtr;
Dog myDog;
animalPtr = &myDog;
animalPtr->sound(); // Calls Dog's sound() method
return 0;
}
Output:​
Dogs bark.
7. Constructors in Inheritance​
Constructors of the base class are not inherited by the derived class. However, the base class constructor can be called from the derived class.
Example:​
#include <iostream>
using namespace std;
class Base {
public:
Base() {
cout << "Base class constructor." << endl;
}
};
class Derived : public Base {
public:
Derived() {
cout << "Derived class constructor." << endl;
}
};
int main() {
Derived obj; // Calls both Base and Derived class constructors
return 0;
}
Output:​
Base class constructor.
Derived class constructor.
8. Advantages of Inheritance​
- Code Reusability: Inheritance allows the reuse of code written in base classes.
- Extensibility: Existing classes can be easily extended to add new functionality.
- Maintainability: Changes in the base class are automatically reflected in the derived classes.
9. Final Thoughts​
Inheritance in C++ is a powerful tool that allows you to extend and reuse existing code, making your programs more efficient and modular. However, it is essential to use it wisely to avoid complexity and maintain clarity in your codebase.
Happy coding!