Type of Inheritance

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 class Dog. The Dog class inherits features from the Animal 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 the eat() method can be accessed from outside the class. This means any object of the Animal class, or any class that inherits from Animal, can use the eat() 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 the Animal class. This is an example of single inheritance.
  • public Animal: This means that the Dog class inherits all public and protected members (methods and variables) of the Animal class. Specifically, Dog can use the eat() method from Animal.
  • 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 from Animal (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 the Dog class named myDog is created.
  • Method Calls:
  • myDog.eat(): This calls the eat() method, which is inherited from the Animal class. It prints “Eating…”.
  • myDog.bark(): This calls the bark() method, which is specific to the Dog class. It prints “Barking…”.
  • Execution Flow: The program executes these two methods in sequence, first calling the inherited eat() method and then the bark() method.

Multiple Inheritance

  • Definition: A derived class inherits from more than one base class.
  • Example: Suppose you have two base classes FlyingAnimal and SwimmingAnimal, and a derived class Duck 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 and SwimmingAnimal.
    • It means that Duck can use the fly() method from FlyingAnimal and the swim() method from SwimmingAnimal.
    • In addition to the inherited methods, Duck has its own method quack(), which outputs "Quacking..." to the console.

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 type Duck is created.
    • The myDuck object calls the fly() method, which is inherited from FlyingAnimal. This outputs "Flying...".
    • The myDuck object then calls the swim() method, which is inherited from SwimmingAnimal. This outputs "Swimming...".
    • Finally, the myDuck object calls its own quack() method, which outputs "Quacking...".

Multilevel Inheritance

  • Definition: A class is derived from another derived class, creating a chain of inheritance.
  • Example: Consider a base class Animal, a derived class Bird, and another derived class Parrot that inherits from Bird.

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 from Animal, it also has access to the eat() method.
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 from Bird, it also has access to both the fly() method from Bird and the eat() method from Animal.

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 type Parrot is created.
    • The myParrot object calls the eat() method, which it inherits from Animal. This outputs "Eating...".
    • The myParrot object then calls the fly() method, which it inherits from Bird. This outputs "Flying...".
    • Finally, the myParrot object calls its own talk() method, which outputs "Talking...".

Hierarchical Inheritance

  • Definition: Multiple derived classes inherit from a single base class.
  • Example: You have a base class Animal and two derived classes Dog and Cat, both inheriting from Animal

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 and Cat will inherit this eat() 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, the Dog class also inherits the eat() method from the Animal class.
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 the eat() method from the Animal class.

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 type Dog and myCat of type Cat.
    • The myDog object calls the eat() method inherited from the Animal class, which outputs "Eating...".
    • The myDog object then calls its own bark() method, which outputs "Barking...".
    • The myCat object also calls the eat() method inherited from the Animal class, outputting "Eating...".
    • The myCat object then calls its own meow() method, which outputs "Meowing...".

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 classes Mammal and Bird. Then, you create another class Bat that inherits from both Mammal and Bird. 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 of Animal is inherited by any derived classes of Mammal.
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 that Bird doesn’t introduce an additional Animal instance.
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 and Bird.
    • The Bat class has its own method fly(), which outputs "Flying..." to the console.
    • Since both Mammal and Bird classes inherit from Animal using virtual inheritance, Bat inherits only one instance of Animal.

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 type Bat is created.
    • The myBat object calls the eat() method inherited from the Animal class, which outputs "Eating...".
    • The myBat object then calls the giveBirth() method inherited from the Mammal class, which outputs "Giving birth...".
    • Finally, the myBat object calls its own fly() method, which outputs "Flying...".

    Leave a Reply

    Your email address will not be published.

    Need Help?