COS1512/Workshop exercises/2/2012
Workshop Exercises
Introduction to Programming II
COS1512
Semester 2
School of Computing
Bar code
Open Rubric
CONTENTS
1. Introduction...................…………………………………..………………………………………....2
2. Workshop Exercises…..………………………………………………………………………….....2
2
COS1512/Workshop exercises
1 INTRODUCTION
Many students find this module difficult to tackle. For this reason a workshop on objects, the main
focus of this module, was offered in the past. Due to poor attendance, the workshop will not be
offered this semester. However, this document provides the exercises done in the workshop. We
suggest you do these exercises before attempting assignment 2 to assist you in mastering the
basic principles regarding objects. A comprehensive solution to the exercises is presented in the
document Solution to Workshop Exercises.
Please read the following material in preparation for the exercises:
Chapters 10 to 12 in Savitch
Appendix F of Tutorial Letter 101 (the Study Guide)
Section 4 Separate Compilation: Creating Dev-C ++ Projects with Multiple Files in
Appendix A of Tutorial Letter 101.
2 WORKSHOP EXERCISES
Create a class called TimeType to implement the time of day in a program. This represents a
simplified version of a watch. To do this, three files are required:
A header file TimeType.h, that contains the specification of the TimeType class (the interface
to the class);
an implementation file TimeType.cpp, that contains the implementation of the TimeType
class and
a main application file TestTimeType.cpp to test the class TimeType. This file contains the
main() function.
A skeleton of the class specification is given below:
class TimeType {
public:
:
:
private:
int hrs; //hrs < 24
//If hrs is incremented to 24 it should be reset to 0
int mins; //mins < 60
int secs; //secs < 60
};
The TimeType class specification includes the following:
a default constructor that initialises the hours, minutes and seconds to 0 ;
an overloaded constructor that initialises the hours, minutes and seconds according to
three input parameters;
The following member functions:
GetHours() that returns the current value of the hours;
GetMins() that returns the current value of the minutes;
GetSecs() that returns the current value of the seconds;
Set()that sets the hours, minutes and seconds according to three input parameters;
a destructor;
3
a member function TimeToSecs() that converts time (given in hours, minutes and
seconds) to seconds;
a member function SecsToTime() that accepts a parameter representing time in seconds.
SecsToTime() converts the parameter to time in hours, minutes and seconds and sets
the calling object to this time. Use the function declaration void SecsToTime(const int
s);
an overloaded operator - (implemented as a friend) that determines the period between
two given times. It should receive two objects of type TimeType as parameters and return
an object of type TimeType. The two private functions TimeToSecs() and
SecsToTime() are used to determine the time difference between the two parameters.
an overloaded operator << (implemented as a friend) that overloads the stream insertion
operator. It should display the time in the format hh:mm:ss to the given output stream;
and
three private member variables (data members) namely hrs, mins and secs that contain
the current hours, minutes and seconds respectively. These are already given in the
skeleton class specification above.
Question 1
Create the header file TimeType.h that contains the TimeType class specification. This is the
interface file. Initially, insert only the following (partial) declaration for the TimeType class in
TimeType.h.
class TimeType {
public:
private:
int hrs; //hrs < 24
//If hrs is incremented to 24 it should be reset to 0
int mins; //mins < 60
int secs; //secs < 60
};
Question 2
2.1. To enable us to test the class TimeType, create a project that will serve as the main
application file and save it as TestTimeType.cpp. Type the following code in
TestTimeType.cpp:
//main application file
#include <iostream>
#include <string>
#include "TimeType.h"
using namespace std;
int main()
{
return 0;
}
2.2 Add the implementation file TimeType.cpp to the project. TimeType.cpp will contain the
implementation of the TimeType class as described in question 1. Type the following code in
TimeType.cpp:
4
COS1512/Workshop exercises
// implementation file
#include <iostream>
#include <string>
#include "TimeType.h"
using namespace std;
Question 3
Complete the class specification TimeType in the header file TimeType.h, and create and test the
implementation file TimeType.cpp step by step. To do this is, insert the member function
declarations at the correct position in the class specification in the header file TimeType.h. Then
insert the definition for each member function or auxiliary function in TimeType.cpp, as indicated
by the instructions below.
3.1. Insert the member function declaration for the default constructor in the header file
TimeType.h. Add the definition for the default constructor to the implementation file
TimeType.cpp. The default constructor initialises the hours, minutes and seconds to 0.
3.2. Insert the member function declaration for GetHours()in the header file TimeType.h. Add
the definition for the member function GetHours()to the implementation file TimeType.cpp.
GetHours()returns the current value of the hours.
3.3. Insert the member function declaration for Getmins()in the header file TimeType.h. Add
the definition for the member function GetMins()to the implementation file TimeType.cpp.
Getmins() returns the current value of the minutes.
3.4. Insert the member function declaration for GetSecs()in the header file TimeType.h. Add
the definition for the member function GetSecs()to the implementation file TimeType.cpp.
GetSecs()returns the current value of the seconds.
To test these functions add the following statements to the file TestTimeType.cpp, and
compile and execute TestTimeType.cpp.
TimeType time1;
int h,m,s;
h = time1.GetHours();
m = time1.GetMins();
s = time1.GetSecs();
cout << "time1 constructed with default constructor: Hours = "
<< h << " minutes = " << m << " seconds = " << s << endl
<< endl;
3.5. Insert the member function declaration for the member function Set()in the header file
TimeType.h. Add the definition for the member function Set()to the implementation file
TimeType.cpp. The member function Set()sets the hours, minutes and seconds according
to three input parameters. To test this function, add the following statements to
TestTimeType.cpp, and compile and execute TestTimeType.cpp.
time1.Set(13,23,59);
h = time1.GetHours();
m = time1.GetMins();
s = time1.GetSecs();
cout << "time1 set to a new value: Hours = " << h
5
<< " minutes = " << m << " seconds = " << s << endl << endl;
3.6. Insert the member function declaration for the destructor in the header file TimeType.h. Add
the definition for the destructor to the implementation file TimeType.cpp. Include a message
to print “Goodbye!” in the implementation of the destructor. Compile and run
TestTimeType.cpp.
3.7. Insert the member function declaration for the overloaded constructor in the header file
TimeType.h. Add the definition for the overloaded constructor to the implementation file
TimeType.cpp. The overloaded constructor initialises the hours, minutes and seconds
according to three input parameters. To test the overloaded constructor, add the following
statements to TestTimeType.cpp, and compile and execute TestTimeType.cpp.
TimeType time2(5,30,19);
h = time2.GetHours();
m = time2.GetMins();
s = time2.GetSecs();
cout << "time2 constructed with overloaded constructor: Hours = " <<
h << " minutes = " << m << " seconds = " << s << endl
<< endl;
3.8. Insert the member function declaration for the member function TimeToSecs()in the header
file TimeType.h. Add the definition for the member function TimeToSecs()to the
implementation file TimeType.cpp. The member function TimeToSecs()converts time (given
in hours, minutes and seconds) to seconds. To test this function, add the following
statements to TestTimeType.cpp, and compile and execute TestTimeType.cpp.
int allSecs;
allSecs = time1.TimeToSecs();
h = time1.GetHours();
m = time1.GetMins();
s = time1.GetSecs();
cout << "time1 Hours = " << h
<< " minutes = " << m << " seconds = " << s << endl
<< " converted to seconds is " << allSecs << " seconds"
<< endl;
Insert the member function declaration for the member function SecsToTime()in the header
file TimeType.h. Add the definition for the member function SecsToTime()to the
implementation file TimeType.cpp. The member function SecsToTime()converts seconds to
an object representing time in hours minutes and seconds. To test this function, add the
following statements to TestTimeType.cpp, and compile and execute TestTimeType.cpp.
TimeType time3;
time3.SecsToTime(allSecs);
h = time3.GetHours();
m = time3.GetMins();
s = time3.GetSecs();
cout << allSecs << " seconds converted to time is: Hours = "
<< h << " minutes = " << m << " seconds = " << s << endl
<< endl;
3.9. Insert the friend function declaration for the overloaded operator - in the header file
TimeType.h. Add the definition for the overloaded operator - to the implementation file
TimeType.cpp. The overloaded operator - determines the period between two given times.
6
COS1512/Workshop exercises
It should receive two objects of type TimeType as parameters and use the two private
functions TimeToSecs() and SecsToTime() to return an object of type TimeType
representing the time difference between the two given times. To test this function, add the
following statements to TestTimeType.cpp, and compile and execute TestTimeType.cpp.
TimeType time4;
time4 = time1 - time2;
h = time4.GetHours();
m = time4.GetMins();
s = time4.GetSecs();
cout << "Time difference between time 1 and time 2: Hours = "
<< h << " minutes = " << m << " seconds = " << s << endl
<< endl;
3.10.Insert the friend function declaration for the overloaded operator << in the header file
TimeType.h. Add the definition for the overloaded operator << to the implementation file
TimeType.cpp. The overloaded operator << overloads the stream insertion operator to write
the time in the format hh:mm:ss to the given output stream. To test this function, add the
following statements to TestTimeType.cpp, and compile and execute TestTimeType.cpp.
cout << "time1: " << time1 << endl;
cout << "time2: " << time2 << endl;
Question 4 (Bonus question)
4.1 Add an overloaded operator ++ (implemented as a friend) that advances the time by one
second to the class TimeType.
Insert the friend function declaration for the overloaded operator ++ in the header file
TimeType.h. Add the definition for the overloaded operator ++ to the implementation file
TimeType.cpp. The overloaded operator ++ advances the time by one second. To test this
function, add the following statements to TestTimeType.cpp, and compile and execute
TestTimeType.cpp.
++time1;
h = time1.GetHours();
m = time1.GetMins();
s = time1.GetSecs();
cout << "time1 incremented once: Hours = " << h
<< " minutes = " << m << " seconds = " << s << endl << endl;
Test the class Timetype also with the application program shown below. To do this, open a
new project and add the implementation file TimeType.cpp to it. Type the application
program shown below in the main file. The code for this application program can be
downloaded from the departmental website. Study the output once this file has been
compiled and executed. Ensure that you understand each step in the program as well as the
corresponding output.
//The second main application file
#include <iostream>
#include <string>
#include "TimeType.h"
7
using namespace std;
int main()
{
TimeType time1(5, 30, 0);
TimeType time2;
cout << "time 1: " << time1 << endl;
cout << "time 2: " << time2 << endl;
time1.Set(23, 59, 55);
cout << "Incrementing time1 from 23:59:55:" << endl;
int LoopCount;
for (LoopCount = 1; LoopCount <=10; LoopCount++)
{
cout << time1 << ' ';
cout << ' ' ;
++time1;
}
cout << endl;
return 0;
}
Question 5
To further enhance your comprehension, classify each of the member functions in the class
TimeType as an accessor, mutator or facilitator.
©
UNISA 2012