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:
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 displaysMammal eats food
.Speak()
function that displaysMammal 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 theMammal
class for theDog
class.Eat()
should displayDog eats meat
.Speak()
should displayDog 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 theCat
class.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.
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