Skip to main content

Exercises 19.15 Exercises

1.

You are given the code for the Animal class. Declare a class Dog that is a subclass of Animal. The Doc class should override the getTypeString function so that it returns "Canine" instead of "Animal".

2.

You are given the code for the Animal class. Declare a class Dog that is a subclass of Animal. It does not need any new member variables. Make a constructor for Dog that takes no parameters, but sets numLegs to 4 by calling the Animal constructor in its initialization list.

3.

Write a class ThreeDPoint that inherits from TwoDPoint and adds a z coordinate. You should not use composition or create your own x and y variables. ThreeDPoint should only need to store a z value.
You will need to implement a constructor that takes three ints (x, y, z). Do not worry about overriding print.

4.

Copy your code from ExerciseΒ 19.15.3 - you will need it for this problem.
Then add code to override toString in ThreeDPoint. Printing a ThreeDPoint should print the x, y, z coordinates of the point.
Hint.
toString

5.

Write a class VerySilly that extends Silly. It should store a double value in addition to the information stored by Silly. It should have:
  • a constructor: VerySilly(int, string, double) that sets x and s from Silly to be the int and string and stores the double itself.
  • a function toString that gets the result of Silly::toString() and then adds on a space and the the string representation of the stored double.
Examine Silly closely. You have protected access to one of its variables. The other you will have to set via the parent constructor.

6.

Make an abstract class Number that declares an abstract function getValue() that allows the rest of the code to work correctly. (You won’t need to implement any variables or functions in Number, so technically it will be an interface.)
Hint.
const

7.

Copy your code from ExerciseΒ 19.15.6 - you will need it for this problem.
Then add a global function (not a member function of your class) double getDoubled that accepts a Number reference and returns double the value of the Number. The function should work for any class that extends Number, such as Integer or Double.
You have attempted of activities on this page.