0% found this document useful (0 votes)
15 views7 pages

Inheritance - Hirerichal and Hybrid

Uploaded by

rn9685917
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Inheritance - Hirerichal and Hybrid

Uploaded by

rn9685917
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Hierarchal:

Create a base class called "Animal" with a constructor that takes a name
as a parameter and an "eat()" method that displays "{name} is eating."

Create a derived class called "Pet" that inherits from "Animal" and has a
constructor taking a name as a parameter. It should also have a "play()"
method that displays "{name} is playing."

Create derived classes "Dog" and "Cat" that inherit from "Pet." Both
classes should have a constructor taking a name as a parameter. "Dog"
should have a "bark()" method displaying "{name} is barking!" and "Cat"
should have a "meow()" method displaying "{name} says meow!"

In the "main" function, create objects of "Dog" and "Cat" with


appropriate names. Call "eat()" and "play()" on each object, and call
"bark()" on the "Dog" object and "meow()" on the "Cat" object to display
their respective behaviors.
return 0;

Hybrid: Demonstrate the concept of


hybrid inheritance in C++.
a base class Animal
two derived classes Mammal and Bird,
each representing a specific type of
animal.
The Platypus class is then derived from
both Mammal and Bird, representing a
unique animal that inherits
characteristics from both mammal and
bird categories.
#include <iostream>

using namespace std;

// Base class

class Animal {

public:

void displayType() {

cout << "Animal" << endl;

};

// Derived class 1

class Mammal : public Animal {

public:

void displayType() {

cout << "Mammal" << endl;

};

// Derived class 2

class Bird : public Animal {


public:

void displayType() {

cout << "Bird" << endl;

};

// Derived class 3 inheriting from both Mammal and Bird

class Platypus : public Mammal, public Bird {

public:

void displayType() {

cout << "Platypus" << endl;

};

int main() {

Platypus platypus;

platypus.displayType(); // Output: Platypus

platypus.Mammal::displayType(); // Output: Mammal

platypus.Bird::displayType(); // Output: Bird

return 0;

You might also like