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
请按任意键继续. . .
相关推荐
yue20040310 小时前
Spring IoC 与 DI 核心概念与原理笔记
java·笔记·spring
wuminyu10 小时前
专家视角看Java多态性的底层基石vtable(虚函数表)构建过程解析
java·linux·c语言·jvm·c++
charlie11451419110 小时前
现代Qt开发教程(新手篇)1.10——进程
开发语言·c++·qt·学习
绿豆人10 小时前
Cache缓存项目学习2
学习·缓存
山楂树の10 小时前
H.265 (HEVC) 视频解码转逐帧图像 完整实现方案
学习·音视频·h.265
星幻元宇VR10 小时前
VR观景台推动安全科普走向沉浸体验
科技·学习·安全·vr·虚拟现实
码途漫谈10 小时前
Easy-Vibe开发篇阅读笔记(十三)——附录之用 Dify 搭建知识库问答系统
笔记·ai·开源·ai编程
十安_数学好题速析10 小时前
【多选】成比之道:巧解三角形中比例综合
笔记·学习·高考
wj30558537810 小时前
CMake 项目切换 Ninja 构建问题排查记录
c++
嵌入式小企鹅10 小时前
RISC-V车规专委会成立、AI模型集中开源、半导体产能加速爬坡
人工智能·学习·ai·程序员·算力·risc-v·半导体