Challenge 2: Displaying Message Using Inheritance
In this challenge, can you extend a parent class with one function overridden for each child?
We'll cover the following
Problem statement
Whenever you visit a zoo, there are many types of animals in it. However, for each animal some things never change- they all have a name and an age. Also a country of origin. This time, we want you to use the concept of classes and inheritance to solve the exercise below!
Coding exercise
The code below has:
-
A parent class named
Animal
.- Inside it define:
name
age
set_data(int a,string b)
:- sets
age
andname
parameters and to the given values.
- sets
- Inside it define:
-
Then there are two derived classes
Zebra
Dolphin
-
The derived classes should
- Return a string containing a message telling the
age
and thename
as well as information about place of origin of that animal.- Hint: You have to create two separate message functions for both the base classes.
- Hint: The origin of Zebra is Africa, and the origin of Dolphin is New Zealand.
- Return a string containing a message telling the
Example
-
The
animal_type
namedname
isage
years old. Theanimal_type
comes fromorigin
. -
If we have an animal of class
Zebra
, whose name isZ
with age,2
and the country nameAfrica
. The output should be as follows:
The zebra named Z is 2 years old. The zebra comes from Africa
Get hands-on with 1400+ tech skills courses.