CS 102 (Fall '03)
[Schedule]
[Examples] [Programs]
[Notes & Reference]
[Syllabus] [Lab
& TA] [Tests]
[Grades]
A class is like a struct, but a class normally includes member functions as well as member variables. We use the class keyword rather than the struct keyword.Take a look at program 6.3 from our text, illustrating a class to implement the day of the year. In particular note:
The public keyword (default is private)- Including not only member data, but a member function output( ) as well.
- Accessing class data members using the dot notation, as we did with structures.
- Calling class member functions using the dot notation
- The class scope resolution operator :: used with member function output( )
- Calling the output( ) member function from the perspective of two different days. (It is critical that you understand this!)
Since we're not sure who might reuse this DayOfYear class, it is a good idea to enforce certain standards for its use. We can do this by limiting access to the data members. To retrieve or change their values, a user must use functions that are included as part of the class. This approach is known as information hiding, or encapsulation.
This is illustrated in program 6.4 from our text, illustrating a class where the data members are now private rather than public as in the previous program. Note the following:
- data members ( month and day) are now private instead of public
- We have introduced functions to get and set the data member values. Recall our discussion of function overloading, where the argument signature determines which version of a function to call. This is illustrated in having two different versions of the set( ) function.
- Private data members can be used in member functions for this class, but not elsewhere.
The get and set functions are typical, and also called accessor and mutator functions, respectively.
To summarize, a class consists of:
class className
{
public:
public members, i.e. the set & get functions, public data members
...
private:
private members, i.e. private data members, functions for use only within this class
...
};// Each class function would have a definition such as that shown below
returnType className::classFunctionName()
{
...
}
A constructor is a special member function used to initialize a class object. For example, in the DayOfYear class in this program we might have:
class DayOfYear { public: DayOfYear(int monthValue, int dayValue); //Initializes the month and day to arguments. DayOfYear(int monthValue); //Initializes the date to the first of the given month. DayOfYear( ); //Initializes the date to January 1. void input( ); ... private: int month; int day; void testDate( ); // a private member function };where we see three constructors, each with a different number of arguments. A constructor must have the same name as the class and cannot return a value. Above we see part of the interface for the DayOfYear class. Here is the implementation for the first constructor:
void DayOfYear::testDate( )
{
if ((month < 1) || (month > 12))
{
cout << "Illegal month value!\n";
exit(1);
}
if ((day < 1) || (day > 31))
{
cout << "Illegal day value!\n";
exit(1);
}
} DayOfYear::DayOfYear(int monthValue, int dayValue)
{ month = monthValue; day = dayValue;
testDate( );
}There is a preferable way of initializing data members, using an initializer list. This looks like:
DayOfYear::DayOfYear(int monthValue, int dayValue) :month(monthValue), day(dayValue) { testDate( );
}where we start with a colon ':', followed by initialization pairs. Each pair has the data member name (e.g. month) and in parenthesis the value to use to initialize it (e.g. monthValue).
Instances (i.e. objects) of DayOfYear could be created using:
DayOfYear date1(7,4), date2(5,5), date3(3), date4;Note how the different constructors are called. Calling a constructor with no arguments requires leaving off the parenthesis (e.g. date4), otherwise the compiler would have no way of differentiating this from a declaration of a function with no arguments.
Constructors may not be explicitly called like other member functions(e.g. date1.DayOfYear(7,4) is illegal). A constructor can be explicitly called when used to initialize a new instance, such as
DayOfYear date1; // default constructor called here
DayOfYear date2; // default constructor called here
date1 = DayOfYear(12,25); // 2 argument constructor explicitly called here
date2 = DayOfYear(); // call the default constructorIn the case above where date1 is assigned, an anonymous object is created on the right-hand side DayOfYear(12,25), and then that anonymous object is assigned into date1.
A default constructor is supplied for you if you have not explicitly defined any constructors. Once you define a constructor, however, you should be sure to also create a default constructor (with no arguments). Otherwise a declaration with no arguments (e.g. DayOfYear date1) would be illegal.
See this example of a simple Date class, illustrating using constructors, static data members and functions, const parameters and functions, and a simple destructor.
[CS Dept] [UIC]
[Prof. Reed]