Exercise 3: Displaying Message Using Virtual Functions
Display information about two base classes by using virtual functions.
We'll cover the following...
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:
Mammalclass:Has one
protectedvariable for the age of the mammal.A constructor that takes the age of a mammal as input and sets it.
The function
Eat()that displaysMammal eats food.Speak()function that displaysMammal speaks mammalian!!.get_Age()function that returns the age of the mammal.
Dogclass:Inherits all the members from the
Mammalclass.Implement all
memberfunctions of theMammalclass for theDogclass.Eat()should displayDog eats meat.Speak()should displayDog barks: ruff! ruff!.get_Age()should return the dog’s age.
Catclass:Inherits all the members from the
Mammalclass.Implement all member functions of the
Mammalclass for theCatclass.Eat()should displayCat eats meat.Speak()should displayCat 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.