Inheritance in C++ - Base and Derived Classes
Inheritance is a fundamental concept in object-oriented programming that allows you to create new classes (derived or child classes) based on existing classes (base or parent classes). It promotes code reuse and establishes a hierarchical relationship between classes. In this guide, we'll delve into the concept of inheritance in C++, with a focus on base and derived classes, and provide sample code to illustrate its usage.
Base Classes
A base class is the class from which another class (the derived class) inherits. It contains common attributes and behaviors that can be shared among multiple derived classes. Here's an example of a base class in C++:
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
double area() {
return 0.0;
}
void display() {
cout << "This is a shape." << endl;
}
};
int main() {
Shape shape;
cout << "Area: " << shape.area() << endl;
shape.display();
return 0;
}
In this example, the `Shape` class is a base class with common methods like `area` and `display`. Objects of this class can be created, but the `area` method returns a default value of 0.0.
Derived Classes
A derived class inherits properties and behaviors from a base class. It can also have its own attributes and methods. Here's an example of a derived class:
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
double area() {
return 0.0;
}
void display() {
cout << "This is a shape." << endl;
}
};
// Derived class
class Circle : public Shape {
public:
Circle(double r) : radius(r) {}
double area() {
return 3.14159 * radius * radius;
}
private:
double radius;
};
int main() {
Circle circle(5.0);
cout << "Area of Circle: " << circle.area() << endl;
circle.display();
return 0;
}
In this example, we have a derived class `Circle` that inherits from the base class `Shape`. The `Circle` class has its own `area` method to calculate the area of a circle. It overrides the base class method.
Access Control in Inheritance
In C++, you can control the access to the base class members in a derived class using access specifiers like `public`, `protected`, and `private`. These specifiers determine whether the members of the base class are visible and accessible in the derived class. Here's an example:
#include <iostream>
using namespace std;
class Base {
public:
int publicMember;
Base() : publicMember(42), protectedMember(21), privateMember(10) {}
void display() {
cout << "Base Class - Public Member: " << publicMember << endl;
}
protected:
int protectedMember;
private:
int privateMember;
};
class Derived : public Base {
public:
void show() {
cout << "Derived Class - Public Member: " << publicMember << endl;
cout << "Derived Class - Protected Member: " << protectedMember << endl;
}
};
int main() {
Derived derivedObj;
derivedObj.show();
return 0;
}
In this example, the `Base` class has public, protected, and private members. The `Derived` class inherits publicly from `Base`, which allows it to access the public and protected members, but not the private member of the base class.
Conclusion
Inheritance in C++ is a powerful mechanism that enables code reuse and establishes a hierarchy of classes. Base classes provide a foundation for derived classes, which can inherit and extend their behavior. Understanding inheritance is essential for designing well-structured and maintainable C++ programs.