Exercise 3: Displaying Message Using Virtual Functions
Let's 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, you have to implement these classes with the following characteristics:
Mammal
class:Has one
protected
variable for age of the mammal.A constructor that takes the age of mammal as input and sets it.
The function
Eat()
that displays “Mammal eats food”.Function
Speak()
that displays “Mammal speaks mammalian!!”.Function
get_Age()
which returns the age of the mammal.
Dog
class:Inherits all the members from
Mammal
class.Implement all
member
functions ofMammal
class forDog
class.Eat()
should display “Dog eats meat”.Speak()
should display “Dog barks: ruff! ruff!”.get_Age()
should return Dog’s age.
Cat
class:Inherits all the members from
Mammal
class.Implement all member functions of
Mammal
class forCat
class.Eat()
should display “Cat eats meat”.Speak()
should display “Cat meows: Meow! Meow!”.get_Age()
should return Cat’s age.
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy