13032 - Time   

Description

Design a class named Time. Data which are stored in Time class consists of year(int), month(int), day(int), hour(int), minute(int), second(int).

 

Constructors:

  1. The default constructor of the class should initialize year(int), month(int), day(int), hour(int), minute(int), second(int) to -1.
  2. The parameterized constructor Time(int h, int mi, int s) should initialize Time's hour, minute and second to h, mi and s.
  3. The parameterized constructor Time(int y, int mo, int d, int h, int mi, int s) should initialize Time's year, month, day, hour, minute and second to y, mo, d, h, mi and s.

 

Methods:

  1. void setAll(int y, int mo, int d, int h, int mi, int s) – Should set time's year, month, day, hour, minute, second to y, mo, d, h, mi and s, repectively.
  2. void setTime(int h, int mi, int s) – Should set time's hour, minute, second to h, mi and s, repectively.
  3. void setDate(int y, int mo, int d) – Should set time's year, month, day to y, mo, and d, repectively.
  4. void print() - Should print out time’s data.
  • If all time’s data are set, print: h:mi:s d, mo, y. Example, time -> year(2020), month(11), day(29), hour(9), minute(2), second(49), print: 9:2:49 11 29 2020 (Hint: cout << hour << “:” << minute << “:” << second << “ ” << month << “ ” << day << “ ” << year << endl; )
  • If one of year, month, day is not set, print: h:mi:s. Example, time -> year(-1), month(11), day(29), hour(9), minute(2), second(49), print: 9:2:49
  • If one of hour, minute, second is not set, print: d, mo, y. Example, time -> year(2020), month(11), day(29), hour(-1), minute(-1), second(49), print: 11 29 2020
  • If one of year, month, day is not set, and one of hour, minute, second is not set, print "Time is not set". Example, time -> year(2020), month(-1), day(29), hour(9), minute(2), second(-1), print: Time is not set

 

main.cpp

#include <iostream>
#include <string>
 
using namespace std;
 
class Time
{
public:
    Time() : year(-1), month(-1), day(-1), hour(-1), minute(-1), second(-1) {}
    /* Todo */
    // Implement 2 parameterized constructors
 
    /* Todo */
    // Implment setAll(), setTime(), setDate(), print()
 
private:
    int yearmonthdayhourminutesecond;
};
 
// Don't touch main()
int main()
{
    int testcase = 0;
    cin >> testcase;
 
    if (testcase == 1)
    {
        Time time = Time(2020121121634);
        time.print();
    }
    else if (testcase == 2)
    {
        Time time1 = Time(155431);
        time1.print();
        Time time2 = Time();
        time2.print();
    }
    else if (testcase == 3)
    {
        Time time = Time();
        time.print();
        time.setDate(2021324);
        time.print();
        time.setTime(71517);
        time.print();
    }
    else if (testcase == 4)
    {
        Time time = Time(17, -145);
        time.print();
        time.setDate(20225, -1);
        time.print();
        time.setAll(2024, -14, -12322);
        time.print();
    }
    else if (testcase == 5)
    {
        Time time = Time(-1, -1, -1101010);
        time.print();
        time.setAll(30311225, -1, -1, -1);
        time.print();
        time.setTime(123117);
        time.print();
        time.setDate(2020121);
        time.print();
    }
 
    return 0;
}

Input

No need to handle input. (Input would be number 1~5 to represent the index of test-case)

Output

Depend on test case show in main.cpp.

Sample Input  Download

Sample Output  Download

Tags




Discuss