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;
}