练习写C++代码(11)--实现简单的时钟类3

本文讨论了如何优化时间管理与计算过程,通过改进Clock类和People类的实现,提高了时间差计算效率,同时增加了功能如显示当前时间、设置闹钟和查看时间提示。此外,介绍了后续改进计划,包括代码分离、方法调整以简化代码,以及关键功能的增强,以适应更复杂的时间管理需求。
摘要由CSDN通过智能技术生成

对减法进行修改,假定两个时间相减只是为了知道他们的时间差,用于定时。

定义一个People来使用Clock类,方法是显示当前时间,设置闹钟,察看时间提示。


#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
using namespace std;

///define class Clock
class Clock
{
public:
	Clock();
	Clock(int, int);
	~Clock();
	
	///test valid
	bool test();
        bool test(int h, int m);
	///show the time
	void showTime();
	///give a prompt
	void prompt();
	///create object use ctor
	Clock createObject();
	
	friend class People;
	///overload operator
	friend Clock operator+(const Clock& c1, const Clock& c2);
	friend Clock operator-(const Clock& c1, const Clock& c2);
	friend ostream &operator<<(ostream &os, Clock &c);

private:
	int hour;
	int minutes;	
};
///ctor
Clock::Clock()
{

}

Clock::Clock(int h, int m)
{
	hour = h;
	minutes = m;
}

Clock::~Clock()
{

}
///test valid
bool Clock::test()
{
	if (hour > 23 || hour < 0 || minutes > 59 || minutes < 0)
		return false;

	return true;
}

bool test(int h, int m)
{
	if (h > 23 || h < 0 || m > 59 || m < 0)
		return false;

	return true;
}
///show time
void Clock::showTime()
{
	cout<<"The time is:"<
      
      
       
       <<"h"<
       
       
         <<"m"< 
        
          = t2) temp = t1 - t2; else temp = t1 + 24 * 60 -t2; return Clock(temp / 60, temp % 60); } ostream &operator<<(ostream &os, Clock &c) { os< 
         
           <<"h"< 
          
            <<"m"; return os; } ///give a prompt void prompt() { cout<<"Please enter two integers:"; } ///create object use ctor Clock createObject() { prompt(); int h, m; cin>>h>>m; while (!test(h,m)) { cout<<"Error input."< 
           
             >h>>m; } return Clock(h,m); } class People { public: ///ctor People(); People(int, int); ~People(); ///look the current time void lookTime(); ///set a time for meetting void setMeetingTime(int h, int m); ///ask how long to meeting Clock howLong(); private: ///current time Clock time; ///meeting time Clock timing; }; People::People() { time.hour = time.minutes = timing.hour = timing.minutes = 0; } People::People(int h, int m) { time.hour = h; time.minutes = m; timing.hour = timing.minutes = 0; } People::~People() { } ///look the current time void People::lookTime() { time.showTime(); } ///set a time for meetting void People::setMeetingTime(int h, int m) { timing.hour = h; timing.minutes = m; } ///ask how long to meeting Clock People::howLong() { return (timing - time); } int main() { ///create a random srand((int)time(0)); int num = rand() % (24 * 60); People people(num / 60, num % 60); people.lookTime(); cout<<"Please enter your meeting time(two integers):"; int h, m; cin>>h>>m; people.setMeetingTime(h, m); Clock c = people.howLong(); cout<<"There are "< 
            
              <<" to meeting."< 
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   



接下来的改进:
1.写在一个文件中比较冗余,最好分开写。
2.一些方法的增加和删除,使代码更加简洁。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值