C++ example from 23rd September 1999
This defines a very very basic version of the classes legalentity,
person, and corporation, as discussed in class. There are 7 files
which may be downloaded and compiled and run without alteration, except
that you will have to change their names: XXXh.txt should be chaned to
XXX.h, XXXcpp.txt should be changed to XXX.cpp.
This shows the required
steps for compilation, linking, and execution. It assumes that the 7 files
are put in their own subdirectory, whcih they do not share with any other
applications.
$ CC -c legalentity.cpp
$ CC -c person.cpp
$ CC -c corporation.cpp
$ CC -c main.cpp
$ CC *.o -o run
$ run
Things to consider:
How can critical data be properly protected? For example, the numemployees
membry of corporation is a protected int; the only way it can be accessed
is through the public method getnumemployees(), which does not allow users
to change the member at all. If we had an array member (perhaps an array
of ints) and an access method called getarray() that returns a pointer to
the array, the protection would be ruined; users could simply follow the
pointer and change the elements of the protected member. What happens
when we have a protected string member and a public get method that
returns the string? Do we still have effective protection or not?
How safe are the getpresident and getemployee methods of corporation?
What would be a better way to store the employees of a corporation than
a fixed length array?
The printall method of corporation is not very flexible. How could we
arrange for the normal << operator of C++ to be able to print
our objects automatically?
It is quite likely that somebody running a program that uses these classes
would want to search for a particular person or corporation if they
know its name. How could such a feature be added?
We can see from the various tostring methods that the C++ library class
called string has defined its own version of the + operator, letting us
use + to append two strings. How could we do things like that? Would it be
a good idea to do so?
The employ method of corporation just kills the program if something goes
wrong (you try to add a 101st employee to the array of 100). What are
the appropriate ways of handling an error condition in a running program?