类的学习--clock类

建立一个Clock类,并实现其中的成员函数。

class Clock {

public:

 Clock(){};//构造函数

 Clock(int h,int m,int s){};//有参数的构造函数

 void setclock(){}; //设置新的时间

 int gethour(){};//获取小时

 int getmin(){};//获取分钟

 int getsecd(){};//获取秒

 int secondbetween(){}; //返回与另一个时间对象的时间间隔,单位为秒

Clock  clockadd(int h,int m,int s){};

Clock  clocksub(int h,int m,int s){};

void showtime(){};

//重载运算符

Clock operator+=(int seconds);

Clock operator-=(int seconds);

Clock operator++();

Clock operator++(int);

Clock operator—();

Clock operator—(int);

Clock operator+(int seconds);

Clock operator-(int seconds);

 //重载运算符双目

friend long operator-(const Clock &T1,const Clock &T2);

 //时间大小比较

friend bool operator>(const Clock &T1,const Clock &T2);

friend bool operator<(const Clock &T1, const Clock &T2);

friend bool operator==(const Clock &T1,const Clock &T2);

friend bool operator>=(const Clock &T1,const Clock& T2);

friend bool operator<=(const Clock &T1, const Clock &T2);

friend bool operator!=(const Clock &T1, const Clock &T2);

private:

  int hour;

  int min;

  int sec;

}

并编写一个主程序,对Clock类的各个方法进行调用测试。

#include <iostream>
#include <cmath>
#include <stdlib.h>
using namespace std;

class Clock {
public:

	//默认构造函数
	Clock() { hour = 0, minute = 0, second = 0; };

	//带参数的构造函数
	Clock(int h, int m, int s);

	//复制构造函数
	Clock(const Clock& C);

	//析构函数
	~Clock() {};

	//设置时间
	void setclock(int newh, int newm, int news) {

		hour = newh, minute = newm, second = news;
		if (newh >= 12 || newm >= 60 || news >= 60)
		{
			cout << "您输入的数据不合法!" << endl;
			cout << "时钟重置为00:00:00" << endl;
			hour = 0, minute = 0, second = 0;
		}
	};

	int gethour() { return hour; };
	int getminute() { return minute; };
	int getsecond() { return second; };

	//时钟转换为秒
	int TotalSecond();

	void showtime() {
		cout << hour << ":" << minute << ":" << second << endl;
	};

	//返回与另一个时间对象的时间间隔,单位为秒
	int secondbetween(Clock c);

	//返回值为Clock类型 add 两个时钟的和 sub 两个时钟的差
	Clock clockadd(int h, int m, int s);
	Clock clocksub(int h, int m, int s);

	//重载运算符
	Clock operator += (int seconds);
	Clock operator -= (int seconds);
	Clock operator ++();  //前置++
	Clock operator ++(int);  //后置++
	Clock operator --();
	Clock operator --(int);
	Clock operator +(int seconds);
	Clock operator -(int seconds);
	

	//friend友元函数
	//重载运算符双目 
	friend Clock operator -(const Clock& T1, const Clock& T2);

	//重载右移运算符
	friend ostream& operator<<(ostream& cout, Clock C);

	//时间大小比较
	friend bool operator >(const Clock& T1, const Clock& T2);
	friend bool operator <(const Clock& T1, const Clock& T2);
	friend bool operator ==(const Clock& T1, const Clock& T2);
	friend bool operator >=(const Clock& T1, const Clock& T2);
	friend bool operator <=(const Clock& T1, const Clock& T2);
	friend bool operator !=(const Clock& T1, const Clock& T2);
	
private:
	int hour, minute, second;
};


Clock::Clock(int h, int m, int s) {
	hour = h, minute = m, second = s; 
	if (h >= 12 || m >= 60 || s >= 60)
	{
		cout << "您输入的数据不合法!" << endl;
		cout << "时钟重置为00:00:00" << endl;
		hour = 0, minute = 0, second = 0;
	}
}
//构造函数不可指定返回值类型 且无返回值
Clock::Clock(const Clock& C) {
	hour = C.hour;
	minute = C.minute;
	second = C.second;
}

int Clock::TotalSecond() {
	int t;
	t = hour * 60 * 60 + minute * 60 + second;
	return t;
}

