运算符重载(超详细)

运算符重载

1.运算符重载是什么

使对象操作更美观的技术。
运算符重载是一种形式的c++多态。
要重载运算符,需使用被称为运算符函数的特殊函数形式。


运算符函数的格式如下:

operatop( argument-list)

例如,operator+() 重载+运算符
   operator*()重载*运算符
 
op必须是有效的c++运算符,不能虚构一个新的符号
 

2.运算符重载实例

我们将会对下面这个例子进行改造:

//ss.h
#ifndef MYTIME_H_
#define MYTIME_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
//file00.cpp
#include<iostream>
#include"ss.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
{
	/*不适用引用的原因,声明的sum为临时变量,如果用引用返回,
	在这个函数运行完后就会被删除,程序就会出现错误*/
	Time sum;
	sum.minutes = minutes + t.minutes;
	sum.hours = hours + t.hours;
	sum.hours += sum.minutes / 60;
	sum.minutes %= 60;
	return sum;
}

void Time::show() const
{
	std::cout << hours << "	hours	" << minutes << "	minutes" << std::endl;
}
//file01.cpp
#include<iostream>
#include"ss.h"

int main()
{
	using std::cout;
	
	Time plan(45,10);
	Time coding(12,45);
	Time total;
	total = plan.Sum(coding);
	total.show();
	return 0;
}

 
 
 
 
 
  下面是改造的结果:

//ss.h
#ifndef MYTIME_H_
#define MYTIME_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
//file00.cpp
#include<iostream>
#include"ss.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//此处发生变化
{
	/*不适用引用的原因,声明的sum为临时变量,如果用引用返回,
	在这个函数运行完后就会被删除,程序就会出现错误*/
	Time sum;
	sum.minutes = minutes + t.minutes;
	sum.hours = hours + t.hours;
	sum.hours += sum.minutes / 60;
	sum.minutes %= 60;
	return sum;
}

void Time::show() const
{
	std::cout << hours << "	hours	" << minutes << "	minutes" << std::endl;
}
//file01.cpp
#include<iostream>
#include"ss.h"

int main()
{
	using std::cout;
	
	Time plan(45,10);
	Time coding(12,45);
	Time total;
	total = plan + coding;
	total.show();
	return 0;
}

3.运算符重载操作原理

operator+()函数的名称使得可以使用函数表示法或运算符表示法来调用它。
编译器将根据操作数的类型来确定如何做:

int a,b,c;
Time A,B,C;
c = a + b;//使用int情况
C = A + B;//使用定义的 +

如果将两个以上的对象相加时,会有什么结果
  比如:t1,t2,t3和t4都是Time的对象
  可以用

t4 = t1 + t2 + t3;

上述的语句就会被转化为:

t4 = t1.operator+(t2 + t3);
//之后会变成
t4 = t1.operator+(t2.operator+(t3));

4.重载限制

  1. 重载后的运算符至少有一个操作数时用户定义的类型。例如:不能将减法运算符(-)重载为计算两个double 值得和,而不是它们的差。

  2. 使用运算符时不能违反运算符原来的句法规则
    例如:
      不能将求模运算符(%)重载成使用一个操作数:
    int x;
    Time shiva;
    % x;    //无效的求模运算符
    % shiva;  //无效的重载操作符

  3. 不能修改运算符的优先级
    因此,重载加号运算符,则新的运算符与原来的加号具有相同的优先级。

  4. 不能创建新运算符
      例如不能定义operator**()函数来表示求幂。

  5. 以下运算符不能重载

  • sizeof
  • .   成员运算符
  • .*   成员指针运算符
  • ::   作用域解析运算符
  • ?:   条件运算符
  • typeid   一个RTTI运算符
  • const_cast   强制类型转换运算符
  • dynamic_cast   强制类型转换运算符
  • reinterpret_cast   强制类型转换运算符
  • static_cast   强制类型转换运算符
  1. 下列运算符只能通过成员函数进行重载
  • =:赋值运算符
  • ():函数调用运算符
  • []:下标运算符
  • ->:通过指针访问类成员的运算符

5.使用其他运算符具体实现

//ss.h
#ifndef MYTIME_H_
#define MYTIME_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
//file00.cpp
#include<iostream>
#include"ss.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
{
	/*不适用引用的原因,声明的sum为临时变量,如果用引用返回,
	在这个函数运行完后就会被删除,程序就会出现错误*/
	Time sum;
	sum.minutes = minutes + t.minutes;
	sum.hours = hours + t.hours;
	sum.hours += sum.minutes / 60;
	sum.minutes %= 60;
	return sum;
}

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

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

void Time::show() const
{
	std::cout << hours << "hours	" << minutes << "minutes" 
		<< std::endl << std::endl;
}
//file01.cpp
#include<iostream>
#include"ss.h"

int main()
{
	using std::cout;
	
	Time plan(45,10);
	Time coding(12,45);
	Time total;
	total = plan + coding;
	total.show();

	cout << "plan = ";
	plan.show();
	cout << "coding = ";
	coding.show();

	cout << "coding - plan = ";
	total = coding - plan;
	total.show();

	cout << "2 * plan = ";
	total = plan * 2;
	total.show();

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值