Tuesday, August 17, 2010

C++: Exercise: creating a small class

By now you should have understood the basics of creating a class in C++.
Let's put this info into practice:

This exercise is in two parts. The first part is creating an interface file
(.h) for a class, and the second one is creating the implementation (.cpp)
file for this class.

Spend some time coming up with design and implementation of a table class.
The class would describe a typical dining table. The member variables and
functions are:
Member variables:
* Three double variables, representing length, width and height of this
table.
* A string with the manufacturer and a string for uses of this table e.g.
dining. writing desk, etc.
* An integer to store manufacturing date, as well as an integer to store the
sitting capacity of this table.

Member functions:
* A default constructor: Set the width, height and length to zero, use null
string for manufacturer and purpose of this table and set the capacity and
manufacturing date to zero.
* Parameterized constructor 1: passes a string (uses of the table0 and an
int (sitting capacity0 to the object. The length and width must be set to
three times the capacity (in feet), height to 1 (1 meter) and manufacturer
to null string.
* Parameterized constructor 2: Same as first constructor but adds a string
for manufacturer.
* Accessors: Three of them - get_sitting_capacity to return an int,
get_manufacturer to return a string and get_table_uses to return uses of
this table.
* Mutators: three of them - set_manufacturer to set the manufacturer,
set_table-uses to set the uses of this table and set_sitting_capacity to set
how many people can sit on the table.
* Be sure to split the interface and implementation into separate files. In
the header file, include the inclusion guards; in the implementation file,
be sure to add scope resolution operator and include the .h file.

We'll improve the quality of this table class much later when we create more
types of tables on top of a base table (be prepared for inheritence...).
// JL

No comments:

Post a Comment