Lez's home
MSc Degree
-
Previous page
Next page
Critical Appraisal
Introduction
The main purpose of this assignment was to write the declaration and implementation files in order to run the two supplied driving .cpp files of testdial.cpp and testclk.cpp. This involved writing the classes Dial and Clock24. Further implementations of using the class Dial were required. Two new classes (Imperial and Meter) were created and are documented below.[1]
testdial.cpp
This is a supplied program to run dial.cpp and dial.h. The source code for testdial.cpp is on page 11. dial.cpp is on page 15 and dial.h is on page 14. Reading through the testdial.cpp code showed which constructors and functions were required for class Dial. These were:
- a conversion constructor accepting two integer arguments
It was obvious that the first integer set the number of divisions and the second the current position of the dial. Therefore, two private data members of type int were required to hold the number of divisions and the current position (called dialdivision and position respectively).
- a default deconstructor
Since a constructor is going to be used a deconstructor is needed. No arrays of any type are included in class Dial, so this is a very simple deconstructor. In fact, none of the classes in this report use any arrays so all the deconstructors are very simple and none of them include any statements.
- a default constructor
Constructs an instance of class Dial with all data members set to zero.
- a copy constructor accepting a Dial as an argument
In testdial.cpp snooze_hrs is declared as being assigned the value of alarm_hrs. This could have been written snooze_hrs(alarm_hrs), which shows the Dial alarm_hrs being used as an argument, although either way of expressing this declaration is correct.
- an assignment operator (overloaded operator =)
After declaration the Dial alarm_mins is given the value of bedtime_mins. This is different to the copy constructor’s assignment since that was done at creation of the object. Here the assignment is on two previously created Dial objects.
- a set() function accepting two integer arguments
snooze_mins was created with the default constructor. The set() function is to give values to the number of dial divisions (first argument) and the current position (second argument).
- a value() function no arguments, returns an int
This function was required to find out the value of position, which is a private data member. The value is returned as an integer.
- an equality operator (overloaded operator = =)
This was required to compare two Dials and return true if they were the same. In this case ‘the same’ means they need to have the same number of dial divisions and the same current position.
- an addition & assignment operator (overloaded operator +=)
This was required to add an integer value to a Dial object’s current position and give the new current position as a result of the addition. Checks needed to be included to see if the dial goes past the last division that is ‘over the top’.
- a passedtop() function which returns an integer
This function is to provide a value for the dial having gone ‘over the top’, which may affect another dial. This function includes calculations to allow for the dial going ‘over the top’ more than once. The value for ‘over the top’ is kept in a private data member of type integer called advance.
- an ostream output operator (overloaded operator <<)
To enable a Dial to be output to screen the operator<< function was also required. This must be a friend function since it needs to be able to read the class’s private data members from outside the current instance.
testclk.cpp
This is a supplied program to run clock24.cpp and clock24.h. Note: clock24.h #includes dial.h so the functions described above are available to it. The source code for testclk.cpp is on page 12. clock24.cpp is on page 17 and clock24.h is on page 16. The constructors and functions required for class Clock24 are as follows:
- a conversion constructor accepting two integer arguments
For a Clock24 type object, the two integer arguments set two dials – the first for the hours, the second for the minutes. Both these Dial objects are declared as private data members.
- a default deconstructor
- a default constructor
alarm was declared without any arguments so this needs its hours and minutes current positions to be set to zero (i.e. the time will be 00:00), but the Dials of hours and minutes will have the dial divisions set to 24 and 60 respectively.
- a copy constructor accepting a Dial as an argument
As in testdial.cpp, the Clock24 object snooze is declared as being assigned the value of Clock24 object alarm. For this the Dials are constructed using the conversion constructor supplied in dial.h. Again, this expression could have been written as snooze(alarm).
- a set() function accepting two integer arguments
This function is very similar to the Dial set() function. The first integer argument is the hours and the second the minutes. Class Clock24 set() function calls the Dial set() function in order to set the Dials of hours and minutes.
- a subtraction operator (overloaded operator -)
This was required to subtract one Clock24 from another Clock24 and to return the total number of minutes as an integer. Since this is a binary operation using two Clock24s, this overloaded operator- function had to be declared as a friend function. This allows private data members of both instances of Clock24 to be read for the calculation.
- an addition & assignment operator (overloaded operator +=)
This was required to add an integer value to a Clock24’s minutes, with appropriate functionality to increment the hours dial if necessary. The overloaded operator+= from class Dial was used for this function. Since this function only uses the current instance of class Clock24 the binary += operator does not need to be declared as a friend function.
- an ostream output operator (overloaded operator <<)
Again this operator needed to be overloaded to enable a Clock24 to be output to screen.
If an operator* function had been supplied for class Dial, it would have been possible for the overloaded operator- in class Clock24 to have used Dials for the calculation rather than referring directly to the value. This was not implemented due to lack of time.
metric.cpp
This is the driving .cpp file for the convert.exe program. It allows users to add measurements in feet and inches and obtain a result in yards, feet and inches, which is then converted to metres. Error checking on input data is included. This implementation makes use of class Imperial in imprl.cpp and imprl.h.
worm.cpp
This is the driving file for inchworm.exe, which uses class Imperial (in imprl.cpp and imprl.h). This program allows a user to set the starting position of a worm (within a length of 12 inches), specify how fast it can go (in inches per hour) and for how long it can travel. Limits have been included for speed (50 inches per hour) and time of travel (24 hours). This is a simple once-only program.
imprl.cpp & imprl.h
imprl.h is the header file for imprl.cpp and #include’s dial.h. Class Imperial has private data members of three Dials (for yards, feet and inches), and the following:
- a default constructor
This sets all Dials to zero by calling the conversion constructor of class Dial.
- a conversion constructor accepting two integer arguments
The first integer sets the feet and the second is for inches. This constructor also checks for the Dials going ‘over the top’ and, if so, adjusting the other Dials appropriately.
- a conversion constructor accepting one integer argument
This function (used by worm.cpp) sets the inches Dial. This constructor does not check for the inches Imperial value for going ‘over the top’. This extra functionality was not needed for worm.cpp, but could be added in the future.
- a copy constructor accepting an Imperial as an argument
This constructor uses the copy constructor of class Dial to create and assign values for each Dial of yards, feet and inches.
- a default deconstructor
- a set() function accepting two integer arguments
This function, similar to the Clock24 set() function, takes in two integers. The first integer is feet and the second inches. This function uses the operator+= function and set() function from class Dial.
- an assignment operator (overloaded operator =)
This function was included to allow one Imperial to be assigned to another Imperial.
- an addition & assignment operator (overloaded operator +=)
This function was included to add (and assign) two Imperials. Since this function uses two Imperials, it needed to be declared as a friend function.
- a multiplication operator (overloaded operator *)
This function was included to allow an Imperial to be multiplied by an integer in worm.cpp. Again this is a friend function.
- an ostream output operator (overloaded operator <<)
Again this operator needed to be overloaded to enable an Imperial to be output to screen.
Two more functions of operator- and operator+ are included, but these are not used in the implementations of worm.cpp or metric.cpp. They may be required for future implementations of class Imperial, so have not been removed.
elecmetr.cpp
This is the driving file for the elecmetr.exe program. This program allows the user to input the current meter reading, the number of units used per day and the number of days to run. The output displays the final meter reading, the number of units used and the total cost. The cost_per_unit is set within the program and fixed at compilation. This could be changed to an input value, allowing the user to see the same number of units used with differing unit cost. elecmetr.cpp implementation makes use of class Meter (which #include’s dial.h).
meter.cpp & meter.h
meter.h is the header file for meter.cpp. Class Meter has private data members of four Dials (thou, hund, ten and unit). Since all these Dials will have the same dial divisions, it would be possible to declare them as an array. This was not done due to lack of time. The public members of class Meter are as follows:
- a default constructor
This sets all Dials to zero by calling the conversion constructor of class Dial.
- a conversion constructor accepting four integer arguments
This sets each of the Dials to give a meter reading.
- a copy constructor accepting a Meter as an argument
This is used to create a Meter with the same values as another already constructed Meter.
- a default deconstructor
- a set() function accepting four integer arguments
This function uses the operator+= function and set() function from class Dial.
- an addition & assignment operator (overloaded operator +=)
This function was included to add (and assign) an integer to a Meter.
- a subtraction operator (overloaded operator -)
This was required to subtract one Meter from another Meter and return the total number of units as an integer. Since this is a binary operation using two Meters, this overloaded operator- function had to be declared as a friend function.
- an ostream output operator (overloaded operator <<)
Again this operator needed to be overloaded to enable a Meter to be output to screen.
Future Improvements
Possible future improvements which could be made to these programs:
class Dial & class Clock24
- Include other arithmetic overloaded operator functions for both classes.
- Include other functions e.g. for testing going ‘over the top’ in a reverse direction – useful for subtraction of Dials/Clock24s.
convert.exe
- Inclusion of other operations available to the user e.g. multiply, divide or subtract two or more measurements.
- Permitting negative input.
- For the quitting of the program to be more graceful, i.e. to use ‘q’ to quit and report errors for any other non-allowable input.
- To minimise the number of decimal places in the metres’ output.
inchworm.exe
- To allow for negative values to be input (the worm could crawl backwards if it wanted!).
- To allow for further unlimited runs of the program.
- To allow further functionality. e.g. the worm could travel for longer distances by including wait time (for it to rest and eat), etc.
- Use class Clock24 to keep track of time.
elecmetr.exe
- To have an array of four Dials for the meter.h rather than four individual Dials. For only four Dials the code is not too complicated. However, the use of an array could be of benefit if more Dials of the same type are used in this manner in another implementation.
- To have different error types reported depending on the error. e.g. character input, not enough arguments, too many arguments, negative number attempted, etc. (See test el6.txt, el7.txt & el8.txt on page 39.)
FOOTNOTES:
- Class names will be shown with an initial capital (e.g. Dial, Clock24), variables, functions, etc will be shown in courier type face (e.g. dialdivision), and file names will be shown in italics (e.g. testdial.cpp).
Lez's home
MSc Degree
-
Previous page
Next page
Copyright 2000 Lez Bullwer