Suppose that we wish to design a rectangle object. We must decide 1) what data should be included in this object and 2) what functions are needed for this object. The data needed for this object should include a rectangle's dimensions: width and height of the rectangle. Some of the functions needed might include: a function to print a rectangle, a function to compute the area of the rectangle, a function to compute and print the perimeter of a rectangle, a function to draw a rectangle and a function to allow the user to set the length and width of the rectangle. Now we are ready to construct the class. The basic syntax is:
For example, the declaration of the rectangle class may appear as follows:
The Rectangle class has seven members - five member functions (Two Constructors,
getArea, getPerimeter and setSize) and two member variables
(length and width). The member variables form the data that is
needed to create a rectangle and the member functions allow the user to perform
different operations with a rectangle. Note that in this example, clients (users)
of this class have access to the member functions but do not have access to
the member variables.