Second week item three--time class

Source: Internet
Author: User

After reading and running the program, expand the functionality of the class as required.

#include <iostream> using namespace std;         Class Time {public:void set_time ();    void Show_time ();   Private:bool is_time (int, int, int);      This member function is set to private and is appropriate, please taste int hour;      int minute;  int sec;  };      void Time::set_time () {char c1,c2;      cout<< "Please enter the time (format Hh:mm:ss)";          while (1) {cin>>hour>>c1>>minute>>c2>>sec; if (c1!= ': ' | |          c2!= ': ') cout<< "format is not correct, please re-enter" <<endl;          else if (!is_time (hour,minute,sec)) cout<< "Time is illegal, please re-enter" <<endl;      else break;  }} void Time::show_time () {cout<Requirements:
(1) On the basis of the original class, add the following member functions within the class (will be built-in member functions)

    • Add_a_sec ()//Add 1 seconds
    • Add_a_minute ()//Add 1 minutes
    • Add_an_hour ()//Add 1 hours
in the main () number, the newly added member function is called to test the expanded functionality.
(2) Add another three member functions, which require declaration within a class, outside of the class definition.
    • Add_seconds (int)//Add n seconds
    • add_minutes (int)//Add N minutes
    • add_hours (int)//Increase n hours
Tips:
    • Consider the case where the increase exceeds the range of values;
    • Add n seconds, the number of seconds may exceed 60, adjust the number of seconds, and can call the number of minutes to increase the member function to make time legal; Similarly, the increase in minutes has similar problems.
My answer:

/* Copyright (c) 2014, Yantai University School of Computer * All rights reserved.     * File name: Test.cpp * Chen Dani * Completion Date: March 18, 2015 * Version number: v1.0 */#include <iostream>using namespace Std;class time{public:    void Set_time ();    void Show_time ();    void add_a_hours (int);    void add_a_minutes (int);    void add_a_seconds (int);p Rivate:bool is_time (int,int,int);    int hour;    int minute; int sec;};    void Time::set_time () {char c1,c2;    cout<< "Please enter the time (format Hh:mm:ss)";        while (1) {cin>>hour>>c1>>minute>>c2>>sec; if (c1!= ': ' | |        c2!= ': ') cout<< "format is not correct, please re-enter" <<endl;        else if (!is_time (hour,minute,sec)) cout<< "Time is illegal, please re-enter" <<endl;    else break; }}void Time::show_time () {cout<Experience: be familiar with how to define a function and the value that the parameter passes to the argument. Or to be familiar with the knowledge of the past.


Second week item three--time class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.