C++ Inheritance
The mechanism of inheritance in C++ enables a class to take on the traits and characteristics of another class. Whereas the class that is inherited is known as the base class or superclass, the class that inherits is known as the derived class or subclass. A key idea in object-oriented programming is inheritance, which encourages code reuse and makes it possible to write code that is more adaptable and scalable.
Syntax:
class DerivedClass : accessSpecifier BaseClass {
// derived class members
};
With this syntax, BaseClass is the name of the superclass being inherited, DerivedClass is the name of the subclass, accessSpecifier is the access specifier that determines how visible base class members are in the derived class, and DerivedClass is the name of the subclass.
Single Inheritance:
The most frequent type of inheritance is single inheritance, in which a new class is derived from a single base class. The derived class receives all of the base class’s properties under this type of inheritance.
Syntax:
class Base {
// class members
};
class Derived : access_specifier Base {
// class members
};
Example:
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
int main() {
Dog d;
d.eat(); // inherited from Animal class
d.bark(); // defined in Dog class
return 0;
}
Multiple Inheritance:
An example of multiple inheritance is when a new class derives from multiple base classes. The derived class receives all of the base classes’ properties under this type of inheritance.
Syntax:
class Base1 {
// class members
};
class Base2 {
// class members
};
class Derived : access_specifier Base1, access_specifier Base2 {
// class members
};
Example:
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Vehicle {
public:
void drive() {
cout << "Driving..." << endl;
}
};
class DogCar : public Animal, public Vehicle {
public:
void bark() {
cout << "Barking..." << endl;
}
};
int main() {
DogCar dc;
dc.eat(); // inherited from Animal class
dc.drive(); // inherited from Vehicle class
dc.bark(); // defined in DogCar class
return 0;
}
Multilevel Inheritance:
Multilevel inheritance is a type of inheritance where a derived class is created from another derived class.
Syntax:
class Base {
// class members
};
class Derived1 : access_specifier Base {
// class members
};
class Derived2 : access_specifier Derived1 {
// class members
};
Example:
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
class BullDog : public Dog {
public:
void bite() {
cout << "Biting..." << endl;
}
};
int main() {
BullDog bd;
bd.eat(); // inherited from Animal class
bd.bark(); // inherited from Dog class
bd.bite(); // defined in BullDog class
return 0;
}
Hierarchical Inheritance:
Hierarchical inheritance is a type of inheritance where two or more derived classes are created from a single base class.
Syntax:
class Base {
// class members
};
class Derived1 : access_specifier Base {
// class members
};
class Derived2 : access_specifier Base {
// class members
};
Example:
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
class Cat : public Animal {