Inheritance in C++
Inheritance is an OOP mechanism that allows a new class (derived) to inherit the properties and behaviors of an existing class (base). With inheritance, we can build an organized class hierarchy, reduce code duplication, and support polymorphism.
C++ supports three main types of inheritance: single inheritance, multilevel inheritance, and multiple inheritance. The access modifier used in inheritance determines how base class members are accessed in the derived class.
Code Example
#include <iostream>
#include <string>
using namespace std;
class Shape {
protected:
string color;
string name;
public:
Shape(string c, string n) : color(c), name(n) {}
virtual double area() {
return 0;
}
void display() {
cout << "Shape: " << name << ", Color: " << color << endl;
}
};
class Rectangle : public Shape {
private:
double width;
double height;
public:
Rectangle(string c, double w, double h) : Shape(c, "Rectangle") {
width = w;
height = h;
}
double area() override {
return width * height;
}
void displayDetails() {
display();
cout << "Width: " << width << ", Height: " << height << endl;
cout << "Area: " << area() << endl;
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(string c, double r) : Shape(c, "Circle") {
radius = r;
}
double area() override {
return 3.14159 * radius * radius;
}
void displayDetails() {
display();
cout << "Jari-jari: " << radius << endl;
cout << "Area: " << area() << endl;
}
};
class Animal {
public:
void eat() {
cout << "Makan" << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Guk guk!" << endl;
}
};
class Puppy : public Dog {
public:
void play() {
cout << "Bermain!" << endl;
}
};
class Cat : public Animal {
public:
void meow() {
cout << "Meong!" << endl;
}
};
int main() {
cout << "=== Rectangle ===" << endl;
Rectangle rect("Merah", 5, 3);
rect.displayDetails();
cout << "\n=== Circle ===" << endl;
Circle circ("Biru", 4);
circ.displayDetails();
cout << "\n=== Multilevel Inheritance ===" << endl;
Puppy puppy;
puppy.eat();
puppy.bark();
puppy.play();
cout << "\n=== Array of Shapes ===" << endl;
Shape* shapes[] = { &rect, &circ };
for (int i = 0; i < 2; i++) {
cout << "Area: " << shapes[i]->area() << endl;
}
return 0;
}
Code Explanation
-
class Shape- The base class containing the data memberscolorandname, as well as the virtual functionarea(). -
protected:- Members that can be accessed from within the class itself as well as from derived classes, but not from outside. -
class Rectangle : public Shape- Rectangle inherits all public and protected members from Shape.publicinheritance preserves the access level of the base class members. -
double area() override- Overrides the virtual function from the base class.overrideensures the function really does override the base class's virtual function. -
Shape(string c, string n) : color(c), name(n)- An initializer list. Initializes the base class's data members when the constructor is called. -
class Puppy : public Dog- Multilevel inheritance. Puppy inherits from Dog, which inherits from Animal. Puppy has all the functions from the three classes. -
Shape* shapes[]- An array of pointers to Shape. It can point to a Rectangle or a Circle (polymorphism). -
public,protected,privateinheritance -publicpreserves access,protectedchanges public to protected,privatechanges everything to private.
Everyday Use Cases
Imagine the animals in a zoo. All animals have a name, a color, and a way of eating. Cats, dogs, and birds are types of animals that inherit those basic traits but have unique behaviors (meowing, barking, chirping). A puppy is a young dog that inherits traits from the dog. When you view all the animals on a single information screen, the system uses polymorphism to display the area of each cage.
Conclusion
Inheritance allows a derived class to inherit members from a base class. public inheritance preserves the original access, protected changes public to protected, and private changes everything to private. Multilevel inheritance is a linear inheritance chain. Multiple inheritance allows a class to inherit from more than one base class. Use virtual inheritance to solve the diamond problem in multiple inheritance.