C ++ allowed to be overloaded operator is different from the C language is a major optimization function, but it also has its own limitations, let us together look!
Experiment 1: define a plurality of class Complex, overloading, "+" operator, so that it can be used to increase since i.e. real and imaginary part of the complex respectively incremented and the member function implemented; Overload "+" and "- "operator, so that it can be used for addition and subtraction of a plurality of complex, the UF element function implementation.
- Overloaded "+" operator: with member functions to achieve:
#include <iostream>
using namespace std;
class Complex
{
private:
int real,imag;
public:
Complex(){real=0;imag=0;}//无参构造函数
Complex(int r,int i){real=r;imag=i;}//有参构造函数
void print()
{
cout<<real;
if(imag>=0)
{cout<<"+";}
cout<<imag<<"i"<<endl;
}
//前置自增
Complex operator++()
{
Complex c;
c.real=++(this->real);
c.imag=++imag;
return c;
}
//后置自增
Complex operator++(int)
{
/*Complex c;
c.real=(this->real)++;
c.imag=imag++;
return c;*/
Complex c=*this;
real++;
imag++;
return c;
}
};
int main()
{
Complex c1(1,1),c2(2,-2),c3,c4,c5;
c1.print();
c3.print();
c4=c2++;
c5=++c1;//成员函数实现 c1.operator++() this
c4.print();
c5.print();
return 0;
}
- Overloaded "+" and "-" operator: UFIDA member function to achieve:
#include <iostream>
using namespace std;
class Complex
{
private:
int real,imag;
public:
Complex(){real=0;imag=0;}//无参构造函数
Complex(int r,int i){real=r;imag=i;}//有参构造函数
friend Complex operator-(Complex cc1,Complex cc2)//c4=c1-c2
{
Complex c;
c.real=cc1.real-cc2.real;
c.imag=cc1.imag-cc2.imag;
return c;
}
/*
friend Complex operator-(Complex &cc1,Complex &cc2)//c4=c1-c2
{
return Complex(cc1.real-cc2.real ,cc1.imag -cc2.imag );
}
*/
friend Complex operator+(Complex cc1,Complex cc2)//c5=c1+c2
{
Complex c;
c.real=cc1.real+cc2.real;
c.imag=cc1.imag+cc2.imag;
return c;
}
void print()
{
cout<<real;
if(imag>=0){cout<<"+";}
cout<<imag<<"i"<<endl;
}
};
int main()
{
Complex c1(1,1),c2(2,-2),c3,c4,c5;
c1.print();
c3.print();
c4=c1-c2;//operator-(c1,c2);
c4.print();
c5=c1+c2;//operator+(c1,c2);
c5.print();
return 0;
}
Experiment 2:
programming, Overload operator "+" and "-", two of the matrices of addition, subtraction.
#include <iostream>
using namespace std;
int i,j;
class Matrix
{
private:
int matrix[3][3];
public:
void show();
Matrix();//无参构造函数
Matrix operator + (Matrix &m1);
Matrix operator - (Matrix &m1);
};
Matrix::Matrix()
{
for(i=0;i<3;i++)
for(j=0;j<3;j++)
matrix[i][j]=rand()%20;
}
Matrix Matrix::operator+(Matrix &m1)
{
Matrix tmp;
for( i=0;i<3;i++)
for( j=0;j<3;j++)
tmp.matrix [i][j]=matrix[i][j]+m1.matrix [i][j];
return tmp;
}
Matrix Matrix::operator-(Matrix &m1)
{
Matrix tmp;
for( i=0;i<3;i++)
for( j=0;j<3;j++)
tmp.matrix [i][j]=matrix[i][j]-m1.matrix [i][j];
return tmp;
}
void Matrix::show()
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<matrix[i][j]<<'\t';
cout<<endl;
}
cout<<endl;
}
void main()
{
Matrix m1,m2,m3,m4;
m3=m1+m2;
m4=m1-m2;
m1.show ();
m2.show();
cout<<"Matrix m1+m2="<<endl;
m3.show ();
cout<<"Matrix m1-m2="<<endl;
m4.show ();
return ;
}