Understanding Inheritance in C++
C++ | Tips
nheritance is a fundamental concept in object-oriented programming (OOP) that allows us to create classes based on other classes, promoting code reuse and organization. In C++, inheritance enables us to derive a class from an existing one, allowing the new class to inherit methods and attributes. In this post, we’ll dive into different types of inheritance with examples to illustrate the functionality.
Check Github Repo :-
1. Basic Inheritance
Let’s start with a simple example:
#include <iostream>
class Animal {
protected:
std::string name;
public:
void speak() {
std::cout << name << " speaks." << std::endl;
}
void setName(const std::string& n) {
name = n;
set();
}
private:
void set() {
std::cout << "New animal created." << std::endl;
}
};
class Dog : public Animal {
public:
Dog(std::string name) {
setName(name);
}
void bark() {
std::cout << "Dog " << name << " barks: Woof!" << std::endl;
}
};
int main() {
Dog myDog("Tommy");
myDog.bark();
myDog.speak();
return 0;
}
New animal created.
Dog Tommy barks: Woof!
Tommy speaks.
Explanation:
- Access Specifiers: The
protected
access specifier in theAnimal
class allows theDog
subclass to access thename
attribute, but it remains hidden from outside classes. - Private Methods: The
set
method inAnimal
is private, so it cannot be directly accessed byDog
. When a name is assigned, theset
function outputs a message indicating the creation of a new animal.
2. Multiple Inheritance
Multiple inheritance is when a class inherits from more than one base class. While powerful, it requires careful handling to avoid ambiguity.
class Mammal {
public:
void feedMilk() {
std::cout << "Feeds milk." << std::endl;
}
};
class Bird {
public:
void layEggs() {
std::cout << "Lays eggs." << std::endl;
}
};
class Platypus : public Mammal, public Bird {
public:
void specialAbility() {
std::cout << "Can do both!" << std::endl;
}
};
int main() {
Platypus perry;
perry.feedMilk();
perry.layEggs();
perry.specialAbility();
return 0;
}
Feeds milk.
Lays eggs.
Can do both!
Explanation:
The Platypus
class inherits from both Mammal
and Bird
, meaning it can use methods from both classes. Multiple inheritance is an ideal choice here, as it accurately represents the unique characteristics of a platypus.
3. Virtual Inheritance
When using multiple inheritance, ambiguity can arise if multiple base classes inherit from a common base class. Virtual inheritance solves this problem by ensuring only one instance of the shared base class exists.
class Device {
public:
void powerOn() {
std::cout << "Device is powering on." << std::endl;
}
};
class Computer : virtual public Device {};
class Phone : virtual public Device {};
class SmartPhone : public Computer, public Phone {
public:
void useApp() {
std::cout << "Using an app on the smartphone." << std::endl;
}
};
int main() {
SmartPhone myPhone;
myPhone.powerOn();
myPhone.useApp();
return 0;
}
Device is powering on.
Using an app on the smartphone.
Explanation:
Computer
and Phone
both inherit from Device
. Without virtual inheritance, the SmartPhone
class would inherit two copies of Device
. However, by using virtual public
inheritance, only one Device
instance is shared between Computer
and Phone
, avoiding ambiguity.
Conclusion
Inheritance in C++ enables you to create clean, modular code by extending existing classes, promoting both flexibility and reusability. Through public, protected, and private inheritance, C++ offers nuanced control over how attributes and methods are inherited.