COP 3014 Recitation - Week 9

Classes and objects

In this session, you will write a class named Time and use your Time class to create instances (or objects) of the Time type. Your Time class is a datatype, just as int, string, float, double, ofstream and ifstream are all datatypes. As a result, as long as you have the necessary #include directives, all of the following variable declarations are legal in C++:

  • int a;
  • double c;
  • ifstream f;
  • ifstream myinputfile;
  • float number;
  • Time theTime;
  1. In Visual Studio, right click on Header Files > Add > New item > Header File (.h). Name the file "time5.h"
  2. Type this into your "time5.h" file. Don't type the comments to save time.
  3. Right click on Source Files > Add > New item >C++ File (.cpp). Name it whatever you want to.
  4. Type this into your ".cpp" file. Don't type the comments to save time.
  5. Now you will change your main() function to use the constructor instead of the setTime() function. The prototype for the Time() constructor is declared in time5.h, and the function definition for the Time() constructor is defined in your .cpp file. Take a brief look at both of them. To use the constructor instead of the setTime() function, change your main() function to look like this.
  6. Replace your main() function with this one. Fill in the blanks.
Program examples