int Clock::secondbetween(Clock c) {
	int t1, t2;
	t1 = this->hour * 60 * 60 + this->minute * 60 + this->second;
	t2 = c.hour * 60 * 60 + c.minute * 60 + c.second;
	//cout << "时间间隔:"<< abs(t1 - t2) << endl;
	return abs(t1 - t2);
}

ostream& operator<<(ostream& cout, Clock C) {
	cout << C.hour << ":" << C.minute << ":" << C.second;
	return cout;
}

Clock Clock::clockadd(int h, int m, int s) {
	int tm = minute, ts = second;
	if ((s + second) >= 60)
	{
		second = (s + second) % 60;
		minute += ((s + ts) / 60);
		tm = minute;
	}
	else
		second += s ;
	if ((m + minute) >= 60) {
		minute = (m + minute) % 60;
		hour += (m + tm) / 60;
	}
	else
		minute += m;
	if ((hour + h) >= 12) {
		hour = (hour + h) % 12;
	}
	else
		hour += h;

	return * this;
}

Clock Clock::clocksub(int h, int m, int s) {
	int th = h, tm = m, ts = s % 60;

	//运算符模式 把秒转换为时钟
	if ((h == 0) & (m == 0) & (s > 60)) {
		tm = s / 60;
		//大于60分钟
		if ((s / 60) >= 60) { 
			tm = (s / 60) % 60;
			th = s / 3600;
			//大于12小时
			if (((s / 60) / 60) >= 12) {
				th = (s / 3600) % 12;
			}	
		}
		//cout << "转换后:";
		//cout << th << ":" << tm << ":" << ts << endl;
    }

	if ((second - ts) < 0) {
		second = second - ts + 60;
		minute -= 1;
	}
	else second -= ts;
	if ((minute - tm) < 0) {
		minute = minute - tm + 60;
		hour -= 1;
	}
	else
		minute -= tm;
	if ((hour - th) < 0) {
		hour = hour + 12 - th;
	}
	else
		hour -= th;

	return * this;
}

Clock Clock:: operator += (int seconds) {
	return clockadd(0, 0, seconds);
}

Clock Clock::operator -= (int seconds) {
	return clocksub(0, 0, seconds);
}

Clock Clock:: operator + (int seconds) {
	Clock C = *this;
	return C.clockadd(0, 0, seconds);
}

Clock Clock::operator - (int seconds) {
	Clock C = *this;
	return C.clocksub(0, 0, seconds);
}

Clock operator -(const Clock& T1, const Clock& T2) {
	Clock C1 = T1, C2 = T2;
	C1.clocksub(T2.hour, T2.minute, T2.second);
	return C1;
}

Clock Clock::operator ++() {
	return clockadd(0, 0, 1);
}

Clock Clock::operator ++(int) {
	Clock C = *this;   //保存当前值
	clockadd(0, 0, 1); //当前值+1
	return C;          //返回没有+1的值
}

Clock Clock::operator --() {
	return clocksub(0, 0, 1);
}

Clock Clock::operator --(int) {
	Clock C = *this;
	clocksub(0, 0, 1);
	return C;
}

bool operator >(const Clock& T1, const Clock& T2) {
	Clock C1 = T1, C2 = T2;
	return C1.TotalSecond() > C2.TotalSecond();
}

bool operator <(const Clock& T1, const Clock& T2) {
	Clock C1 = T1, C2 = T2;
	return C1.TotalSecond() < C2.TotalSecond();
}

bool operator ==(const Clock& T1, const Clock& T2) {
	Clock C1 = T1, C2 = T2;
	return C1.TotalSecond() == C2.TotalSecond();
}

bool operator >=(const Clock& T1, const Clock& T2) {
	Clock C1 = T1, C2 = T2;
	return C1.TotalSecond() >= C2.TotalSecond();
}

bool operator <=(const Clock& T1, const Clock& T2) {
	Clock C1 = T1, C2 = T2;
	return C1.TotalSecond() <= C2.TotalSecond();
}

bool operator !=(const Clock& T1, const Clock& T2) {
	Clock C1 = T1, C2 = T2;
	return C1.TotalSecond() != C2.TotalSecond();
}

