C++运算符的重载
Posted Alkaid:
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++运算符的重载相关的知识,希望对你有一定的参考价值。
目录
一.重载运算符加减乘除“+”“-“*”“/”
函数类型 operator 运算符名称(形参表)
对运算符的重载处理
例题1:复数类进行加减乘除
复数运算相关公式
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<iostream>
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;
例题2:求两个复数之和(c1+c2)、整数和复数之和(c+i或者i+c)
#include<iostream>
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<iostream>
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 类型名
实现转换的语句
例题:处理一个复数和一个double数相加的运算
#include<iostream>
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;
以上是关于C++运算符的重载的主要内容,如果未能解决你的问题,请参考以下文章