COSC 1320 - C++ Programming
Bob Comer, Professor, CIT/CSC


Notes on Classes

When you are working with classes, it is important to distinguish between code in the member functions of a class ("class code") and code that uses the class. Code that makes use of the services provided by a class is sometimes called client code, and I will use this term in the following discussion.

Set and Get Member Functions
Read the introductory material on classes (pages 575 - 603 in the Malik textbook) before reading this section

The data members of a class should normally be private members to prevent the client programmer from misusing the class. Controlled access to the private data members is provided by public member functions called access functions. These functions must be carefully designed to maintain the integrity of the data. Member functions that allow the client programmer to store data in a class object are usually called "set" functions. Set functions should always do data validation before storing the data in the object. Member functions that retrieve data stored in an object are called "get" functions.

Following is a partial listing of the clockType class from Chapter 13 of the Malik textbook showing the get and set member functions.

class clockType
{
public:
   void setTime( int, int, int ); 
   void getTime( int&, int&, int& ); 
private: 
   int hr; 
   int min; 
   int sec; 
};

void clockType::setTime( int hours, int minutes, int seconds )
{
   if ( 0 <= hours && hours < 24 ) 
      hr = hours;
   else
      hr = 0;
   if ( 0 <= minutes && minutes < 60 ) 
      min = minutes;
   else 
      min = 0;
   if ( 0 <= seconds && seconds < 60 ) 
      sec = seconds;
   else
      sec = 0;
}

void clockType::getTime( int& hours, int& minutes, int& seconds )
{
   hours = hr;
   minutes = min;
   seconds = sec;
}

 

Notice the data validation in the setTime function. Values provided by the client programmer are checked to ensure that they are in the valid range of values before being assigned to the data members. You might think that this function should print error messages if the data is invalid. However, normally the client programmer should decide how to deal with errors like this. Exception handling provides a natural way to deal with problems like this. Exception handling is covered in our next C++ course (Advanced C++). In this course, we will generally just change invalid values to some "default" value (in this case, zero).

The clockType class in the Malik textbook only includes a single get function. Member function getTime returns the value of each of the class data members. Often classes will provide functions for retrieving the values of individual data members. In the partial listing of setTime below, we have added individual get functions.

class clockType
{
public:
	void setTime( int, int, int );
	void getTime( int&, int&, int& );
	int getHours( );
	int getMinutes( );
	int getSeconds( );
private:
	int hr;
	int min;
	int sec;
};

void clockType::setTime( int hours, int minutes, int seconds )
{
	if ( 0 <= hours && hours < 24 ) 
		hr = hours;
	else
		hr = 0;
	if ( 0 <= minutes && minutes < 60 ) 
		min = minutes;
	else 
		min = 0;
	if ( 0 <= seconds && seconds < 60 ) 
		sec = seconds;
	else
		sec = 0;
}

void clockType::getTime( int& hours, int& minutes, int& seconds )
{
	hours = hr;
	minutes = min;
	seconds = sec;
}

int clockType::getHours( )
{
	return hr;
}

int clockType::getMinutes( )
{
	return min;
}

int clockType::getSeconds( )
{
	return sec;
}

 

Classes may also provide multiple set functions that give the client programmer different options for storing data in the object. In this particular example, a single setTime function seems adequate.

 

Constructor member functions

Constructor functions allow the client programmer to initialize the data members of an object when it is created. (Contrast this with a set function, which only allows the client programmer to store values in the data members of an object after it is created.) The partial listing of the clockType class below includes two constructor functions taken from the Malik textbook.

class clockType
{
public:
	clockType( );
	clockType( int, int, int );
	void setTime( int, int, int );
	void getTime( int&, int&, int& );
	int getHours( );
	int getMinutes( );
	int getSeconds( );
private:
	int hr;
	int min;
	int sec;
};

clockType::clockType( )
{
	hr = 0;
	min = 0;
	sec = 0;
}

clockType::clockType( int hours, int minutes, int seconds )
{
	if ( 0 <= hours && hours < 24 ) 
		hr = hours;
	else
		hr = 0;
	if ( 0 <= minutes && minutes < 60 ) 
		min = minutes;
	else 
		min = 0;
	if ( 0 <= seconds && seconds < 60 ) 
		sec = seconds;
	else
		sec = 0;
}

