一、问题及代码
- #include <iostream>
- using namespace std;
- class Time
- {
- public:
- void set_time( );
- void show_time( );
- void add_a_sec()
- {
- sec++;
- }
- void add_a_minute()
- {
- minute++;
- }
- void add_an_hour()
- {
- hour++;
- }
- void add_seconds(int n);
- void add_minutes(int n) ;
- void add_hours(int n) ;
- void update();
- private:
- bool is_time(int, int, int); //这个成员函数设置为私有的,是合适的,请品味
- int hour;
- int minute;
- int sec;
- };
- void Time::set_time( )
- {
- char c1,c2;
- cout<<"请输入时间(格式hh:mm:ss)";
- while(1)
- { cin>>hour>>c1>>minute>>c2>>sec;
- if(c1!=':'||c2!=':')
- cout<<"格式不正确,请重新输入"<<endl;
- else if (!is_time(hour,minute,sec))
- cout<<"时间非法,请重新输入"<<endl;
- else
- break;
- }
- }
- void Time::show_time( )
- {
- cout<<hour<<":"<<minute<<":"<<sec<<endl;
- }
- bool Time::is_time(int h,int m, int s)
- {
- if (h<0 ||h>24 || m<0 ||m>60 || s<0 ||s>60)
- return false;
- return true;
- }
- void Time::add_seconds(int n)
- {
- sec=sec+n;
- }
- void Time::add_minutes(int n)
- {
- minute=minute+n;
- }
- void Time::add_hours(int n)
- {
- hour=hour+n;
- }
- void Time::update()
- {
- if(sec==60)
- {
- sec=0;
- minute++;
- }
- if(minute==60)
- {
- minute=0;
- hour++;
- }
- if(hour==24)
- {
- hour=0;
- }
- }
- int main( )
- {
- int x;
- Time t1;//定义对象
- t1.set_time( );
- t1.update();
- t1.show_time( );
- cout<<"时分秒各加1"<<endl;
- t1.add_a_sec();
- t1.add_a_minute();
- t1.add_an_hour();
- t1.update();
- t1.show_time();
- t1.set_time( );
- t1.update();
- t1.show_time( );
- cout<<"输入时分秒加(n?):";
- cin>>x;
- t1.add_seconds(x);
- t1.add_minutes(x);
- t1.add_hours(x);
- t1.update();
- t1.show_time();
- return 0;
- }
要求:请在原类基础上,增加下列成员函数,要求前三个在类内定义,后三个在类内声明,类外定义。在main()数中增加适当的调用以展示扩充类定义后的功能。
add_a_sec() //增加1秒钟
add_a_minute() //增加1分钟
add_an_hour() //增加1小时
add_seconds(int) //增加n秒钟
add_minutes(int) //增加n分钟
add_hours(int) //增加n小时
提示:(1)要考虑增加后超出取值范围的情形;(2)增加n秒后,秒数可能会超过60,调整秒数,并可以调用增加分钟数的成员函数,使时间合法;同理,增加分钟数也有类似问题。
代码
/*
* 文件名称:张引
* 作 者:
* 完成日期: 2017年 5月 1日
* 版 本 号:v1.0
* 对任务及求解方法的描述部分:类和对象
* 输入描述:
* 问题描述:类和对象
* 程序输出:
* 问题分析:
* 算法设计:
*/
#include<iostream>
using namespace std;
class Time
{
public:
void set_time();
void show_time();
void add_a_sec()
{
sec++;
}
void add_a_minute()
{
minute++;
}
void add_an_hour()
{
hour++;
}
void add_seconds(int n);
void add_minutes(int n);
void add_hours(int n);
void update();
private:
bool is_time(int, int, int);//这个成员函数设置为私有的,是合适的,请品味
int hour;
int minute;
int sec;
};
void Time::set_time()
{
char c1, c2;
cout << "请输入时间(格式hh:mm:ss)";
while (1)
{
cin >> hour >> c1 >> minute >> c2 >> sec;
if (c1 != ':' || c2 != ':')
cout << "格式不正确,请重新输入" << endl;
else if (!is_time(hour, minute, sec))
cout << "时间非法,请重新输入" << endl;
else
break;
}
}
void Time::show_time()
{
cout << hour << ":" << minute << ":" << sec << endl;
}
bool Time::is_time(int h, int m, int s)
{
if (h<0 || h>24 || m<0 || m>60 || s<0 || s>60)
return false;
return true;
}
void Time::add_seconds(int n)
{
sec = sec + n;
}
void Time::add_minutes(int n)
{
minute = minute + n;
}
void Time::add_hours(int n)
{
hour = hour + n;
}
void Time::update()
{
if (sec == 60)
{
sec = 0;
minute++;
}
if (minute == 60)
{
minute = 0;
hour++;
}
if (hour == 24)
{
hour = 0;
}
}
int main()
{
int x;
Time t1;//定义对象
t1.set_time();
t1.update();
t1.show_time();
cout << "时分秒各加1" << endl;
t1.add_a_sec();
t1.add_a_minute();
t1.add_an_hour();
t1.update();
t1.show_time();
t1.set_time();
t1.update();
t1.show_time();
cout << "输入时分秒加(n?):";
cin >> x;
t1.add_seconds(x);
t1.add_minutes(x);
t1.add_hours(x);
t1.update();
t1.show_time();
return 0;
}
二 运行结果
三 心得体会
总结复习了类和对象的实现方法 加深了对类的理解
四 知识点总结
1 类的声明
2 类内函数的声明和调用