Exercise 3: Displaying Message Using Inheritance
This exercise requires you to implement the concepts of polymorphism/inheritance to display information about two base classes.
We'll cover the following
Problem Statement
The code below has:
-
A parent class named
Animal
.- Inside it define:
name
(protected
)age
(protected
)set_data(int a,string b)
function (public
):- takes
age
andname
parameters and sets them to given values.
- takes
- Inside it define:
-
Then there are two derived classes
Zebra
Dolphin
-
The derived classes should
- Have a function that returns a string containing a message telling the
age
and thename
as well as information about place of origin of that animal.- Here’s a link showing how you can add values to a string.
- Hint: You have to create two separate message functions for both the derived classes.
- Have a function that returns a string containing a message telling the
Example
Input:
name
ofZebra
is set to Ana and theage
is set to 5name
ofDolphin
is set to Jin and theage
is set to 2
Then:
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy