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
请按任意键继续. . .
相关推荐
bkspiderx6 小时前
C++操作符优先级与结合性全解析
c++·思维导图·操作符优先级·结合性
楼田莉子6 小时前
基于Linux的个人制作的文件库+标准输出和标准错误
linux·c语言·c++·学习·vim
数据门徒6 小时前
《人工智能现代方法(第4版)》 第6章 约束满足问题 学习笔记
人工智能·笔记·学习·算法
im_AMBER6 小时前
weather-app开发手记 01 HTTP请求基础 | Axios GET 请求
笔记·网络协议·学习·计算机网络·http·axios
数据门徒6 小时前
《人工智能现代方法(第4版)》 第8章 一阶逻辑 学习笔记
人工智能·笔记·学习·算法
好奇龙猫6 小时前
【AI学习-comfyUI学习-第十四节-joycaption3课程工作流工作流-各个部分学习】
人工智能·学习
子夜江寒6 小时前
Python 学习-Day9-pandas数据导入导出操作
python·学习·pandas
繁华似锦respect6 小时前
单例模式出现多个单例怎么确定初始化顺序?
java·开发语言·c++·单例模式·设计模式·哈希算法·散列表
点云SLAM6 小时前
Decisive 英文单词学习
人工智能·学习·英文单词学习·雅思备考·decisive·起决定性的·果断的