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
Animal
and a derived classDog
. TheDog
class inherits features from theAnimal
class.
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
public
keyword indicates that theeat()
method can be accessed from outside the class. This means any object of theAnimal
class, 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
Dog
class is defined as inheriting from theAnimal
class. This is an example of single inheritance.
-
public Animal
: This means that theDog
class inherits all public and protected members (methods and variables) of theAnimal
class. Specifically,Dog
can use theeat()
method fromAnimal
.
- Member Function: The
Dog
class has its own method,bark()
, which prints “Barking…” to the console. - Specialization: The
Dog
class 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 theDog
class namedmyDog
is created. - Method Calls:
myDog.eat()
: This calls theeat()
method, which is inherited from theAnimal
class. It prints “Eating…”.myDog.bark()
: This calls thebark()
method, which is specific to theDog
class. 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
FlyingAnimal
andSwimmingAnimal
, and a derived classDuck
that 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;
}
};
FlyingAnimal
Class:- 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;
}
};
SwimmingAnimal
Class:- 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;
}
};
Duck
Class:- This class inherits from both
FlyingAnimal
andSwimmingAnimal
. - It means that
Duck
can use thefly()
method fromFlyingAnimal
and theswim()
method fromSwimmingAnimal
. - In addition to the inherited methods,
Duck
has 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;
}
main
Function:- An object
myDuck
of typeDuck
is created. - The
myDuck
object calls thefly()
method, which is inherited fromFlyingAnimal
. This outputs"Flying..."
. - The
myDuck
object then calls theswim()
method, which is inherited fromSwimmingAnimal
. This outputs"Swimming..."
. - Finally, the
myDuck
object 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 classParrot
that 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;
}
};
Animal
Class:- 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;
}
};
Bird
Class:- This class inherits from the
Animal
class. - It represents a bird, which is a specific type of animal.
- It has a public method
fly()
, which outputs"Flying..."
to the console. - Since
Bird
inherits fromAnimal
, it also has access to theeat()
method.
- This class inherits from the
class Parrot : public Bird {
public:
void talk() {
cout << "Talking..." << endl;
}
};
Parrot
Class:- This class inherits from the
Bird
class. - It represents a parrot, a specific type of bird.
- It has a public method
talk()
, which outputs"Talking..."
to the console. - Since
Parrot
inherits fromBird
, it also has access to both thefly()
method fromBird
and 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;
}
main
Function:- An object
myParrot
of typeParrot
is created. - The
myParrot
object calls theeat()
method, which it inherits fromAnimal
. This outputs"Eating..."
. - The
myParrot
object then calls thefly()
method, which it inherits fromBird
. This outputs"Flying..."
. - Finally, the
myParrot
object 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
Animal
and two derived classesDog
andCat
, 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;
}
};
Animal
Class:- This is the base class representing a generic animal.
- It has a public method
eat()
, which, when called, outputs"Eating..."
to the console. - Both
Dog
andCat
will inherit thiseat()
method.
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
Dog
Class:- This class inherits from the
Animal
class. - 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, theDog
class also inherits theeat()
method from theAnimal
class.
- This class inherits from the
class Cat : public Animal {
public:
void meow() {
cout << "Meowing..." << endl;
}
};
Cat
Class:- This class also inherits from the
Animal
class. - It represents a cat, another specific type of animal.
- It has a public method
meow()
, which outputs"Meowing..."
to the console. - The
Cat
class also inherits theeat()
method from theAnimal
class.
- 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;
}
main
Function:- Two objects are created:
myDog
of typeDog
andmyCat
of typeCat
. - The
myDog
object calls theeat()
method inherited from theAnimal
class, which outputs"Eating..."
. - The
myDog
object then calls its ownbark()
method, which outputs"Barking..."
. - The
myCat
object also calls theeat()
method inherited from theAnimal
class, outputting"Eating..."
. - The
myCat
object 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 classesMammal
andBird
. Then, you create another classBat
that inherits from bothMammal
andBird
. 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;
}
};
Animal
Class:- 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;
}
};
Mammal
Class:- This class represents mammals and inherits from
Animal
using virtual inheritance. - It has a public method
giveBirth()
, which outputs"Giving birth..."
to the console. - By using
virtual
inheritance, this ensures that only one instance ofAnimal
is 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;
}
};
Bird
Class:- This class represents birds and also inherits from
Animal
using virtual inheritance. - It has a public method
layEggs()
, which outputs"Laying eggs..."
to the console. - Similar to
Mammal
, virtual inheritance ensures thatBird
doesn’t introduce an additionalAnimal
instance.
- This class represents birds and also inherits from
class Bat : public Mammal, public Bird {
public:
void fly() {
cout << "Flying..." << endl;
}
};
Bat
Class:- This class represents a bat, which is both a mammal and a bird-like creature (in terms of flying ability).
- It inherits from both
Mammal
andBird
. - The
Bat
class has its own methodfly()
, which outputs"Flying..."
to the console. - Since both
Mammal
andBird
classes inherit fromAnimal
using virtual inheritance,Bat
inherits 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;
}
main
Function:- An object
myBat
of typeBat
is created. - The
myBat
object calls theeat()
method inherited from theAnimal
class, which outputs"Eating..."
. - The
myBat
object then calls thegiveBirth()
method inherited from theMammal
class, which outputs"Giving birth..."
. - Finally, the
myBat
object calls its ownfly()
method, which outputs"Flying..."
.
- An object