CSCI 240 - Possible Questions for Quiz 10

Use the following class definition to answer the questions on this quiz:

class Time
  {
  public:
    Time( int, int, int );    //Initializes a Time object

    void setHour( int );      //Set hour data member to a value between 1 and 12
    void setMinute( int );    //Set minute data member to a value between 0 and 59
    void setSecond( int );    //Set second data member to a value between 0 and 59

    int getHour();            //Returns the contents of the hour data member
    int getMinute();          //Returns the contents of the minute data member
    int getSecond();          //Returns the contents of the second data member

    void printTime();         //Prints the contents of a Time object (hour:minute:second)

  private:
    int hour, minute, second;
  };
  1. (2 points) Write the constructor for the Time class. It takes three integer arguments: the potential hour, minute, and second values for the Time object. The constructor should pass the individual arguments to the setHour, setMinute, and setSecond methods to initialize the data members.

  2. (2 points) Write the setHour method for the Time class. It takes an integer argument that will potentially be used to initialize the hour data member. If the passed in value is not between 1 and 12, inclusive, then initialize the hour data member to 12. Otherwise, use the passed in value to initialize the hour data member.

  3. (2 points) Write the getSecond method for the Time class. It takes no arguments and returns an integer. This method should simply return the contents of the second data member.

  4. (2 points) Write the printTime method for the Time class. It takes no arguments and returns nothing. This method should print the time in the format hour:minute:second. For minutes and seconds, if the value is less than 10, display a leading 0.

  5. (2 points) Create an instance of the Time class called time1. It should have an initial time with the hour value of 10, minute value of 3, and second value of 28.

  6. (2 points) Call the printTime method for an instance of the Time class named time1.

  7. (2 points) Write a single line of C++ code that will display/print only the hour value for an instance of the Time class named time1.