Index
There are five primary types of inheritance in C++. Here’s a brief overview of each type:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid (or Virtual) Inheritance
Single Inheritance
- Definition: A single derived class inherits from a single base class.
- Example: Imagine you have a base class
Animaland a derived classDog. TheDogclass inherits features from theAnimalclass.
Code
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
int main() {
Dog myDog;
myDog.eat(); // Inherited from Animal
myDog.bark(); // Specific to Dog
return 0;
}
Output:
Eating...
Barking...
Explanation:
Step 1: Defining the ‘Animal’ Class:
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
- Class Definition: This defines a class named
Animal.
- Member Function: The class has one public method,
eat(), which when called, prints “Eating…” to the console.
- Access Specifier: The
publickeyword indicates that theeat()method can be accessed from outside the class. This means any object of theAnimalclass, or any class that inherits fromAnimal, can use theeat()method.
Step 2: Defining the ‘Dog’ Class:
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
- Inheritance: The
Dogclass is defined as inheriting from theAnimalclass. This is an example of single inheritance.
-
public Animal: This means that theDogclass inherits all public and protected members (methods and variables) of theAnimalclass. Specifically,Dogcan use theeat()method fromAnimal.
- Member Function: The
Dogclass has its own method,bark(), which prints “Barking…” to the console. - Specialization: The
Dogclass adds new functionality (barking) on top of what it inherits fromAnimal(eating).
Step 3. Main Function: Creating and Using a Dog Object
int main() {
Dog myDog;
myDog.eat(); // Inherited from Animal
myDog.bark(); // Specific to Dog
return 0;
}- Object Creation: In the
main()function, an object of theDogclass namedmyDogis created. - Method Calls:
myDog.eat(): This calls theeat()method, which is inherited from theAnimalclass. It prints “Eating…”.myDog.bark(): This calls thebark()method, which is specific to theDogclass. It prints “Barking…”.
- Execution Flow: The program executes these two methods in sequence, first calling the inherited
eat()method and then thebark()method.
Multiple Inheritance
- Definition: A derived class inherits from more than one base class.
- Example: Suppose you have two base classes
FlyingAnimalandSwimmingAnimal, and a derived classDuckthat inherits from both.
Code
#include <iostream>
using namespace std;;
class FlyingAnimal {
public:
void fly() {
cout << "Flying..." << endl;
}
};
class SwimmingAnimal {
public:
void swim() {
cout << "Swimming..." << endl;
}
};
class Duck : public FlyingAnimal, public SwimmingAnimal {
public:
void quack() {
cout << "Quacking..." << endl;
}
};
int main() {
Duck myDuck;
myDuck.fly(); // Inherited from FlyingAnimal
myDuck.swim(); // Inherited from SwimmingAnimal
myDuck.quack(); // Specific to Duck
return 0;
}
Output:
Flying...
Swimming...
Quacking...
Explanation
Step 1: Class Definitions
class FlyingAnimal {
public:
void fly() {
cout << "Flying..." << endl;
}
};
FlyingAnimalClass:- This is a simple class that represents an animal that can fly.
- It has a single public method
fly()which, when called, outputs"Flying..."to the console.
class SwimmingAnimal {
public:
void swim() {
cout << "Swimming..." << endl;
}
};
SwimmingAnimalClass:- This class represents an animal that can swim.
- It has a public method
swim()which outputs"Swimming..."to the console.
Step 2: Multiple Inheritance
class Duck : public FlyingAnimal, public SwimmingAnimal {
public:
void quack() {
cout << "Quacking..." << endl;
}
};
DuckClass:- This class inherits from both
FlyingAnimalandSwimmingAnimal. - It means that
Duckcan use thefly()method fromFlyingAnimaland theswim()method fromSwimmingAnimal. - In addition to the inherited methods,
Duckhas its own methodquack(), which outputs"Quacking..."to the console.
- This class inherits from both
Step 3: Using the Duck Class
int main() {
Duck myDuck;
myDuck.fly(); // Inherited from FlyingAnimal
myDuck.swim(); // Inherited from SwimmingAnimal
myDuck.quack(); // Specific to Duck
return 0;
}
mainFunction:- An object
myDuckof typeDuckis created. - The
myDuckobject calls thefly()method, which is inherited fromFlyingAnimal. This outputs"Flying...". - The
myDuckobject then calls theswim()method, which is inherited fromSwimmingAnimal. This outputs"Swimming...". - Finally, the
myDuckobject calls its ownquack()method, which outputs"Quacking...".
- An object
Multilevel Inheritance
- Definition: A class is derived from another derived class, creating a chain of inheritance.
- Example: Consider a base class
Animal, a derived classBird, and another derived classParrotthat inherits fromBird.
Code
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Bird : public Animal {
public:
void fly() {
cout << "Flying..." << endl;
}
};
class Parrot : public Bird {
public:
void talk() {
cout << "Talking..." << endl;
}
};
int main() {
Parrot myParrot;
myParrot.eat(); // Inherited from Animal
myParrot.fly(); // Inherited from Bird
myParrot.talk(); // Specific to Parrot
return 0;
}
Output:
Eating...
Flying...
Talking...
Explanation
Step 1: Class Definitions
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
AnimalClass:- This is the base class representing a generic animal.
- It has a single public method
eat(), which, when called, outputs"Eating..."to the console.
class Bird : public Animal {
public:
void fly() {
cout << "Flying..." << endl;
}
};
BirdClass:- This class inherits from the
Animalclass. - It represents a bird, which is a specific type of animal.
- It has a public method
fly(), which outputs"Flying..."to the console. - Since
Birdinherits fromAnimal, it also has access to theeat()method.
- This class inherits from the
class Parrot : public Bird {
public:
void talk() {
cout << "Talking..." << endl;
}
};
ParrotClass:- This class inherits from the
Birdclass. - It represents a parrot, a specific type of bird.
- It has a public method
talk(), which outputs"Talking..."to the console. - Since
Parrotinherits fromBird, it also has access to both thefly()method fromBirdand theeat()method fromAnimal.
- This class inherits from the
Step 2: Using the ‘Parrot’ Class
int main() {
Parrot myParrot;
myParrot.eat(); // Inherited from Animal
myParrot.fly(); // Inherited from Bird
myParrot.talk(); // Specific to Parrot
return 0;
}
mainFunction:- An object
myParrotof typeParrotis created. - The
myParrotobject calls theeat()method, which it inherits fromAnimal. This outputs"Eating...". - The
myParrotobject then calls thefly()method, which it inherits fromBird. This outputs"Flying...". - Finally, the
myParrotobject calls its owntalk()method, which outputs"Talking...".
- An object
Hierarchical Inheritance
- Definition: Multiple derived classes inherit from a single base class.
- Example: You have a base class
Animaland two derived classesDogandCat, both inheriting fromAnimal
Code
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
class Cat : public Animal {
public:
void meow() {
cout << "Meowing..." << endl;
}
};
int main() {
Dog myDog;
Cat myCat;
myDog.eat(); // Inherited from Animal
myDog.bark(); // Specific to Dog
myCat.eat(); // Inherited from Animal
myCat.meow(); // Specific to Cat
return 0;
}
Output:
Eating...
Barking...
Eating...
Meowing...
Explanation
Step 1: Class Definitions
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
AnimalClass:- This is the base class representing a generic animal.
- It has a public method
eat(), which, when called, outputs"Eating..."to the console. - Both
DogandCatwill inherit thiseat()method.
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
DogClass:- This class inherits from the
Animalclass. - It represents a dog, which is a specific type of animal.
- It has a public method
bark(), which outputs"Barking..."to the console. - In addition to its own
bark()method, theDogclass also inherits theeat()method from theAnimalclass.
- This class inherits from the
class Cat : public Animal {
public:
void meow() {
cout << "Meowing..." << endl;
}
};
CatClass:- This class also inherits from the
Animalclass. - It represents a cat, another specific type of animal.
- It has a public method
meow(), which outputs"Meowing..."to the console. - The
Catclass also inherits theeat()method from theAnimalclass.
- This class also inherits from the
Step 2: Using the Dog and Cat Classes
int main() {
Dog myDog;
Cat myCat;
myDog.eat(); // Inherited from Animal
myDog.bark(); // Specific to Dog
myCat.eat(); // Inherited from Animal
myCat.meow(); // Specific to Cat
return 0;
}
mainFunction:- Two objects are created:
myDogof typeDogandmyCatof typeCat. - The
myDogobject calls theeat()method inherited from theAnimalclass, which outputs"Eating...". - The
myDogobject then calls its ownbark()method, which outputs"Barking...". - The
myCatobject also calls theeat()method inherited from theAnimalclass, outputting"Eating...". - The
myCatobject then calls its ownmeow()method, which outputs"Meowing...".
- Two objects are created:
Hybrid (or Virtual) Inheritance
- Definition: A combination of more than one type of inheritance, often involving multiple and multilevel inheritance.
- Example: Suppose you have a base class
Animal, and two derived classesMammalandBird. Then, you create another classBatthat inherits from bothMammalandBird. To avoid ambiguity, virtual inheritance is used.
Code
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Mammal : virtual public Animal {
public:
void giveBirth() {
cout << "Giving birth..." << endl;
}
};
class Bird : virtual public Animal {
public:
void layEggs() {
cout << "Laying eggs..." << endl;
}
};
class Bat : public Mammal, public Bird {
public:
void fly() {
cout << "Flying..." << endl;
}
};
int main() {
Bat myBat;
myBat.eat(); // Inherited from Animal
myBat.giveBirth(); // Inherited from Mammal
myBat.fly(); // Specific to Bat
return 0;
}
Output:
Eating...
Giving birth...
Flying...
Explanation
Step 1: Class Definitions
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
AnimalClass:- This is the base class representing a generic animal.
- It has a public method
eat(), which, when called, outputs"Eating..."to the console. - This method will be accessible to all classes that derive from
Animal.
class Mammal : virtual public Animal {
public:
void giveBirth() {
cout << "Giving birth..." << endl;
}
};
MammalClass:- This class represents mammals and inherits from
Animalusing virtual inheritance. - It has a public method
giveBirth(), which outputs"Giving birth..."to the console. - By using
virtualinheritance, this ensures that only one instance ofAnimalis inherited by any derived classes ofMammal.
- This class represents mammals and inherits from
class Bird : virtual public Animal {
public:
void layEggs() {
cout << "Laying eggs..." << endl;
}
};
BirdClass:- This class represents birds and also inherits from
Animalusing virtual inheritance. - It has a public method
layEggs(), which outputs"Laying eggs..."to the console. - Similar to
Mammal, virtual inheritance ensures thatBirddoesn’t introduce an additionalAnimalinstance.
- This class represents birds and also inherits from
class Bat : public Mammal, public Bird {
public:
void fly() {
cout << "Flying..." << endl;
}
};
BatClass:- This class represents a bat, which is both a mammal and a bird-like creature (in terms of flying ability).
- It inherits from both
MammalandBird. - The
Batclass has its own methodfly(), which outputs"Flying..."to the console. - Since both
MammalandBirdclasses inherit fromAnimalusing virtual inheritance,Batinherits only one instance ofAnimal.
Step 2: Using the Bat Class
int main() {
Bat myBat;
myBat.eat(); // Inherited from Animal
myBat.giveBirth(); // Inherited from Mammal
myBat.fly(); // Specific to Bat
return 0;
}
mainFunction:- An object
myBatof typeBatis created. - The
myBatobject calls theeat()method inherited from theAnimalclass, which outputs"Eating...". - The
myBatobject then calls thegiveBirth()method inherited from theMammalclass, which outputs"Giving birth...". - Finally, the
myBatobject calls its ownfly()method, which outputs"Flying...".
- An object

