Exercise: Class and Object
Class: a collection of a fixed number of components. The components of a class
are called members of the class.
Purpose and Objective: To enhance readability and programmer productivity by
describing and grouping data and functions that correspond to naturally
occurring objects and categories.
The general form or syntax for a class is:
class className {
class Variables and functions
};
1. Declare a class named Item to include three private member variables:
id (integer), name (string), and price (floating point).
2. Define a public constructor for Item:
Have formal parameters that include default values and a statement body with
assignments for actual values passed for formal parameters to give to the
Item member variables.
3. Create public accessor (getter) and mutator (setter) methods for each of
the member variables.
4. Test Item class:
a. Instantiate i, an Item object with default values provided by the
constructor.
b. Print the property values for object i. Use the accessor methods
associated with this object.
c. Call the setters for id, name and price, pass literals as actual
arguments, then print the values for object i.
d. Instantiate i2, another Item object with actual parameter values 12016,
"Garlic Press", 13.95, then print. [Optionally, the object i could be
explicitly destructed before creating another instance.]