int main() {

	//构造函数
	Clock myclock;
	Clock youclock(0, 0, 0);
	Clock heclock(8, 30, 30);
	Clock herclock = heclock;
	myclock.setclock(7, 15, 0);

	herclock.setclock(4, 30, 30);

	//显示函数
	cout << "-----showtime函数显示时间------" << endl << endl;

	cout << "myclock的时间为:";
	myclock.showtime();
	cout << "youclock的时间为:";
	youclock.showtime();
	cout << "heclock的时间为:";
	heclock.showtime();
	cout << "herclock的时间为:";
	herclock.showtime();

	cout << endl << endl;

	//获取小时
	cout << "调用myclock.gethour()函数获取hour  "<<endl;
	cout << "myclock.hour = ";
	cout << myclock.gethour() << endl;

	//时钟转换秒
	cout << "调用herclock.TotalSecond()函数把herclock转换成秒  " << endl;
	cout << "herclock的时间转换为秒为:";
	cout << herclock.TotalSecond() << endl;

	//时间间隔
	cout << "调用myclock.secondbetween(youclock)函数获取myclock与youclock的时间间隔  " << endl;
	cout << "myclock和youclock的时间间隔为:" << myclock.secondbetween(youclock) << "秒" << endl;

	cout << endl << endl;

	//时钟加时钟
	cout << "-----clockadd函数实现时钟的加法------" << endl;

	cout << "heclock + 3:29:29 = ";
	heclock.clockadd(3, 29, 29);
	heclock.showtime();

	//时钟减时钟
	cout << "-----clocksub函数实现时钟的减法------" << endl;

	cout << "herclock - 4:30:32 = ";
	herclock.clocksub(4, 30, 32);
	herclock.showtime();

	cout << endl << endl;

	
	//运算符重载 单位为秒
	cout << "-----运算符重载------" << endl << endl;

	cout << "myclock的时间为:" << myclock << endl;
	cout << "youclock的时间为:" << youclock << endl;
	cout << "heclock的时间为:" << heclock << endl;
	cout << "herclock的时间为:" << herclock << endl << endl;
	
	//时钟 += 运算符
	cout << "********** += 运算符***********" <<endl << "myclock += 17101 的值为: ";
	myclock += 17101;
	myclock.showtime();

	//时钟 -= 运算符
	cout << "********** -= 运算符***********" << endl << "youclock -= 1  的值为:";
	youclock -= 1;
	youclock.showtime();

	cout << endl;

	//时钟自增1s
	cout << "**********自增自减运算符***********" << endl;
	cout << "++heclock = " << ++heclock << endl;
	cout << "heclock++ = " << heclock++ << endl;
	cout << "显示heclock : " << heclock << endl << endl;

	//时钟自减1s
	cout << "--youclock =" << --youclock << endl;
	cout << "youclock-- = " << youclock-- << endl;
	cout << "显示youclock :" << youclock << endl << endl;

	//时钟 + 运算符
	myclock = youclock + 183;
	cout << "********** + 运算符***********" << endl;
	cout << "myclock =  youclock + 183 = "<< youclock << " + 183 = " << myclock << endl;
	
	//时钟 - 运算符
	youclock = heclock - 350;
	cout << "********** - 运算符***********" << endl;
	cout << "youclock = heclock - 350 = " << heclock << " - 350 = " << youclock << endl;


	//双目 - 运算符
	myclock.setclock(11, 20, 30);
	cout << "重置myclock的时间为:" << myclock << endl;
	youclock.setclock(1, 12, 30);
	cout << "重置youclock的时间为:" << youclock << endl;
	cout << "********** - 双目运算符***********" << endl;
	cout << "myclock - youclock = " << myclock - youclock << endl;

	cout << endl << endl;

	//时间比较
	cout << "-----时间比较------" << endl << endl;

	heclock.setclock(8, 0, 0);
	herclock.setclock(8,30,0);
	cout << "heclock的时间为:" << heclock << endl;
	cout << "herclock的时间为:" << herclock << endl;
	cout << "比较heclock和herclock的大小:" << endl << endl;
	if (heclock > herclock)
		cout << "heclock大于herclock" << endl;
	if (heclock < herclock)
		cout << "heclock小于herclock" << endl;
	if (heclock >= herclock)
		cout << "heclock大于等于herclock" << endl;
	if (heclock <= herclock)
		cout << "heclock小于等于herclock" << endl;
	if (heclock != herclock)
		cout << "heclock不等于herclock" << endl;
	if (heclock == herclock)
		cout << "heclock等于herclock" << endl;

	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值