C++ inheritance
Inheritance is one of the core concepts of Object-Oriented Programming (OOP), and it is extensively used in C++. It 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). Inheritance provides a way to reuse code, increase modularity, and improve maintainability in a program.
C++ supports different types of inheritance, such as single inheritance, multiple inheritance, multilevel inheritance, and hierarchical inheritance.
In C++, inheritance allows a class to derive properties and behaviors from another class. This means that the derived class inherits all the members (attributes and methods) of the base class, but it can also have additional features or override the base class methods.
To create a derived class, you use the following syntax:
class DerivedClass : accessSpecifier BaseClass {
// Additional members and methods of the derived class
};
The access specifier can be:
C++ supports several types of inheritance. Let's explore each type:
In single inheritance, a derived class inherits from only one base class.
Example:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void speak() {
cout << "Animal speaks!" << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void bark() {
cout << "Dog barks!" << endl;
}
};
int main() {
Dog dog;
dog.speak(); // Inherited from Animal class
dog.bark(); // Defined in Dog class
return 0;
}
Explanation:
Dog
class is derived from the Animal
class.Dog
class inherits the speak()
function from the Animal
class.bark()
function is specific to the Dog
class.In multilevel inheritance, a class is derived from another class, and that derived class is further inherited by another class.
Example:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void speak() {
cout << "Animal speaks!" << endl;
}
};
// Derived class from Animal
class Mammal : public Animal {
public:
void walk() {
cout << "Mammal walks!" << endl;
}
};
// Further derived class from Mammal
class Dog : public Mammal {
public:
void bark() {
cout << "Dog barks!" << endl;
}
};
int main() {
Dog dog;
dog.speak(); // Inherited from Animal
dog.walk(); // Inherited from Mammal
dog.bark(); // Defined in Dog
return 0;
}
Explanation:
Dog
inherits from Mammal
, and Mammal
inherits from Animal
.Dog
class has access to all the functions from both Mammal
and Animal
.In multiple inheritance, a derived class can inherit from more than one base class.
Example:
#include <iostream>
using namespace std;
// Base class 1
class Animal {
public:
void speak() {
cout << "Animal speaks!" << endl;
}
};
// Base class 2
class Vehicle {
public:
void drive() {
cout << "Vehicle drives!" << endl;
}
};
// Derived class inheriting from both Animal and Vehicle
class Car : public Animal, public Vehicle {
public:
void honk() {
cout << "Car honks!" << endl;
}
};
int main() {
Car car;
car.speak(); // Inherited from Animal
car.drive(); // Inherited from Vehicle
car.honk(); // Defined in Car
return 0;
}
Explanation:
Car
class inherits from both Animal
and Vehicle
.Car
to have methods from both the Animal
and Vehicle
classes.In hierarchical inheritance, multiple classes inherit from a single base class.
Example:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void speak() {
cout << "Animal speaks!" << endl;
}
};
// Derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "Dog barks!" << endl;
}
};
// Derived class 2
class Cat : public Animal {
public:
void meow() {
cout << "Cat meows!" << endl;
}
};
int main() {
Dog dog;
dog.speak(); // Inherited from Animal
dog.bark(); // Defined in Dog
Cat cat;
cat.speak(); // Inherited from Animal
cat.meow(); // Defined in Cat
return 0;
}
Explanation:
Dog
and Cat
inherit from the same base class Animal
.speak()
function from Animal
.In C++, when a derived class inherits from a base class, the members of the base class can be accessed depending on the access specifier used:
In inheritance, the constructor and destructor of the base class are invoked when a derived class object is created or destroyed.
#include <iostream>
using namespace std;
class Animal {
public:
Animal() {
cout << "Animal constructor called!" << endl;
}
~Animal() {
cout << "Animal destructor called!" << endl;
}
};
class Dog : public Animal {
public:
Dog() {
cout << "Dog constructor called!" << endl;
}
~Dog() {
cout << "Dog destructor called!" << endl;
}
};
int main() {
Dog dog;
return 0;
}
Output:
Animal constructor called!
Dog constructor called!
Dog destructor called!
Animal destructor called!
Explanation:
Animal
) is called first, followed by the constructor of the derived class (Dog
).