015-003-004
Downcast: Type Conversion
Medium
Problem Description
Downcast: Type Conversion
In this problem, you will create a program that reads a dog's name from standard input, stores a Dog object in an Animal type variable, converts it to Dog type using a downcast, and calls a Dog-specific method to display the result to standard output.
Learning Objective: Learn type conversion from parent to child class (downcast)
Overview
Downcast converts a parent class type variable to child class type. It only succeeds when the actual object is of the child class.
Specifications
- Read a dog's name (string) from one line of standard input
- The Dog class accepts a name in its constructor and stores it in the
namefield - Store Dog object in Animal type variable
- Downcast to Dog type
- Call Dog-specific method
bark()
Input Format
[dog name]
Output Format
[name] says: Woof!
Example
Input: Rex
Output: Rex says: Woof!
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up