Description
3.1 Introduction to Inheritance
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a new class (the derived or child class) to inherit properties (data members) and behaviors (member functions) from an existing class (the base or parent class). The primary purpose of inheritance is to promote code reuse and establish a “is-a” relationship between classes (e.g., a “Car” is a “Vehicle”).
Defining a Derived Class A derived class is defined using the : operator, followed by the visibility mode and the name of the base class. The syntax is as follows:
C++
class DerivedClass : visibility_mode BaseClass {
// members of the derived class
};
For example, class Dog : public Animal {}; would define Dog as a derived class of Animal, with public inheritance.
Visibility Modes and Effects Visibility modes control how the members of the base class are accessed by the derived class and by objects outside the class hierarchy.
3.2 Types of Inheritance
- Single Inheritance: A derived class inherits from only one base class. This is the simplest form of inheritance.
- Multilevel Inheritance: A derived class inherits from a base class, which itself is a derived class of another base class. This creates a chain of inheritance. For example,
Class Cinherits fromClass B, andClass Binherits fromClass A. - Multiple Inheritance: A derived class inherits from two or more base classes. This allows a class to combine features from multiple unrelated classes.
- Hierarchical Inheritance: Multiple derived classes inherit from a single base class. This is useful for creating a family of classes with common features.
- Hybrid Inheritance: A combination of two or more types of inheritance. This often involves combining single, multiple, hierarchical, or multilevel inheritance to model complex relationships.
3.3 Virtual Base Class, Abstract Class, and Constructors
Virtual Base Class In multiple inheritance, a common issue is the “diamond problem,” where a derived class inherits from two classes that share a common ancestor. This leads to the derived class having multiple copies of the ancestor’s members. A virtual base class is used to solve this problem by ensuring that only one copy of the common base class’s members is inherited. The virtual keyword is used during inheritance.
Abstract Class An abstract class is a class that cannot be instantiated on its own. It is designed to be a base class for other classes. It must contain at least one pure virtual function (a virtual function with = 0; in its declaration). Derived classes must implement these pure virtual functions; otherwise, they also become abstract.
Constructors in a Derived Class When a derived class object is created, the constructors of both the base class and the derived class are called. The order of execution is:
- Base class constructors are executed first.
- Then, the derived class constructor is executed.
The derived class constructor is responsible for passing arguments to the base class constructor. This is done using an initialization list in the derived class constructor’s definition.
C++
DerivedClass::DerivedClass(int x) : BaseClass(x) {
// Derived class constructor body
}
This ensures that the base class part of the object is properly initialized before the derived class part.
Keywords of this Topic: class, public, protected, private, virtual, abstract, inheritance, base class, derived class.





Reviews
There are no reviews yet.