c++运算符的重载

c++允许大部分运算符的重载

不允许重载的运算符(.  *  ::   sizeof  ?:)(就只有这五种不可以重载)

重载运算符又分为单目运算符,双目运算符,流插入运算符和流提取运算符

以下就是重载这几种运算符的简单的例子:

//#include<iostream>
//using namespace std;
//class String
//{
//public:
//	String(){ p = NULL;}
//	String(char * str);
//	friend bool operator > (String &string1, String &string2);
//	friend bool operator < (String &string1, String &string2);
//	friend bool operator == (String &string1, String &string2);
//	void display();
//private:
//	char *p;
//};
//String::String(char * str)
//{
//	p = str;
//}
//void String::display()
//{
//	cout << p;
//}
//
//
//bool operator > (String &string1, String &string2)
//{
//	if (strcmp(string1.p, string2.p) > 0)
//		return true;
//	else
//		return false;
//}
//bool operator < (String &string1, String &string2)
//{
//	if (strcmp(string1.p, string2.p) < 0)
//		return true;
//	else
//		return false;
//}
//bool operator == (String &string1, String &string2)
//{
//	if (strcmp(string1.p, string2.p) == 0)
//		return true;
//	else
//		return false;
//}
//void compare(String &string1, String &string2)
//{
//	if (operator > (string1,string2)==1)
//	{
//		string1.display(); cout << ">"; string2.display();
//
//	}
//	else 
//	if (operator < (string1, string2) == 1)
//	{
//		string1.display(); cout << "<"; string2.display();
//
//	}
//	else 
//	if (operator == (string1, string2) == 1)
//	{
//		string1.display(); cout << "=="; string2.display();
//
//	}
//	cout << endl;
//}
//int main()
//{
//	String string1("hello"), string2("book"), string3("compere"), string4("hi");
//	
//	compare(string1, string3);
//	compare(string2, string3);
//	compare(string3, string4);
//	compare(string1, string4);
//	return 0;
//
//
//}
//重载双目运算符


//
//#include<iostream>
//using namespace std;
//class Time
//{
//public:
//	Time(){ _minute = 0; _sec = 0;}
//	Time(int minute, int sec) :_minute(minute), _sec(sec){}
//	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 += 1;
//
//	}
//	return *this;
//}
//Time Time::operator++(int)
//{
//	Time temp(*this);
//	_sec++;
//	if (_sec >= 60)
//	{
//		_sec -= 60;
//		_minute+= 1;
//
//	}
//	return temp;
//
//}
//
//int main()
//{
//	Time time1(26,59),time2;
//	cout << "time1 :";
//	time1.display();
//	time1++;
//	cout << "++time1 :";
//	time1.display();
//	time2 = time1++;
//	cout << "time2 :" ;
//    time2.display();
//

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

//}
//重载单目运算符;








#include<iostream>
using namespace std;
class A
{
public:
	//A(){ _real = 0; _imag = 0; }
	//A(double real, double imag) :_real(real), _imag(imag){}
	//A operator + (A &c2);
	friend ostream&operator<<(ostream&, A&);
	friend istream&operator>>(istream&, A&);
private:
	double _real;
	double _imag;
};
//A A::operator+(A &c2)
//{
//	return A(_real + c2._real, _imag + c2._imag);
//}
ostream&operator<<(ostream&output, A&c)
{
	output << "(" << c._real;
	if (c._imag >= 0)output << "+";
	output << c._imag << "i)" << endl;
	return output;
} 

istream&operator>>(istream&input, A&c)
{
	cout <<"请输入实部和虚部:";
	input >> c._real >> c._imag;
	return input;
}
int main()
{
	//A c1(3, 4), c2(6,4), c3;
	//c3 = c2 + c1;
	//cout << c3;
	//return 0;
	A c1, c2;
	cin >> c1 >> c2;
	cout << "c1 =" << c1 << endl;
	cout << "c2 =" << c2 << endl;
	return 0;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值