Exercise 3: Displaying Message Using Virtual Functions

Display information about two base classes by using virtual functions.

Problem statement

We will first build three classes: Mammal (parent class), Dog (derived class) and Cat (derived class). The Dog and Cat class will inherit from Mammal.

In the exercise, implement these classes with the following characteristics:

  • Mammal class:

    • Has one protected variable for the age of the mammal.

    • A constructor that takes the age of a mammal as input and sets it.

    • The function Eat() that displays Mammal eats food.

    • Speak() function that displays Mammal speaks mammalian!!.

    • get_Age() function that returns the age of the mammal.

  • Dog class:

    • Inherits all the members from the Mammal class.

    • Implement all member functions of the Mammal class for the Dog class.

    • Eat() should display Dog eats meat.

    • Speak() should display Dog barks: ruff! ruff!.

    • get_Age() should return the dog’s age.

  • Cat class:

    • Inherits all the members from the Mammal class.

    • Implement all member functions of the Mammal class for the Cat class.

    • Eat() should display Cat eats meat.

    • Speak() should display Cat meows: Meow! Meow!.

    • get_Age() should return the cat’s age.

If you don’t know how to do this, click the “Show Hint” button.

Create a free account to access the full course.

Continue your learning journey with a 14-day free trial.

By signing up, you agree to Educative's Terms of Service and Privacy Policy