C++-使用类(运算符重载)

使用类

(1)   运算符重载:

运算符重载将将重载的概念拓展到运算符上,允许赋予C++运算符多种含义。 C++允许将运算符重载拓展到用户定义的类型。

例如:将两个数组相加是一种常见的运算。 通常,需要使用for循环实现:

for (int i = 0; i < 20;i++)

    evening[i] = sam[i] + janet[i];

但在C++中,可以定义一个表示数组的类,并重载+运算符。

evening= sam + janet;   //将两个数组对象加起来

1.     mytime0.h

//mytime0.h -- 运算符重载前的Time类
#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_H_

2.	mytime0.cpp

//mytime0.cpp -- 实现Time方法
#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;
	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";
}

Sum()函数中,参数是引用,但返回类型却不是引用。 将参数声明为引用的目的是为了提高效率。 如果按值传递Time对象,代码的功能将相同,但传递引用,速度将更快,使用的内存将更少。

然而, 返回值不能是引用。 因为函数将创建一个新的Time对象(sum),来表示另外两个Time对象的和。 返回对象将创建对象的副本,而调用函数可以使用它。然而,如果返回类型为Time &,则引用的将是sum对象。但由于sum对象是局部变量,在函数结束时将被删除,因此引用将指向一个不存在的对象。 使用返回类型Time意味着程序将在删除sum之前构造它的拷贝,调用函数将得到该拷贝。

 

3.     usetime0.cpp

 

//usetime0cpp -- 使用Time类第一版
#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;
	std::cin.get();
	return 0;
}

 

1-1.         添加加法运算符:mytime1.h

将Time类转换为重载的加法运算符,只要将Sum()名称改为operator+()即可。

Time operator+(const Time & t) const;

 

//mytime1.h -- 运算符重载前的Time类
#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 operator+(const Time & t) const;
	void Show() const;
};
#endif // !MYTIME0_H_

1-2.         mytime1.cpp

//mytime1.cpp -- 实现Time方法
#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";
}

1-3.         usetime1.cpp

//usetime1.cpp -- 使用Time类第2版
#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;
	//total=coding + fixing; 编译器将使用相应运算符函数替换上述运算符:
	//total=coding.operator+(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;
	std::cin.get();
	return 0;
}

 

2-1. 其他重载运算符:mytime2.h

将两个时间相减或将时间乘以一个因子,这需要重载减法和乘法运算符。 这和重载加法运算符采用的技术相同,即创建operator-()和operator *()方法。 也就是说,将下面的原型添加到类声明中:

Time operator-(const Time & t) const;

Time operator*(double n) const;

 

 

 

 

 

//mytime2.h 
#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 operator+(const Time & t) const;
	Time operator-(const Time & t) const;
	Time operator*(double n) const;
	void Show() const;
};
#endif // !MYTIME0_H_

2-2. mytime2.cpp

//mytime2.cpp -- 实现Time方法
#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";
}

2-3. usetime2.cpp

//usetime2.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;   //使用operator+()
	total.Show();
	cout << endl;
	diff = weeding - waxing;    //使用operator-()
	cout << "weeding time - waxing time = ";
	diff.Show();
	cout << endl;
	adjusted = total * 1.5;      //使用operator*()
	cout << "adjusted work time = ";
	adjusted.Show();
	cout << endl;
	std::cin.get();
	return 0;
}

 

 

 


 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值