|
Exercises
Notice: If you have background in C and I haven't scared you off yet, these exercises will seem trivial to you. Please do them anyway. Get used to the new way of thinking and structuring your problems. All the exercises should be solved using only the constructs introduced so far. You may compare your solutions with my solutions.
- Write a program using class World, whose output is "Hello from 1! Good bye from 1. Hello from 2! Good bye from 2." Find at least two different solutions.
- Find a programmer's error in this class definition. Give two different ways of correcting it.
class Glove
{
public:
Glove (int cFingers)
: _n (cFingers), _hand (_n)
{
cout << "Glove with " << _n << " fingers\n";
}
private:
Hand _hand;
int _n;
};
|
- Define class HorBar whose constructor prints the following pattern
where the number of minus signs is passed as an int argument to the constructor. Similarly class VerBar should print a vertical bar of height n, like this:
Define class Frame that contains (embedded) the upper horizontal bar, the vertical bar and the lower horizontal bar. Its constructor should take two arguments, the horizontal and the vertical extent of the frame
The Frame object can be defined and initialized in the program using the following syntax:
- Define class Ladder that contains two frames and a VerBar between them. The constructor should take two arguments, one for the horizontal extent and one for the vertical distance between horizontal bars. It should print the following pattern when created with (5, 2):
+-----+
|
|
+-----+
|
|
+-----+
|
|
+-----+
|
- Write a program that asks for a number and calculates its factorial. Factorial of n is the product of all positive integers up (and including n. n! = 1 * 2 * 3 * ... * n . To multiply two numbers put an asterisk, *, between them.
- Define class Planet that inherits from CelestialBody and is characterized by an albedo (how reflective its surface is--use a double to store it).
- In the following class definition, replace dummy strings with actual words in such a way that during the construction of the object, the string "program makes objects with class" is printed
class One: public Two
{
public:
One ()
{
Three three;
cout << "dummy ";
}
private:
Four _four;
};
|
Each of the classes Two, Three, and Four prints some dummy string in its constructor.
Design a similar class which, during construction, would print the above string, and during destruction print the same words in reverse order. The constraint is that each class has to have exactly the same code (print the same string) in its constructor and in its destructor.
- Design, implement and test class Average. An object of type Average should accept values that are put into it. At any point in time it should be possible to retrieve their cumulative arithmetic average.
- Design a class hierarchy (chain of inheritance) that derives a Human from a LivingBeing. Make sure you use the is-a relationship. Now add branches for a Tomato and an Elephant. Write a little test program that defines one of each inside main. Each class should have a constructor that prints a short but pointed message.
- Design a Human exploring the has-a relationship. A human has a head, the head has a nose, the nose has a nose hair, etc. Derive a Man and a Woman. Implement simple classes whose constructors print messages and create a couple of Humans.
|