void clockType::setTime( int hours, int minutes, int seconds )
{
	if ( 0 <= hours && hours < 24 ) 
		hr = hours;
	else
		hr = 0;
	if ( 0 <= minutes && minutes < 60 ) 
		min = minutes;
	else 
		min = 0;
	if ( 0 <= seconds && seconds < 60 ) 
		sec = seconds;
	else
		sec = 0;
}

void clockType::getTime( int& hours, int& minutes, int& seconds )
{
	hours = hr;
	minutes = min;
	seconds = sec;
}

int clockType::getHours( )
{
	return hr;
}

int clockType::getMinutes( )
{
	return min;
}

int clockType::getSeconds( )
{
	return sec;
}

 

Notice that the code in the constructor with parameters is identical to the code in setTime. When you repeat code in your program, you increase your chances of making mistakes and you make it harder to update your program at a later time. In a class, it is best to provide a single function to validate data provided by the client programmer before it is stored in an object. In this case, setTime is the logical choice. All other functions that need to store data provided by the client programmer should use setTime to store the data. The partial listing of the clockTime class below contains a new version of the constructor with parameters that eliminates the duplicated code.

class clockType
{
public:
	clockType( );
	clockType( int, int, int );
	void setTime( int, int, int );
	void getTime( int&, int&, int& );
	int getHours( );
	int getMinutes( );
	int getSeconds( );
private:
	int hr;
	int min;
	int sec;
};

clockType::clockType( )
{
	hr = 0;
	min = 0;
	sec = 0;
}

clockType::clockType( int hours, int minutes, int seconds )
{
	setTime( hours, minutes, seconds );
}

void clockType::setTime( int hours, int minutes, int seconds )
{
	if ( 0 <= hours && hours < 24 ) 
		hr = hours;
	else
		hr = 0;
	if ( 0 <= minutes && minutes < 60 ) 
		min = minutes;
	else 
		min = 0;
	if ( 0 <= seconds && seconds < 60 ) 
		sec = seconds;
	else
		sec = 0;
}

void clockType::getTime( int& hours, int& minutes, int& seconds )
{
	hours = hr;
	minutes = min;
	seconds = sec;
}

int clockType::getHours( )
{
	return hr;
}

int clockType::getMinutes( )
{
	return min;
}

int clockType::getSeconds( )
{
	return sec;
}

 

Abstraction
Read the section on Data Abstraction (pages 603 - 610) in the Malik textbook) before reading this section.

Abstraction is the act of separating the logical properties of an object from the implementation details. Information hiding refers to hiding the implementation details of a class from the client programmer. To make use of a class, the client programmer only needs to know how to use (call) the public member functions of the class. She does not need to know any of the other details of the class implementation. Together abstraction and information hiding provide a high degree of independence between the class code and the client code. This independence makes program code much easier to maintain and update.

As an example of abstraction, the following partial implementation of the clockType class illustrates that the client programmer's view of an object (the logical view) might differ from the actual implementation of the object. In this example, the external view is that a time is represented by 3 integers - the hours, minutes, and seconds since midnight. However, the implementation has been changed from the previous examples. Time is represented by a single integer - the total number of seconds since midnight. Notice that while this change would require major modifications in most of the member functions, no change would be required in the client code.

class clockType
{
public:
	clockType( );
	clockType( int, int, int );
	void setTime( int, int, int );
	void getTime( int&, int&, int& );
	int getHours( );
	int getMinutes( );
	int getSeconds( );
private:
	int sec;
};

clockType::clockType( )
{
	sec = 0;
}

clockType::clockType( int hours, int minutes, int seconds )
{
	setTime( hours, minutes, seconds );
}

void clockType::setTime( int hours, int minutes, int seconds )
{
	if ( 0 <= hours && hours < 24 && 
	     0 <= minutes && minutes < 60 && 
	     0 <= seconds && seconds < 60 ) 
		sec = seconds + 60 * minutes + 3600 * hours;
	else
		sec = 0;
}

void clockType::getTime( int& hours, int& minutes, int& seconds )
{
	hours = getHours( );
	minutes = getMinutes( );
	seconds = getSeconds( );
}

int clockType::getHours( )
{
	return sec / 3600;
}

int clockType::getMinutes( )
{
	return sec % 3600 / 60;
}

int clockType::getSeconds( )
{
	return sec % 3600 % 60;
}

Return to C++ Home Page

Copyright: Ó 2003 by the Austin Community College
Department of Computer Studies. All rights reserved.
Comments to:
Bob Comer
Last updated: July. 8 2003