目录
一.运算符重载
运算符重载一般有两种方法,分别是成员函数法和类外函数友元法。
成员函数法,顾名思义,就是将重载定义为类的函数成员;
类外友元函数法,就是利用类外单独的函数实现重载功能,并将函数和类建立起友元关系。
两种方法的显著区别就在于:
1.解决访问私有(保护)成员权限问题;
2.传入参数个数不同;(其实第二点区别派生于第一点)
下面对于每种常见的运算符的重载,都会给出两种方法的代码。
对于运算符重载,一般格式为:
1.类内成员函数:
class
{
……
public:
类名 operator 将要重载的运算符 (类名同型的数据类型 &)
{
类名同型的数据类型 结果变量;
计算结果;
return 结果变量;
}
}
int main(){}
2.类外友元函数:
class
{
……
public:
……
friend 类名 operator 将要重载的运算符 (类名同型数据类型 &对象1,类名同型数据类型&对象2);
}
类名 operator 将要重载的运算符 (类名同型数据类型 &对象1,类名同型数据类型&对象2){
类名同型的数据类型 结果变量;
进行二元运算并将结果赋值给结果变量;
返回结果变量;
}
int main (){}
以下是具体例子:
1.1.“+”“-”“*”“/”重载
成员函数实现方法:
#include <iostream>
using namespace std;
class complex
{
protected:
double real, imag;
public:
complex(double a = 0, double b = 0)
{
this->real = a;
this->imag = b;
}
void showcomplex()
{
cout << "(" << this->real << "," << this->imag << "i)" << endl;
}
//重载运算符
complex operator+(complex&);
complex operator-(complex&);
complex operator*(complex&);
complex operator /(complex&);
};
inline complex complex::operator +(complex& c)
{
complex temp;
temp.real = this->real + c.real;
temp.imag = this->imag + c.imag;
return temp;
}
inline complex complex::operator -(complex& c)
{
complex temp;
temp.real = this->real - c.real;
temp.imag = this->imag - c.imag;
return temp;
}
inline complex complex::operator *(complex& c)
{
complex temp;
temp.real = this->real * c.real - this->imag * c.imag;
temp.imag = this->real * c.imag + this->imag * c.real;
return temp;
}
inline complex complex::operator/(complex& c)
{
if (c.real == 0 && c.imag == 0)
{
cout << "error" << endl;
exit(0);
}
else
{
complex temp;
double under = c.real * c.real + c.imag * c.imag;
temp.real = (this->real * c.real + this->imag * c.imag) / under;
temp.imag = (this->imag * c.real - this->real * c.imag) / under;
return temp;
}
}
int main()
{
system("color 0A");
complex a(1, 2), b(2, 3), c;
c = a + b;
c.showcomplex();
c = a - b;
c.showcomplex();
c = a * b;
c.showcomplex();
c = a / b;
c.showcomplex();
return 0;
}
类外友元函数实现方法:
#include<iostream>
using namespace std;
class complex
{
private:
double real, imag;
public:
complex(double a = 0, double b = 0)
{
this->real = a;
this->imag = b;
}
void showcomplex()
{
cout << "(" << this->real << "," << this->imag << "i)" << endl;
}
friend complex operator +(complex&, complex&);
friend complex operator -(complex&, complex&);
friend complex operator *(complex&, complex&);
friend complex operator /(complex&, complex&);
};
complex operator+(complex& a, complex& b)
{
complex temp;
temp.real = a.real + b.real;
temp.imag = a.imag + b.imag;
return temp;
}
complex operator-(complex& a, complex& b)
{
complex temp;
temp.real = a.real - b.real;
temp.imag = a.imag - b.imag;
return temp;
}
complex operator*(complex& a, complex& b)
{
complex temp;
temp.real = a.real * b.real - a.imag * b.imag;
temp.imag = a.real * b.imag + a.imag * b.real;
return temp;
}
complex operator/(complex& a, complex& b)
{
if (b.real == 0 && b.imag == 0)
{
cout << "error" << endl;
exit(0);
}
else
{
complex temp;
double under = b.real * b.real + b.imag * b.imag;
temp.real = (a.real * b.real + a.imag * b.imag) / under;
temp.imag = (a.imag * b.real - a.real * b.imag) / under;
return temp;
}
}
int main()
{
system("color 0A");
complex a(1, 2), b(2, 3), c;
c = a + b;
c.showcomplex();
c = a - b;
c.showcomplex();
c = a * b;
c.showcomplex();
c = a / b;
c.showcomplex();
return 0;
}
运行结果:
1.2“>”“<”重载
成员函数实现方法
#include<iostream>
using namespace std;
class student
{
private:
string name;
double scores;
public:
student()
{
this->name = "unknown name";
this->scores = 0;
}
student(string name, double scores)
{
this->name = name;
this->scores = scores;
}
void operator >(student& s)
{
cout << "operator>" << endl;
if (this->scores > s.scores)
cout << "true" << endl;
else
cout << "false" << endl;
}
void operator<(student& s)
{
cout << "operator<" << endl;
if (this->scores < s.scores)
cout << "true" << endl;
else
cout << "false" << endl;
}
};
int main()
{
system("color 0A");
student a("zhang", 100);
string name;
double scores;
cin >> name >> scores;
student b(name, scores);
a > b;
a < b;
return 0;
}
类外友元函数实现方法
#include<iostream>
using namespace std;
class student
{
private:
string name;
double scores;
public:
student()
{
this->name = "unknown name";
this->scores = 0;
}
student(string name, double scores)
{
this->name = name;
this->scores = scores;
}
friend void operator >(student& a, student& b);
friend void operator <(student& a, student& b);
};
void operator >(student& a, student& b)
{
cout << "operator>" << endl;
if (a.scores > b.scores)
cout << "true" << endl;
else
cout << "false" << endl;
}
void operator <(student& a, student& b)
{
cout << "operator>" << endl;
if (a.scores < b.scores)
cout << "true" << endl;
else
cout << "false" << endl;
}
int main()
{
system("color 0A");
student a("zhang", 100);
string name;
double scores;
cin >> name >> scores;
student b(name, scores);
a > b;
a < b;
return 0;
}
运行结果:
1.3“==”和“!=”的重载
成员函数实现方法
#include<iostream>
using namespace std;
class student
{
private:
int ID;
string name;
public:
student()
{
this->ID - 0;
this->name = "unknown name";
}
student(int ID, string name)
{
this->ID = ID;
this->name = name;
}
bool operator ==(student& s)
{
return ((this->ID == s.ID) && (this->name == s.name)) ? true : false;
}
bool operator !=(student& s)
{
return ((this->ID != s.ID) || (this->name != s.name)) ? true : false;
}
};
int main()
{
system("color 0A");
student a(1, "zhang");
int ID; string name;
while(1)
{
cin >> ID >> name;
student b(ID, name);
cout << "判断a==b:" << (a == b) << endl;
cout << "判断a!=b:" << (a != b) << endl;
}
return 0;
}
类外友元函数实现方法
#include<iostream>
using namespace std;
class student
{
private:
int ID;
string name;
public:
student()
{
this->ID - 0;
this->name = "unknown name";
}
student(int ID, string name)
{
this->ID = ID;
this->name = name;
}
friend bool operator ==(student&, student&);
friend bool operator !=(student&, student&);
};
bool operator ==(student& s1, student& s2)
{
return ((s1.ID == s2.ID) && (s1.name == s2.name)) ? true : false;
}
bool operator!=(student& s1, student& s2)
{
return ((s1.ID != s2.ID) || (s1.name != s2.name)) ? true : false;
}
int main()
{
system("color 0A");
student a(1, "zhang");
int ID; string name;
while(1)
{
cin >> ID >> name;
student b(ID, name);
cout << "判断a==b:" << (a == b) << endl;
cout << "判断a!=b:" << (a != b) << endl;
}
return 0;
}
二.输入输出流重载
输入输出流重载方法比较固定,一般格式为:
1.输入流重载:
class 类名
{
protected://或者private
……;
public:
……;
friend istream & operator >>(istream&,类名同型数据类型&);
}
istream& operator >>(istream& 变量名1,类名同型数据类型& 变量名2)
{
……;//实现特定功能的代码
return 变量名1;
}
2.输出流重载:
class 类名
{
protected://或者private
……;
public:
……;
friend ostream &operator <<(ostream&,类名同型数据类型&);
}
ostream& operator <<(ostream& 变量名1,类名同型数据类型& 变量名2)
{
……;//实现特定功能的代码
return 变量名2;
}
2.1输入流重载:
#include <iostream>
using namespace std;
class complex
{
protected:
double real, imag;
public:
complex(double a = 0, double b = 0)
{
this->real = a;
this->imag = b;
}
void showcomplex()
{
cout << "(" << this->real << "," << this->imag << "i)" << endl;
}
//重载运算符
complex operator+(complex&);
complex operator-(complex&);
complex operator*(complex&);
complex operator/(complex&);
//重载输入输出流
friend istream& operator >> (istream&, complex&);
};
inline complex complex::operator +(complex &c)
{
complex temp;
temp.real = this->real + c.real;
temp.imag = this->imag + c.imag;
return temp;
}
inline complex complex::operator-(complex& c)
{
complex temp;
temp.real = this->real - c.real;
temp.imag = this->imag - c.imag;
return temp;
}
inline complex complex::operator*(complex& c)
{
complex temp;
temp.real = this->real * c.real - this->imag * c.imag;
temp.imag = this->real * c.imag + this->imag * c.real;
return temp;
}
inline complex complex::operator/(complex& c)
{
complex temp;
double under = c.real * c.real + c.imag * c.imag;
if (under == 0)
{
cout << "error";
exit(0);
}
temp.real = (this->real * c.real + this->imag * c.imag) / under;
temp.imag = (this->imag * c.real - this->real * c.imag) / under;
return temp;
}
istream& operator >> (istream& input, complex& c)
{
cout << "请分别输入一个复数的实部和虚部:" << endl;
input >> c.real >> c.imag;
return input;
}
int main()
{
complex a, b, c;
cin >> a >> b;
c = a + b; c.showcomplex();
c = a - b; c.showcomplex();
c = a * b; c.showcomplex();
c = a / b; c.showcomplex();
return 0;
}
2.2输出流重载:
#include <iostream>
using namespace std;
class complex
{
protected:
double real, imag;
public:
complex(double a = 0, double b = 0)
{
this->real = a;
this->imag = b;
}
void showcomplex()
{
cout << "(" << this->real << "," << this->imag << "i)" << endl;
}
//重载运算符
complex operator+(complex&);
complex operator-(complex&);
complex operator*(complex&);
complex operator/(complex&);
//重载输入输出流
friend istream& operator >> (istream&, complex&);
friend ostream& operator<<(ostream&, complex&);
};
inline complex complex::operator +(complex &c)
{
complex temp;
temp.real = this->real + c.real;
temp.imag = this->imag + c.imag;
return temp;
}
inline complex complex::operator-(complex& c)
{
complex temp;
temp.real = this->real - c.real;
temp.imag = this->imag - c.imag;
return temp;
}
inline complex complex::operator*(complex& c)
{
complex temp;
temp.real = this->real * c.real - this->imag * c.imag;
temp.imag = this->real * c.imag + this->imag * c.real;
return temp;
}
inline complex complex::operator/(complex& c)
{
complex temp;
double under = c.real * c.real + c.imag * c.imag;
if (under == 0)
{
cout << "error";
exit(0);
}
temp.real = (this->real * c.real + this->imag * c.imag) / under;
temp.imag = (this->imag * c.real - this->real * c.imag) / under;
return temp;
}
istream& operator >> (istream& input, complex& c)
{
cout << "请分别输入一个复数的实部和虚部:" << endl;
input >> c.real >> c.imag;
return input;
}
ostream& operator<<(ostream& output, complex& c)
{
cout << "(" << c.real << "," << c.imag << "i)" << endl;
return output;
}
int main()
{
complex a, b, c;
cin >> a >> b;
c = a + b; cout << c;
c = a - b; cout << c;
c = a * b; cout << c;
c = a / b; cout << c;
return 0;
}