Mastering Inheritance in Object-Oriented C++ Programming Language
In the realm of object-oriented programming, one of the most pivotal and empowering concepts is inheritance
In the realm of object-oriented programming, one of the most pivotal and empowering concepts is inheritance
When embarking on the creation of a new class, rather than laboriously crafting fresh data members and member functions, the programmer can simply dictate that the new class inherits the traits of an existing class. The originating class is christened the base class, while the newly born class inherits the title of the derived class.
Inheritance embodies the notion of an “is a” relationship. To elucidate, consider the hierarchy of a mammal being an animal, a dog being a mammal, thereby concluding that a dog is inherently an animal.
A class can derive from multiple base classes, effortlessly inheriting both data and functions from various sources. To establish a derived class, a class derivation list is employed, stipulating the base class(es) in a distinct format. The structure of this declaration follows the pattern:
class derived-class: access-specifier base-classThe access-specifier assumes one of three roles: public, protected, or private, while base-class references an existing class. If no access-specifier is specified, private is assumed.
An illustration can be found in the foundation class Shape and its inheritor, the class Rectangle:
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Printing the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}This example magnificently showcases the implementation of inheritance with the Rectangle class building upon the foundation laid by the Shape class.
A derived class enjoys access to all non-private constituents of its base class. Consequently, base-class members intended to remain hidden from derived-class member functions should be designated as private within the base class.
Summarizing the accessibility across various contexts:
| Access | Same Class | Derived Classes | Outside Classes |
|---|---|---|---|
| Public | Yes | Yes | Yes |
| Protected | Yes | Yes | No |
| Private | Yes | No | No |
Inheritance can be implemented through public, protected, or private inheritance. The access-specifier determines the type:
C++ emboldens the concept of multiple inheritance, wherein a class can inherit from more than one class simultaneously. The syntax for this advanced form follows:
class derived-class: access baseA, access baseB...Where access is public, protected, or private, and multiple base classes are specified separated by commas.
For instance:
// Base class Shape
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Base class PaintCost
class PaintCost {
public:
int getCost(int area) {
return area * 70;
}
};
// Derived class Rectangle
class Rectangle: public Shape, public PaintCost {
public:
int getArea() {
return (width * height);
}
};In this exemplary scenario, Rectangle joyfully inherits from both Shape and PaintCost.
Inheritance remains a cornerstone of object-oriented programming, transforming classes into hierarchies and encapsulating the essence of relationships. The techniques and strategies disclosed here pave the way to efficient, robust, and highly organized code._
Subscribe to get the latest posts sent to your email.