c++ 运算符的重载

1.1对运算符重载的方法

运算符的重载方法是定义一个重载运算符的函数,运算符的重载实质上是函数的重载

1.2重载运算符的规则

  • c++只允许用户对已有的c++运算符进行重载
  • c++的绝大部分运算符允许被重载

不允许重载的运算符只有五个:
. (成员访问运算符)
*(成员指针访问运算符)
::(域运算符)
sizeof(长度运算符)
?:(条件运算符)

  • 重载不能改变运算符运算对象(操作数)的个数
  • 重载不能改变运算符的优先级别
  • 重载不能改变运算符的结合性
  • 重载的运算符,其参数至少应有一个类对象(或类对象的引用)

这句话比较难以理解,之后会有详细讲解

1.3运算符重载函数作为类成员和函数和友元函数

对运算重符重载的函数有两种处理方式:
1.运算符重载的函数作为类的成员函数
2.运算符重载的函数作为普通函数,在类中定义为友元函数

eg.加法的复数相加重载 的友元写法

//运算符重载函数作友元函数
#include<iostream>
using namespace std;
class Complex
{
public:
	Complex() { real = 0; imag = 0; }
	
	Complex(double r, double i) { real = r, imag = i; }
	//声明为友元函数
	friend Complex operator+(Complex& c1, Complex& c2);
	void display()
	{
		cout << "(" << real << "," << imag << "i)" << endl;
	}
private:
	double real;
	double imag;
};
//在类外定义重载函数
Complex operator+(Complex& c1, Complex& c2)
{
	return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
int main()
{
	Complex c1(3, 4), c2(5, -10), c3;
	c3 = c1 + c2;
	cout << "c1=";
	c1.display();

	cout << "c2=";
	c2.display();

	cout << "c1+c2=";
	c3.display();
}

1.4各种运算符的重载

1.4.1双目运算符的重载

双目运算符
有两个操作数(参与运算的数据),通常在运算符的左右两侧。

eg.将 >,==重载为字符串比较

#include<iostream>
#include<string>
using namespace std;
class String
{
public:
	String() {  }
	String(string str);
	friend bool operator>(String string1, String string2);
	friend bool operator==(String string1, String string2);
	void display()
	{
		cout << p;
	}
private:
	string p;
};
String::String(string str)
{
	p = str;
}

bool operator>(String string1, String string2)
{
	if (string1.p > string2.p)
		return true;
	else return false;
}
bool operator==(String string1, String string2)
{
	if (string1.p == string2.p)
		return true;
	else return false;
}

int main()
{
	String string1("HELLO"), string2("HELLA");
	cout << (string1 > string2) << endl;
	cout << (string1 == string2) << endl;
	
}

1.4.2单目运算符的重载

单目运算符
有一个操作数,只有一个参数

eg.利用"++"模拟秒表

#include<iostream>
using namespace std;
class Time
{
public:
	Time() { minute = 0; sec = 0;}
	Time(int m,int s):minute(m),sec(s){}
	Time operator++();
	void display()
	{
		cout << minute << ":" << sec << endl;
	}
private:
	int minute;
	int sec;
};
Time Time::operator++()
{
	if (++sec >= 60)
	{
		sec -= 60;//满60秒就进一秒
		++minute;
	}
	return *this;
}
int main()
{
	Time time1(30, 0);

	for (int i = 0; i <= 60; i++)
	{
		++time1;
		time1.display();
		return 0;
	}
}

其实有的读者注意到,"++“和”-- "有前置和后置两种,他们的作用是不同的,那么当我们在一个程序中使用他们时,我们该如何区别呢?
c++约定:在自增和自减运算符的重载函数中,增加一个int型形参,就是后置自增(自减)运算符函数。

#include<iostream>
using namespace std;
class Time
{
public:
	Time() { minute = 0; sec = 0; }
	Time(int m,int s):minute(m),sec(s){}
	Time operator++();
	Time operator++(int);
	void display()
	{
		cout << minute << ":" << sec << endl;
	}
private:
	int minute;
	int sec;
};
Time Time::operator++()
{
	if (++sec >= 60)
	{
		sec -= 60;
		++minute;
		return *this;
	}
}
Time Time::operator++(int)//这个int只是为了区别于前置,无特别作用
{
	Time temp(*this);
	sec++;
	if (sec >= 60)
	{
		sec -= 60;
		++minute;
	}
	return temp;
}
int main()
{
	Time time1(34, 59), time2;

	cout << "time1:";
	time1.display();//time1:34:59 (time1原值)

	++time1;
	cout << "++time1:";
	time1.display();//time:35:0 (执行++time1后time1的值)

	time2 = time1++;
	cout << "time1++:";
	time1.display();//time:35:1 (再执行time1++后time的值)

	cout << "time2:";
	time2.display();//time:35:0 (time2保存的是执行time1之前的time1的值)

}

1.4.3重载流插入符“<<”

c++的流插入运算符和流提取运算符都是c++编译系统在类库中提供的
cin与cout分别是istream类和ostream类的对象。

用户对"<<"和”>>“重载的函数形式ostream如下:
istream & operator >> (istream & ,自定义类 &);
ostream & operator << (ostream & ,自定义类 &);

这里需要注意:
流运算符的重载函数只能作友元函数。而不能将他们定义为成员函数

#include<iostream>
using namespace std;
class Complex
{
public:
	Complex() { real = 0; imag = 0; }

	Complex(double r,double i):real(r),imag(i){}
	Complex operator+(Complex& c2);
	friend ostream& operator<< (ostream&, Complex&);

private:
	double real;
	double imag;
};
Complex Complex::operator+(Complex& c2)
{
	return Complex(real + c2.real, imag + c2.imag);
}
ostream& operator<<(ostream& output, Complex& c)
{
	output << "(" << c.real << "+" << c.imag << "i)" << endl;
	return output;
}
int main()
{
	Complex c1(2, 4), c2(6, 10), c3;
	c3 = c1 + c2;
	cout << c3;
	return 0;
}

1.4.4重载流提取符“>>”

#include<iostream>
using namespace std;
class Complex
{
public:
	friend ostream& operator<<(ostream&, Complex&);
	friend istream& operator>>(istream&, Complex&);
private:
	double real;
	double imag;
};
ostream& operator<<(ostream& output, Complex& c)
{
	output << "(" << c.real << "+" << c.imag <<"i)"<< endl;
	return output;
}
istream& operator>>(istream& input, Complex& c)
{
	cout << "input real part and imag part of complex number:";
	input >> c.real >> c.imag;
	return input;
}

int main()
{
	Complex c1, c2;
	cin >> c1 >> c2;
	cout << "c1=" << c1 << endl;
	cout << "c2=" << c2 << endl;
	return 0;
}

1.5不同类型数据之间的转换

1.5.1标准类型数据间的转换

c++提供显式类型转换,可以指定将一种指定的数据转换成另一种类型
格式:
类型名(数据)

1.5.2把其他类型函数转换为类对象

转换构造函数
Complex(double r){real=r;imag=0;}

归纳起来:
1)声明一个类(例如上面的Complex
2)在这个类中定义只有一个参数的构造函数,参数的类型是需要转换的类型,在函数体中指定转换的方法。
3)在该类的作用域内可以用以下进行类型转换

1.5.3把类对象转化为其他类型函数

类型转换函数
oprator 类型名()
{实现转换的函数}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ornamrr

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值