C++ Prime Plus 学习笔记025

书籍:C++ Primer Plus (第六版)(中文版)

工具:Dev-C++ 5.11

电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz

系统类型:64位操作系统,基于X64的处理器 windows10 专业版

第11章 使用类

11.1 运算符重载

11.2 计算时间:一个运算符重载实例

实例11.1

mytime0.h

cpp 复制代码
#ifndef MYTIME0_H_
#define MYTIME0_H_

class Time
{
	private:
		int hours;
		int minutes;
	public:
		Time();
		Time(int h,int m=0);
		void AddMin(int m);
		void AddHr(int h);
		void Reset(int h=0,int m=0);
		Time Sum(const Time & t) const;
		void Show() const;
};
#endif

mytime0.cpp

cpp 复制代码
#include <iostream>
#include "mytime0.h"

Time::Time()
{
	hours=minutes=0;
}

Time::Time(int h,int m)
{
	hours=h;
	minutes=m;
}

void Time::AddMin(int m)
{
	minutes +=m;
	hours += minutes/60;
}

void Time::AddHr(int h)
{
	hours += h;
}

void Time::Reset(int h,int m)
{
	hours = h;
	minutes = m;
}

Time Time::Sum(const Time & t) const
{
	Time sum;
	sum.minutes=minutes+t.minutes;
	sum.hours=hours+t.hours+sum.minutes/60;
	sum.minutes %=60;
	return sum;	
}

void Time::Show() const
{
	std::cout<<hours<<" hours, "<<minutes<<" minutes.\n";
}

usemytime0.cpp

cpp 复制代码
#include <iostream>
#include "mytime0.h"

int main()
{
	using std::cout;
	using std::endl;
	Time planning;
	Time coding(2,40);
	Time fixing(5,55);
	Time total;
	
	cout<<"Planning time = ";
	planning.Show();
	cout<<endl;
	
	cout<<"coding time = ";
	coding.Show();
	cout<<endl;
	
	cout<<"fixing time = ";
	fixing.Show();
	cout<<endl;	
	
	total = coding.Sum(fixing);
	cout<<"coding.Sum(fixing)= ";
	total.Show();
	cout<<endl;
	
	return 0;
}

编译运行结果:

cpp 复制代码
Planning time = 0 hours, 0 minutes.

coding time = 2 hours, 40 minutes.

fixing time = 5 hours, 55 minutes.

coding.Sum(fixing)= 8 hours, 35 minutes.


--------------------------------
Process exited after 1.519 seconds with return value 0
请按任意键继续. . .

实例11.2

mytime1.h

cpp 复制代码
#ifndef MYTIME1_H_
#define MYTIME1_H_

class Time
{
	private:
		int hours;
		int minutes;
	public:
		Time();
		Time(int h,int m=0);
		void AddMin(int m);
		void AddHr(int h);
		void Reset(int h=0,int m=0);
		Time operator+(const Time & t) const;
		void Show() const;
};
#endif

mytime1.cpp

cpp 复制代码
#include <iostream>
#include "mytime1.h"

Time::Time()
{
	hours=minutes=0;
}

Time::Time(int h,int m)
{
	hours=h;
	minutes=m;
}

void Time::AddMin(int m)
{
	minutes +=m;
	hours += minutes/60;
	minutes %=60;
}

void Time::AddHr(int h)
{
	hours += h;
}

void Time::Reset(int h,int m)
{
	hours = h;
	minutes = m;
}

Time Time::operator+(const Time & t) const
{
	Time sum;
	sum.minutes=minutes+t.minutes;
	sum.hours=hours+t.hours+sum.minutes/60;
	sum.minutes %=60;
	return sum;	
}

void Time::Show() const
{
	std::cout<<hours<<" hours, "<<minutes<<" minutes.\n";
}

usemytime1.cpp

cpp 复制代码
#include <iostream>
#include "mytime1.h"

