Chapter
7:
3. PAL Problem:
Make
a little database of your friends by creating a class called Pal. Each of your pals will have the following
(private) attributes: real name, nick
name, age, and location. All “pals” also
have a common characteristic—buddyCount—which keeps tracks of how many pals you
have. Each friend’s location will be an
object of another class—Address--and each “location,” in turn, has the
following attributes: street name and
city name. Here are the functions your
Pal class should have: a constructor
that takes four strings and one integer, printName, printAge, printAddress,
setAge, and a destructor. All the print
and set functions should be set up so that cascading is possible. The constructor should greet your new friend
and increment the counter. The destructor
should do just the opposite. Your
Address functions will be: a constructor
that takes two strings, a destructor, and a print function (which will be
called when printAddress is called).
Make the constructor and destructor of Address print some message just
so you can see the order in which objects are created and destroyed. In the main you should create some buddies in
two different ways: the normal way of instantiating an object of a class; and
using the new operator to create a pointer to a brand new Pal object. For the objects created in each way, call
several print/set functions in a cascaded fashion. Before main ends you should delete the Pal
pointers you created.