練習寫C++代碼(100)--實現簡單的時鐘類4

經過更改之後的代碼文件如下,基本沒有改動,只是分開幾個文件,以及寫了一個makefile。

這樣只要在當前目錄下輸入指令 make就可以編譯了。


///clock.h

#ifndef _CLOCK_H
#define _CLOCK_H

#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();
	
	friend class People;
	///overload operator
	friend Clock operator-(const Clock& c1, const Clock& c2);
	friend ostream& operator<<(ostream& os, const Clock& c);

private:
	int hour;
	int minutes;	
};

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;
};

#endif
///clock.cpp

#include "clock.h"
#include
using namespace std;
///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:"<= t2)
		temp = t1 - t2;
	else
		temp = t1 + 24 * 60 -t2;

	return Clock(temp / 60, temp % 60);
}

ostream& operator<<(ostream& os, const Clock& c)
{
	os<
#include
#include
#include"clock.h"
using namespace std;

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 "<



下一次學習用C++進行普通文件的讀寫。試着學習讀寫壓縮文件 *.zip。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章