Welcome to The Coding College! In this tutorial, we’ll dive into Inheritance, a core feature of Object-Oriented Programming (OOP) in C++. Inheritance allows one class (child class) to acquire the properties and methods of another class (parent class). This enables code reuse, hierarchy creation, and extensibility in C++ programs.
What Is Inheritance?
Inheritance is a mechanism by which one class, called the derived class, inherits attributes and methods from another class, called the base class.
Key Concepts:
- Base Class: The parent class from which properties and methods are inherited.
- Derived Class: The child class that inherits the properties and methods of the base class.
Syntax:
class BaseClass {
// Base class members
};
class DerivedClass : accessSpecifier BaseClass {
// Derived class members
};
Access Specifiers in Inheritance:
The inheritance relationship is defined using the access specifiers:
- public: Public and protected members of the base class remain public and protected in the derived class.
- protected: Public and protected members of the base class become protected in the derived class.
- private: Public and protected members of the base class become private in the derived class.
Example: Basic Inheritance
#include <iostream>
using namespace std;
// Base Class
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
// Derived Class
class Dog : public Animal {
public:
void bark() {
cout << "The dog barks." << endl;
}
};
int main() {
Dog myDog;
myDog.eat(); // Inherited from Animal
myDog.bark(); // Defined in Dog
return 0;
}
Output:
This animal eats food.
The dog barks.
Types of Inheritance
1. Single Inheritance
A derived class inherits from a single base class.
class BaseClass { };
class DerivedClass : public BaseClass { };
2. Multilevel Inheritance
A derived class inherits from another derived class.
class BaseClass { };
class IntermediateClass : public BaseClass { };
class DerivedClass : public IntermediateClass { };
3. Multiple Inheritance
A derived class inherits from more than one base class.
class BaseClass1 { };
class BaseClass2 { };
class DerivedClass : public BaseClass1, public BaseClass2 { };
4. Hierarchical Inheritance
Multiple derived classes inherit from a single base class.
class BaseClass { };
class DerivedClass1 : public BaseClass { };
class DerivedClass2 : public BaseClass { };
5. Hybrid Inheritance
A combination of multiple and hierarchical inheritance.
Example: Multilevel Inheritance
#include <iostream>
using namespace std;
// Base Class
class Vehicle {
public:
void move() {
cout << "This vehicle moves." << endl;
}
};
// Intermediate Class
class Car : public Vehicle {
public:
void fuelType() {
cout << "This car uses petrol or diesel." << endl;
}
};
// Derived Class
class SportsCar : public Car {
public:
void speed() {
cout << "This sports car is very fast." << endl;
}
};
int main() {
SportsCar myCar;
myCar.move(); // From Vehicle
myCar.fuelType(); // From Car
myCar.speed(); // From SportsCar
return 0;
}
Output:
This vehicle moves.
This car uses petrol or diesel.
This sports car is very fast.
Example: Multiple Inheritance
#include <iostream>
using namespace std;
// Base Class 1
class Engine {
public:
void engineType() {
cout << "This is a V8 engine." << endl;
}
};
// Base Class 2
class Body {
public:
void bodyType() {
cout << "This is a sedan body." << endl;
}
};
// Derived Class
class Car : public Engine, public Body {
public:
void display() {
cout << "Car specifications:" << endl;
}
};
int main() {
Car myCar;
myCar.display();
myCar.engineType(); // From Engine
myCar.bodyType(); // From Body
return 0;
}
Output:
Car specifications:
This is a V8 engine.
This is a sedan body.
Protected Members in Inheritance
Protected members of a base class are accessible in the derived class but remain hidden from outside the class hierarchy.
Example:
#include <iostream>
using namespace std;
class Parent {
protected:
int value;
public:
void setValue(int v) {
value = v;
}
};
class Child : public Parent {
public:
void showValue() {
cout << "Value: " << value << endl; // Access protected member
}
};
int main() {
Child obj;
obj.setValue(42); // Set value using public method
obj.showValue(); // Access protected value in derived class
return 0;
}
Output:
Value: 42
Access Control in Inheritance
The following table summarizes how the access specifier of the base class affects inheritance:
Base Class Member | Public Inheritance | Protected Inheritance | Private Inheritance |
---|---|---|---|
public | public | protected | private |
protected | protected | protected | private |
private | Not Inherited | Not Inherited | Not Inherited |
Real-Life Example: Employee Management
#include <iostream>
using namespace std;
// Base Class
class Employee {
protected:
string name;
int id;
public:
void setDetails(string n, int i) {
name = n;
id = i;
}
};
// Derived Class
class Manager : public Employee {
public:
void displayDetails() {
cout << "Manager Name: " << name << ", ID: " << id << endl;
}
};
int main() {
Manager mgr;
mgr.setDetails("Alice", 101);
mgr.displayDetails();
return 0;
}
Output:
Manager Name: Alice, ID: 101
Key Points to Remember
- Inheritance promotes code reuse and modularity.
- Always use the correct access specifier when inheriting classes.
- Use protected for data you want accessible in derived classes but hidden from outside.
- Multiple inheritance can lead to complexity; use it cautiously.
Explore More at The Coding College
Discover more C++ tutorials and programming insights at The Coding College.