目录
一.运算符重载
1.1.“+”重载
成员函数实现方法:
类外友元函数实现方法:
1.2.“-”重载
成员函数实现方法:
类外友元函数实现方法:
1.3.“*”重载
成员函数实现方法:
类外友元函数实现方法:
1.4.“/”重载
成员函数实现方法:
类外友元函数实现方法:
二.输入输出流重载
2.1输入流重载:
2.2输出流重载:
三.成员函数实现方法和类外友元函数实现方法的完整代码
3.1成员函数实现方法:
3.2类外友元函数实现方法:
一.运算符重载
运算符重载一般有两种方法,分别是成员函数法和类外函数友元法。
成员函数法,顾名思义,就是将重载定义为类的函数成员;
类外友元函数法,就是利用类外单独的函数实现重载功能,并将函数和类建立起友元关系。
两种方法的显著区别就在于:
1.解决访问私有(保护)成员权限问题;
2.传入参数个数不同;(其实第二点区别派生于第一点)
下面对于每种常见的运算符的重载,都会给出两种方法的代码。
对于运算符重载,一般格式为:
1.类内成员函数:
class
{……
public:
类名 operator 将要重载的运算符 (类名同型的数据类型 &)
{
类名同型的数据类型 结果变量;
计算结果;
return 结果变量;
}
}
int main(){}
2.类外友元函数:
class
{
……
public:
……
friend 类名 operator 将要重载的运算符 (类名同型数据类型 &对象1,类名同型数据类型&对象2);
}
friend 类名 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&);
};
inline complex complex::operator +(complex &c)
{
complex temp;
temp.real = this->real + c.real;
temp.imag = this->imag + c.imag;
return temp;
}
int main()
{
complex a(1,2),b(2,3),c;
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&);
};
complex operator+(complex& a, complex& b)
{
complex temp;
temp.real = a.real + b.real;
temp.imag = a.imag + b.imag;
return temp;
}
int main()
{
complex a(1, 2), b(2, 3), c;
c = a + b;
c.showcomplex();
return 0;
}
1.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&);
};
inline complex complex::operator -(complex& c)
{
complex temp;
temp.real = this->real - c.real;
temp.imag = this->imag - c.imag;
return temp;
}
int main()
{
complex a(1, 2), b(2, 3), c;
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&);
};
complex operator-(complex& a, complex& b)
{
complex temp;
temp.real = a.real - b.real;
temp.imag = a.imag - b.imag;
return temp;
}
int main()
{
complex a(1, 2), b(2, 3), c;
c = a - b;
c.showcomplex();
return 0;
}
1.3.“*”重载
成员函数实现方法:
#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&);
};
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;
}
int main()
{
complex a(1, 2), b(2, 3), c;
c = a * b;
c.showcomplex();
return 0;
}
类外友元函数实现方法:
#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&);
};
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;
}
int main()
{
complex a(1, 2), b(2, 3), c;
c = a * b;
c.showcomplex();
return 0;
}
1.4.“/”重载
成员函数实现方法:
#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;
}
complex operator /(complex&);
};
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()
{
complex a(1, 2), b(2, 3), c;
c = a / b;
c.showcomplex();
complex d(5, 6), e(0, 0), f;
f = d / e;
f.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&);
};
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()
{
complex a(1, 2), b(2, 3), c;
c = a / b;
c.showcomplex();
complex d(5, 6), e(0, 0), f;
f = d / e;
f.showcomplex();
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;
}
三.成员函数实现方法和类外友元函数实现方法的完整代码
3.1成员函数实现方法:
#include <iostream>
using namespace std;
class complex
{
private:
double real, imag;
public:
complex(double real = 0, double imag = 0)
{
this->real = real;
this->imag = imag;
}
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&);
//输入输出流重载
friend istream& operator>>(istream&, complex&);
friend ostream& operator<<(ostream&, 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.imag == 0 && b.real == 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;
}
}
istream& operator>>(istream& input, complex& c)
{
cout << "请分别输入一个复数的实部和虚部:" << endl;
input >> c.real >> c.imag;
return input;
}
ostream& operator<<(ostream& output, complex& c)
{
output << "(" << 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;
}
3.2类外友元函数实现方法:
#include <iostream>
using namespace std;
class complex
{
private:
double real, imag;
public:
complex(double real = 0, double imag = 0)
{
this->real = real;
this->imag = imag;
}
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&);
//输入输出流重载
friend istream& operator>>(istream&, complex&);
friend ostream& operator<<(ostream&, 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.imag==0&&b.real==0)
{
cout << "error" << endl;
exit(0);
}
else
{
complex temp;
double under = b.real * b.real + b.imag * a.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;
}
}
istream& operator>>(istream& input, complex& c)
{
cout << "请分别输入一个复数的实部和虚部:" << endl;
input >> c.real >> c.imag;
return input;
}
ostream& operator<<(ostream& output, complex& c)
{
output << "(" << 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;
}