int main()
{
	using std::cout;
	using std::endl;
	Time planning;
	Time coding(2,40);
	Time fixing(5,55);
	Time total;
	
	cout<<"Planning time = ";
	planning.Show();
	cout<<endl;
	
	cout<<"coding time = ";
	coding.Show();
	cout<<endl;
	
	cout<<"fixing time = ";
	fixing.Show();
	cout<<endl;	
	
	total = coding + fixing;
	cout<<"coding + fixing = ";
	total.Show();
	cout<<endl;
	
	Time morefixing(3,28);
	cout<<" more fixing time = ";
	morefixing.Show();
	cout<<endl;
	total=morefixing.operator+(total);
	cout<<"morefixing.operator+(total) =  ";
	total.Show();
	cout<<endl;
	
	return 0;
}

编译运行结果:

cpp 复制代码
Planning time = 0 hours, 0 minutes.

coding time = 2 hours, 40 minutes.

fixing time = 5 hours, 55 minutes.

coding + fixing = 8 hours, 35 minutes.

 more fixing time = 3 hours, 28 minutes.

morefixing.operator+(total) =  12 hours, 3 minutes.


--------------------------------
Process exited after 0.5087 seconds with return value 0
请按任意键继续. . .

实例11.3

mytime2.h

cpp 复制代码
#ifndef MYTIME2_H_
#define MYTIME2_H_

class Time
{
	private:
		int hours;
		int minutes;
	public:
		Time();
		Time(int h,int m=0);
		void AddMin(int m);
		void AddHr(int h);
		void Reset(int h=0,int m=0);
		Time operator+(const Time & t) const;
		Time operator-(const Time & t) const;
		Time operator*(double n) const;
		void Show() const;
};
#endif

mytime2.cpp

cpp 复制代码
#include <iostream>
#include "mytime2.h"

Time::Time()
{
	hours=minutes=0;
}

Time::Time(int h,int m)
{
	hours=h;
	minutes=m;
}

void Time::AddMin(int m)
{
	minutes +=m;
	hours += minutes/60;
	minutes %=60;
}

void Time::AddHr(int h)
{
	hours += h;
}

void Time::Reset(int h,int m)
{
	hours = h;
	minutes = m;
}

Time Time::operator+(const Time & t) const
{
	Time sum;
	sum.minutes=minutes+t.minutes;
	sum.hours=hours+t.hours+sum.minutes/60;
	sum.minutes %=60;
	return sum;	
}

Time Time::operator-(const Time & t) const
{
	Time diff;
	int tot1,tot2;
	tot1=t.minutes+60*t.hours;
	tot2=minutes+60*hours;
	diff.minutes = (tot2-tot1)/60;
	diff.hours=(tot2-tot1)/60;
	return diff;	
}

Time Time::operator*(double mult) const
{
	Time result;
	long totalminutes=hours*mult*60+minutes*mult;
	result.hours=totalminutes/60;
	result.minutes=totalminutes%60;
	return result;	
}

void Time::Show() const
{
	std::cout<<hours<<" hours, "<<minutes<<" minutes.\n";
}

usemytime2.cpp

cpp 复制代码
#include <iostream>
#include "mytime2.h"

int main()
{
	using std::cout;
	using std::endl;
	Time weeding(4,35);
	Time waxing(2,47);
	Time total;
	Time diff;
	Time adjusted;
	
	cout<<"weeding time = ";
	weeding.Show();
	cout<<endl;
	
	cout<<"waxing time = ";
	waxing.Show();
	cout<<endl;
	
	cout<<"total work time = ";
	total = weeding+waxing;
	total.Show();
	cout<<endl;	
	
	diff = weeding - waxing;
	cout<<"weeding time - waxing time = ";
	diff.Show();
	cout<<endl;
	
	adjusted=total*1.5;
	cout<<" adjusted work time = ";
	adjusted.Show();
	cout<<endl;
	
	return 0;
}

编译运行结果:

cpp 复制代码
weeding time = 4 hours, 35 minutes.

waxing time = 2 hours, 47 minutes.

total work time = 7 hours, 22 minutes.

weeding time - waxing time = 1 hours, 1 minutes.

 adjusted work time = 11 hours, 3 minutes.


--------------------------------
Process exited after 0.5112 seconds with return value 0
请按任意键继续. . .

11.3 友元

实例11.4

mytime3.h

cpp 复制代码
#ifndef MYTIME3_H_
#define MYTIME3_H_
#include <iostream>

