实质:重载的实质就是写一个重载函数语法:函数类型 operator 运算符名称 {对运算符的重载处理}说明:如果要对加号进行重载,则 “operator +” 就是函数名。
方法: (1)成员函数法 定义:把运算符重载的函数作为类的成员函数 说明:在该方法中,重载函数只有一个参数,因为由于重载函数是Complex类中的成员函数,因此有一个参数是隐含的,运算符函数是用this指针隐式访问类对象的成员。如:this –> real + c2.real。this –> real就是 c1.real。
(2)非成员函数法 定义:运算符重载的函数不是类的成员函数(可以是一个普通函数),放在类外,在类中把它声明为友元函数。 优点:代码看起来更加清晰。
例1: 通过重载实现复数相加(i.e. 对运算符 “+” 的重载)
法一:成员函数法
#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex(){real=
0;imag=
0;}
Complex(
double r,
double i){real=r;imag=i;}
Complex
operator + (Complex &c2);
void display();
};
Complex Complex::
operator +(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
void Complex::display()
{
cout<<
"("<<real<<
","<<imag<<
"i)"<<endl;}
int main()
{
Complex c1(
3,
4),c2(
5,-
10),c3;
c3=c1+c2;
cout<<
"c1="; c1.display();
cout<<
"c2="; c2.display();
cout<<
"c1+c2="; c3.display();
return 0;
}
输出:
法二:非成员函数法——友元函数法
#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex(
double r=
0,
double i=
0);
friend Complex
operator + (Complex c1,Complex c2);
void display();
};
Complex::Complex(
double r,
double i)
{
real=r;imag=i;
}
Complex
operator +(Complex c1,Complex c2)
{
return Complex(c1.real+c2.real,c1.imag+c2.imag);}
void Complex::display()
{
cout<<
"("<<real<<
","<<imag<<
"i)"<<endl;}
int main()
{
Complex c1(
3,
4),c2(
5,-
10),c3;
c3=c1+c2;
cout<<
"c1="; c1.display();
cout<<
"c2="; c2.display();
cout<<
"c1+c2="; c3.display();
return 0;
}
例2: 通过重载实现复数相乘(i.e. 对运算符 “*” 的重载)
这里用友元函数法来实现
#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex(
double r=
0,
double i=
0);
friend Complex
operator * (Complex c1,Complex c2);
void display();
};
Complex::Complex(
double r,
double i)
{
real=r;imag=i;
}
Complex
operator *(Complex c1,Complex c2)
{
Complex c;
c.real=c1.real*c2.real-c1.imag*c2.imag;
c.imag=c1.real*c2.imag+c1.imag*c2.real;
return c;
}
void Complex::display()
{
cout<<
"("<<real<<
","<<imag<<
"i)"<<endl;}
int main()
{
Complex c1(
3,
4),c2(
5,-
10),c3;
c3=c1*c2;
cout<<
"c1="; c1.display();
cout<<
"c2="; c2.display();
cout<<
"c1*c2="; c3.display();
return 0;
}
例3: 有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加(如c=a+b)
#include <iostream>
using namespace std;
class Matrix
{
public:
Matrix();
friend Matrix
operator+(Matrix ,Matrix );
void input();
void display();
private:
int mat[
2][
3];
};
Matrix::Matrix()
{
for(
int i=
0;i<
2;i++)
for(
int j=
0;j<
3;j++)
mat[i][j]=
0;
}
Matrix
operator+(Matrix a,Matrix b)
{
Matrix c;
for(
int i=
0;i<
2;i++)
for(
int j=
0;j<
3;j++)
{c.mat[i][j]=a.mat[i][j]+b.mat[i][j];}
return c;
}
void Matrix::input()
{
cout<<
"input value of matrix:"<<endl;
for(
int i=
0;i<
2;i++)
for(
int j=
0;j<
3;j++)
cin>>mat[i][j];
}
void Matrix::display()
{
for (
int i=
0;i<
2;i++)
{
for(
int j=
0;j<
3;j++)
{
cout<<mat[i][j]<<
" ";}
cout<<endl;
}
}
int main()
{
Matrix a,b,c;
a.input();
b.input();
cout<<endl<<
"Matrix a:"<<endl;
a.display();
cout<<endl<<
"Matrix b:"<<endl;
b.display();
c=a+b;
cout<<endl<<
"Matrix c = Matrix a + Matrix b :"<<endl;
c.display();
return 0;
}