Description
In this lab, we will implement a simple C++ class named Time. It represents all possible times in a 24-hour period, including hours, minutes and seconds. An immediate representation issue is how to handle morning (am) and afternoon (pm) times. We could have a separate bool indicating whether the time is am or pm. It is easier, however, to represent the hours in military time. This means that the hours of the day are numbered from 0 to 23, with 13 being 1 pm, 14 being 2 pm, etc.
Your notes from Lecture 3 with the example Date class will be helpful in completing this lab.
Checkpoint 1
In the first checkpoint you will get started by implementing the initial class design, several member functions, and a simple main program to test your class.
The instructions below describe how to build your executable using from the command line using g++ or clang++ using Cygwin or UNIX terminal. Even if you plan to use Visual Studio or another IDE for the bulk of your work this semester, you are required to also show that you can successfully build and run this lab using g++ from a terminal on your own machine. • Make a subfolder inside of your Data Structures labs directory for lab 2. Create 3 new empty code files named Time.h, Time.cpp and main.cpp. Note that in C++ the name of the header and implementation file are not required to exactly match the name of the class, but it is good coding style to do so. • Begin work on Time.h. Within the file, declare a class called Time. Follow the form and syntax of the Date class from Lecture 3. Read the syntax carefully (such as the semi-colon at the end of the class declaration). Add private member variables for the hour, minute and second. In the public area of the class, declare two constructors: one, the default constructor, should initialize each of the member variables to 0; the other, having three arguments, accepts initial values for the hour, minute and second as function call arguments. Declare member functions to access the values of the hour, the minute and the second (three different member functions). It will be crucial for Checkpoint 3 to make these const. (Recall: a const member function can not change the member variables.) Don’t write the body of any of the functions in the Time.h file. Save all the implementation for the Time.cpp file. • Switch to working on main.cpp. Be sure to add code to #include “Time.h” in addition to including #include Time t1(5,30,59); // calls the non-default constructor w/ 3 integer arguments Time t2(); // COMPILE ERROR – a buggy attempt to call the default constuctor Time t3; // the *correct* way to call the default constructor
• Now implement all of the class constructors and member functions in the file Time.cpp. Don’t forget to add the line to #include “Time.h”. Any file that uses or implements Time functionality must include the Time class header file.
• Now, compile your program and remove errors. Here’s where the difference between compiling and linking matters. When compiling using g++ on the command line, the two separate command lines:
g++ -c main.cpp -Wall g++ -c Time.cpp -Wall
compile the source code to create two object code files called main.o and Time.o separately. The -c means “compile only”. Compiler errors will appear at this point. If there are errors in main.cpp (or Time.cpp), then the files main.o (or Time.o) will not be created. Use the ls command to check. Important Note: We only compile .cpp files. We do not directly compile header files. Header files are compiled only indirectly when included in a .cpp file. Once you have driven out all of the compiler errors, you can “link” the program using the command:
g++ main.o Time.o -o Time_test.exe
to create the executable called Time test.exe. If you have not defined all of the necessary member functions in the Time class, then you would see “linking” errors at this point. You can combine all three command lines (compiling each of the 2 .cpp files to 2 object files and linking all object files) with this command:
g++ main.cpp Time.cpp -o Time_test.exe -Wall
Which is more similar to what we did last lab. Equivalently, if those are the only two .cpp files in the current directory, you can compile and link using the command line wildcard:
g++ *.cpp -o Time_test.exe -Wall
Note that this will not create the intermediate .o files and will only proceed to the linking step if the two files compile cleanly.
To complete this checkpoint: Show compilation of the program using g++/clang++ within the Cygwin or UNIX terminal, with all compiler errors removed and demonstrate correct execution of your program. Yes, please show us you can compile from the terminal with g++, even if you plan to primarily use Visual Studio or another IDE for the rest of the semester.
Checkpoint 2
Create and test a few more member functions. This will require modifications to all three of your files. • setHour, setMinute, setSecond. Each should take a single integer argument and change the appropriate member variable. For now, do not worry about illegal values of these variables (such as setting the hour to 25 or the minute to -15). Assume whoever calls the functions does the right thing. In general, this is a bad assumption, but we will not worry about it here. • PrintAmPm prints time in terms of am or pm, so that 13:24:39 would be output as 1:24:39 pm. This member function should have no arguments. Note that this requires some care so that 5 minutes and 4 seconds after 2 in the afternoon is output as 2:05:04 pm. The output should be to std::cout.
To complete this checkpoint: Show a TA your tested and debugged extensions