class Time
{
	private:
		int hours;
		int minutes;
	public:
		Time();
		Time(int h,int m=0);
		void AddMin(int m);
		void AddHr(int h);
		void Reset(int h=0,int m=0);
		Time operator+(const Time & t) const;
		Time operator-(const Time & t) const;
		Time operator*(double n) const;
		friend Time operator *(double m,const Time & t)
		{ return t*m;}
		friend std::ostream & operator<<(std::ostream & os,const Time & t);
};
#endif

mytime3.cpp

cpp 复制代码
#include <iostream>
#include "mytime3.h"

Time::Time()
{
	hours=minutes=0;
}

Time::Time(int h,int m)
{
	hours=h;
	minutes=m;
}

void Time::AddMin(int m)
{
	minutes +=m;
	hours += minutes/60;
	minutes %=60;
}

void Time::AddHr(int h)
{
	hours += h;
}

void Time::Reset(int h,int m)
{
	hours = h;
	minutes = m;
}

Time Time::operator+(const Time & t) const
{
	Time sum;
	sum.minutes=minutes+t.minutes;
	sum.hours=hours+t.hours+sum.minutes/60;
	sum.minutes %=60;
	return sum;	
}

Time Time::operator-(const Time & t) const
{
	Time diff;
	int tot1,tot2;
	tot1=t.minutes+60*t.hours;
	tot2=minutes+60*hours;
	diff.minutes = (tot2-tot1)/60;
	diff.hours=(tot2-tot1)/60;
	return diff;	
}

Time Time::operator*(double mult) const
{
	Time result;
	long totalminutes=hours*mult*60+minutes*mult;
	result.hours=totalminutes/60;
	result.minutes=totalminutes%60;
	return result;	
}

std::ostream & operator<<(std::ostream & os,const Time & t)
{
	os<<t.hours<<" hours, "<<t.minutes<<" minutes";
	return os;
}

usemytime3.cpp

cpp 复制代码
#include <iostream>
#include "mytime3.h"

int main()
{
	using std::cout;
	using std::endl;
	Time aida(3,35);
	Time tosca(2,48);
	Time temp;
	
	cout<<"Aida and Tosca:\n";
	cout<<aida<<"; "<<tosca<<endl;
	temp=aida+tosca;
	cout<<"Aida + Tosca: "<<temp<<endl;
	temp=aida*1.17;
	cout<<"Aida * 1.17: "<<temp<<endl;
	cout<<"10.0 * Tosca: "<<10.0*tosca<<endl;
	
	return 0;
}

编译运行结果:

cpp 复制代码
Aida and Tosca:
3 hours, 35 minutes; 2 hours, 48 minutes
Aida + Tosca: 6 hours, 23 minutes
Aida * 1.17: 4 hours, 11 minutes
10.0 * Tosca: 28 hours, 0 minutes

--------------------------------
Process exited after 0.521 seconds with return value 0
请按任意键继续. . .
相关推荐
一条破秋裤几秒前
【1115】【多线程】【笔记】
前端·chrome·笔记
AIgorithmGEEK5 分钟前
std::unordered_map 与 std::unordered_set:接口、算法技巧与权衡 (速查)
c++·算法·哈希表·负载因子·桶·unordered_map·unordered_set
神明不懂浪漫22 分钟前
【第八章】多线程(一)
java·开发语言·经验分享·笔记
一拳一个呆瓜2 小时前
【STL】头文件:<cmath>
c++·stl
星恒随风2 小时前
MySQL 从零入门(九):事务管理详解——ACID、COMMIT、ROLLBACK、SAVEPOINT 与四种隔离级别
linux·服务器·数据库·笔记·mysql
蒸蒸yyyyzwd2 小时前
秋招操作系统错题本
笔记·八股
草莓熊Lotso3 小时前
【C++ 基于微服务的即时通讯系统】ElasticSearch 用户数据管理实现
linux·数据库·c++·elasticsearch·微服务·架构
星恒随风3 小时前
MySQL 从零入门(十一):视图与用户权限管理详解
linux·服务器·数据库·笔记·mysql
慧都产品智能化3 小时前
Qt 开发 CAD/CAM 工序表的隐性成本分析
c++·qt·qt5·qt6.3
山楂树の3 小时前
Java 泛型:泛型类、泛型方法、泛型接口与通配符
java·开发语言·笔记