You are on page 1of 3

Structured Programming with Data Structures Group Assignment Lecturer: Gary Campbell Due Date: August 21, 2013

Define a class, clockType, to implement the time of day in a program. Furthermore, suppose that the time is represented as a set of three integers: one to represent the hours, one to represent the minutes, and one to represent the seconds. The program should be able to perform the following operations on the time: 1. Set the time. 2. Return the time. 3. Print the time. 4. Increment the time by one second. 5. Increment the time by one minute. 6. Increment the time by one hour. 7. Compare two times for equality. class clockType should have 10 members: three data members and seven function members.

Some members of the class clockType may be private; others may be public. Deciding which member to make private and which to make public depends on the nature of the member. The general rule is that any member that needs to be accessed outside the class is declared public; any member that should not be accessed directly by the user should be declared private. For example, the user should be able to set the time and print the time. Therefore, the members that set the time and print the time should be declared public. Similarly, the members to increment the time and compare the time for equality should be declared public. On the other hand, to control the direct manipulation of the data members hr, min, and sec, declare these data members private. Furthermore, note that if the user has direct access to the data members, member functions such as setTime are not needed.

The following statements define the class clockType: class clockType { public: void setTime(int hours, int minutes, int seconds); //Function to set the time //The time is set according to the parameters //Postcondition: hr = hours; min = minutes; sec = seconds // The function checks whether the values of hours, // minutes, and seconds are valid. If a value is invalid, // the default value 0 is assigned. void getTime(int& hours, int& minutes, int& seconds) const; 1

//Function to return the time //Postcondition: hours = hr; minutes = min; seconds = sec void printTime() const; //Function to print the time //Postcondition: Time is printed in the form hh:mm:ss. void incrementSeconds(); //Function to increment the time by one second //Postcondition: The time is incremented by one second. // If the before-increment time is 23:59:59, the time // is reset to 00:00:00. void incrementMinutes(); //Function to increment the time by one minute //Postcondition: The time is incremented by one minute. // If the before-increment time is 23:59:53, the time // is reset to 00:00:53. void incrementHours(); //Function to increment the time by one hour //Postcondition: The time is incremented by one hour. // If the before-increment time is 23:45:53, the time // is reset to 00:45:53. bool equalTime(const clockType& otherClock) const; //Function to compare the two times //Postcondition: Returns true if this time is equal to // otherClock; otherwise, returns false private: int hr; //stores the hours int min; //store the minutes int sec; //store the seconds };

Note the following in the definition of the class clockType: The class clockType has seven function members: setTime, getTime, printTime, incrementSeconds, incrementMinutes, incrementHours, and equalTime. It has three data members: hr, min, and sec. The three data membershr, min, and secare private to the class and cannot be accessed outside the class. The seven function memberssetTime, getTime, printTime, incrementSeconds, incrementMinutes, incrementHours, and equalTimecan directly access the data members (hr, min, and sec). In other words, do not pass data members as parameters to member functions. In the function equalTime, the parameter otherClock is a constant reference parameter. That is, in a call to the function equalTime, the parameter otherClock receives the address of the actual parameter, but otherClock cannot modify the value of the actual parameter. You could have declared otherClock as a value parameter, but that would require otherClock to copy the value of the actual parameter, which could result in poor performance The word const at the end of the member functions getTime, printTime, and equalTime specifies that these functions cannot modify the data members of a variable of type clockType.

Note: (Order of public and private members of a class) C++ has no fixed order in which you declare public and private members; you can declare them in any order. The only thing you need to remember is that, by default, all members of a class are private. You must use the public label to make a member available for public access. If you decide to declare the private members after the public members, you must use the private label to begin the declaration of the private members. In the definition of the class clockType, all data members can be private and all function members can be public. However, a function member can also be private. For example, if a member function is used only to implement other member functions of the class, and the user does not need to access this function, you make it private. Similarly, a data member of a class can also be public. The function setTime sets the three data membershr, min, and secto a given value. The given values are passed as parameters to the function setTime. The function printTime prints the time, that is, the values of hr, min, and sec. The function incrementSeconds increments the time by one second, the function incrementMinutes increments the time by one minute, the function incrementHours increments the time by one hour, and the function equalTime compares the two times for equality.
Extending the definition of the class clockType to include two constructors: class clockType { public: //Place the function prototypes of the functions setTime, //getTime, printTime, incrementSeconds, incrementMinutes, //incrementHours, and equalTime as described earlier, here. clockType(int hours, int minutes, int seconds); //Constructor with parameters //The time is set according to the parameters. //Postconditions: hr = hours; min = minutes; sec = seconds // The constructor checks whether the values of hours, // minutes, and seconds are valid. If a value is invalid, // the default value 0 is assigned. clockType(); //Default constructor with parameters //The time is set to 00:00:00. //Postcondition: hr = 0; min = 0; sec = 0 private: int hr; //stores the hours int min; //store the minutes int sec; //store the seconds }; 3

You might also like