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
请按任意键继续. . .
相关推荐
冷雨夜中漫步3 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
半桔3 小时前
【IO多路转接】高并发服务器实战:Reactor 框架与 Epoll 机制的封装与设计逻辑
linux·运维·服务器·c++·io
HABuo4 小时前
【linux文件系统】磁盘结构&文件系统详谈
linux·运维·服务器·c语言·c++·ubuntu·centos
盐焗西兰花4 小时前
鸿蒙学习实战之路-Reader Kit修改翻页方式字体大小及行间距最佳实践
学习·华为·harmonyos
我在人间贩卖青春4 小时前
C++之多重继承
c++·多重继承
QiZhang | UESTC4 小时前
学习日记day76
学习
久邦科技4 小时前
20个免费电子书下载网站,实现电子书自由(2025持续更新)
学习
m0_736919105 小时前
C++代码风格检查工具
开发语言·c++·算法
Gain_chance5 小时前
34-学习笔记尚硅谷数仓搭建-DWS层最近一日汇总表建表语句汇总
数据仓库·hive·笔记·学习·datagrip
Gain_chance6 小时前
36-学习笔记尚硅谷数仓搭建-DWS层数据装载脚本
大数据·数据仓库·笔记·学习