函数类型 operator 运算符名称(形参表)
{对运算符的重载处理}
1.加法运算
(a+bi)+(c+di)=(a+c)+(b+d)i
2.减法运算
(a+bi)-(c+di)=(a-c)+(b-d)i
3.乘法运算
(a+bi)(c+di)=(ac-bd)+(bc+ad)i
4.除法运算
(a+bi)/(c+di)=(ac + bd)/(c^2 + d ^2) +((bc - ad)/(c ^2 + d ^2)) i
#include
using namespace std;
class Complex {
public:
Complex(double a, double b) :real(a), imag(b) {}
void display();
double get_real();
double get_imag();
Complex operator+(Complex&);
Complex operator-(Complex&);
Complex operator*(Complex&);
Complex operator/(Complex&);
private:
double real;
double imag;
};
Complex Complex::operator+(Complex& c) {
return Complex(this->get_real() + c.get_real(), this->get_imag() + c.get_imag());
}
Complex Complex::operator-(Complex& c)
{
return Complex(this->get_real() - c.get_real(), this->get_imag() - c.get_imag());
}
Complex Complex::operator*(Complex& c)
{
return Complex(this->get_real() * c.get_real()- this->get_imag() * c.get_imag(), this->get_imag() * c.get_real()+this->get_real() * c.get_imag());
}
Complex Complex::operator/(Complex& c)
{
return Complex((real * c.get_real() + imag * c.get_imag())/(pow(c.get_real(),2)+(pow(c.get_imag(), 2))),
(imag * c.get_real() - real * c.get_imag()) / (pow(c.get_real(), 2) +(pow(c.get_imag(), 2))));
}
void Complex::display() {
cout << "(" << real << "," << imag << "i)" << endl;
}
double Complex::get_real() {
return real;
}
double Complex::get_imag() {
return imag;
}
int main() {
Complex c1(3, 4);
Complex c2(5, -10);
Complex c3 = c1 + c2;
c3.display();//(8,-6i)
c3 = c1 - c2;
c3.display();//(-2,14i)
c3 = c1 * c2;
c3.display();//(55,-10i)
c3 = c1 / c2;
c3.display();//(-0.2,0.4i)
return 0;
}
#include
using namespace std;
class Complex {
public:
Complex(double a, double b) :real(a), imag(b) {}
void display();
double get_real();
double get_imag();
Complex operator+(int &i);
Complex operator+(Complex &c);
friend Complex operator+(int &,Complex&);
private:
double real;
double imag;
};
double Complex::get_real() {
return real;
}
double Complex::get_imag() {
return imag;
}
Complex Complex::operator+(int& i) {
return Complex(real + i, imag);
}
Complex Complex::operator+(Complex &c) {
return Complex(real + c.get_real(), imag+c.get_imag());
}
void Complex::display() {
cout << "(" << real << "," << imag << "i)" << endl;
}
Complex operator+(int& i, Complex& c)
{
return Complex(c.get_real() + i, c.get_imag());
}
int main() {
Complex c1(3, 4);
Complex c2(5, -10);
int i = 5;
Complex c3 = c1 + c2;
c3.display();//(8,-6i)
c3 = c1 + i;
c3.display();//(8,4i)
c3 = i + c1;
c3.display();//(8,4i)
return 0;
}
只能作为友元函数,不能将它们定义为成员函数
【个人理解:统一写成非成员函数可能是怕原来的流运算符会调用类库里的 ,无法重载自己定义的】
istream& operator>>(istream &,自定义类 &);
ostream& operator<<(ostream &,自定义类 &);
#include
using namespace std;
class Matrix {
public:
Matrix();
friend ostream& operator<<(ostream& out,Matrix &m);
friend istream& operator>>(istream& input, Matrix& m);
friend Matrix operator+(Matrix& a, Matrix& b);
private:
int a[2][3];
};
//重载运算符“++”————矩阵的相加
Matrix operator+(Matrix& x, Matrix& z) {
Matrix t;
for (int i = 0;i < 2;i++) {
for (int j = 0;j < 3;j++) {
t.a[i][j] = x.a[i][j] + z.a[i][j];
}
}
return t;
}
//重载运算符“<<”————矩阵的输入
ostream& operator<<(ostream& out, Matrix& m) {
for (int i = 0;i < 2;i++) {
for (int j = 0;j < 3;j++) {
out << m.a[i][j] << " ";
}
out << endl;
}
return out;
}
//重载运算符“<<”————矩阵的输出
istream& operator>>(istream& in, Matrix& m) {
for (int i = 0;i < 2;i++) {
for (int j = 0;j < 3;j++) {
in >> m.a[i][j];
}
}
return in;
}
//构造函数
Matrix::Matrix() {
for (int i = 0;i < 2;i++) {
for (int j = 0;j < 3;j++) {
a[i][j] = 0;
}
}
}
int main() {
Matrix m1, m2, m3;
cin >> m1 >> m2;
m3 = m1 + m2;
cout << m3 << endl;
return 0;
}
将一个类的对象转换成另一类型的数据。
operator 类型名
{实现转换的语句}
#include
using namespace std;
class Complex {
public:
Complex() {
real = 0;
imag = 0;
}
Complex(double r) {
real = r;
imag = 0;
}
Complex(double a, double b) :real(a), imag(b) {}
void display();
double get_real();
double get_imag();
operator double() { return real; }//重载类型转换符
private:
double real;
double imag;
};
void Complex::display() {
cout << "(" << real << "," << imag << ")" << endl;
}
int main() {
Complex c1(3, 4), c2;
double d;
d = 2.5 + c1;
cout << d << endl;
c2 = Complex(d);
c2.display();
return 0;
}