This document will cover the different types of inheritance in C++ (Single, Multiple, Multilevel),
explaining each concept with illustrative code examples. As requested, I will generate both
DOCX and PDF versions, maintaining anonymity.
Inheritance Types in C++
Introduction to Inheritance
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a
class to inherit properties and behaviors (data members and member functions) from another
class. The class that inherits is called the derived class (or child class), and the class from
which it inherits is called the base class (or parent class).
Key benefits of Inheritance:
● Code Reusability: Members of the base class can be reused by the derived class,
reducing redundant code.
● Polymorphism: It enables runtime polymorphism through virtual functions.
● Extensibility: New functionalities can be added to existing classes without modifying
them.
● "Is-A" Relationship: Inheritance models the "is-a" relationship (e.g., "A Car is a
Vehicle").
Syntax for Inheritance
class DerivedClass : access_specifier BaseClass {
// Members of DerivedClass
};
The access_specifier (e.g., public, protected, private) determines how the protected and public
members of the BaseClass are inherited into the DerivedClass.
● public inheritance: public members of the base class remain public in the derived class,
protected members remain protected. private members are never accessible. This is the
most common and recommended type, modeling the "is-a" relationship directly.
● protected inheritance: public and protected members of the base class become
protected in the derived class.
● private inheritance: public and protected members of the base class become private in
the derived class.
Types of Inheritance
There are several types of inheritance in C++:
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance (one base class, multiple derived classes) - Not explicitly covered
here, but conceptually simple.
5. Hybrid Inheritance (combination of multiple types) - Not explicitly covered here.
1. Single Inheritance
, Single inheritance involves one derived class inheriting from a single base class. It is the
simplest and most common form of inheritance.
Diagram:
Base Class
↑
|
Derived Class
Example Program: Single Inheritance (Animal -> Dog)
#include <iostream>
#include <string>
// Base Class
class Animal {
public:
std::string species;
int age;
Animal(std::string s, int a) : species(s), age(a) {
std::cout << "Animal Constructor Called." << std::endl;
}
void eat() {
std::cout << species << " is eating." << std::endl;
}
void sleep() {
std::cout << species << " is sleeping." << std::endl;
}
};
// Derived Class (inherits publicly from Animal)
class Dog : public Animal {
public:
std::string breed;
// Dog's constructor calls Animal's constructor
Dog(std::string b, int a, std::string sp = "Dog") : Animal(sp, a),
breed(b) {
std::cout << "Dog Constructor Called." << std::endl;
}
void bark() {
std::cout << species << " the " << breed << " is barking:
Woof! Woof!" << std::endl;
}
};
int main() {
// Create an object of the derived class
Dog myDog("Golden Retriever", 3);