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