You are on page 1of 3

OOP Final Test

Section I (3p)

1. Code a class to represent a Pet. Add a public string field to store the name of the pet. Add
a protected constructor that sets the name.

2. Derive a Cat class from Pet. Add a public bool field which stores whether or not the cat is
picky. Provide a public constructor that sets the name and the pickiness of the cat. Call the
Pet constructor to set the name rather than assigning to the protected field directly.

3. Derive a Dog class from Pet. Add a public bool field which stores whether or not the dog
can fetch. Provide a public constructor that sets the name and the fetching ability of the dog.
Call the Pet constructor to set the name rather than assigning to the protected field directly.

4. Add a main method that creates a cat and a dog.

5. Avoid memory leaks.

Section II (1p)
1. Modify the main method to use Pet references to refer to the Dog and Cat. Attempt to access
the members through the Pet references. What do you see? (comment in your source code)

Section III (1p)

1
1. Add a virtual Move method to the Pet class. Move should take a bool that indicates
whether the pet should move fast or slow. Print a message that indicates the name and
behavior: pets walk when moving slow and run when moving fast.

2. Override the Move method in the Cat class. Change the behavior from the generic walking
and running to something more catlike: cats pounce when moving fast and slink when
moving slow.

3. Override the Move method in the Dog class. Change the behavior from the generic walking
and running to something more doglike: dogs bound when moving fast and stride when
moving slow.

Section IV (1p)
1. Add an pure virtual Speak method to the Pet class. There is no reasonable
implementation for the Speak method that would apply to all pets.

2. Override the Speak method in the Cat class. Print the pets name and the string "meows".

3. Override the Speak method in the Dog class. Print the pets name and the string "barks".

Section V (3p)
1. Add a template method named Exercise. The method should take the pet that needs the
workout and an integer to indicate the duration. The pet should Speak before the workout
and then Move the number of times indicated by the duration. Call Exercise from main
passing first a Cat and then a Dog. Verify that the calls to Speak and Move are being
dynamically bound.

2. Add another version of Exercise that takes an array of Pet references. Loop through the
array and have each pet Speak and Move once. In main, create an array of Pet references
an fill it with some cats and some dogs. Pass the array to Exercise and verify that the calls
to Speak and Move are being dynamically bound.

Program correct compilation (1p)

2
Extra points (2p)
Implement a class BitSet which helps us use a set of positive numbers smaller than a value n. In
order to store the numbers we use an array of integers. Each bit of each component indicates if a
corresponding number is in the set or not.

Example:

For n=37 we need a vector of two integers. The bits of the first component indicates if the numbers
from 0 to 31 are in the set or not. The first 5 bits of the second component indicates if the numbers
from 32 to 36 are in the set or not.

For this class overload the operators: + (reunion and add element), < (in, included). Test each of
the operators in a main() method.

You might also like