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 and name parameters and sets them to given values.
  • 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 the name 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.

Example

Input:

  • name of Zebra is set to Ana and the age is set to 5
  • name of Dolphin is set to Jin and the age 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