一、三维坐标类直接运算
#include<iostream>
using namespace std;
class point3d
{
float x,y,z;
public:
point3d(float a=0,float b=0,float c=0)
{x=a;y=b;z=c;}
point3d operator +(point3d const &a)
{
return point3d(a.x+x,a.y+y,a.z+z);
}
point3d operator =(point3d const &a)
{
return point3d(a.x,a.y,a.z);
}
point3d operator --()
{
x--;y--;z--;
return *this;
}
friend point3d operator --(point3d &,int);
friend bool operator !=(point3d const &,point3d const &);
void show() {cout<<"("<<x<<","<<y<<","<<z<<")\n";}
};
point3d operator --(point3d &a, int)
{
point3d temp=a;
--a.x;
--a.y;
--a.z;
return temp;
}
bool operator !=(point3d const &a,point3d const &b)
{
return a.x==b.x && a.y==b.y && a.z==b.z?false:true;
}
void main()
{
point3d a(3,4,5);
point3d b(4,5,6);
cout<<"a";
a.show();
cout<<"b";
b.show();
cout<<endl<<"c=a+b\tc";
point3d c=a+b;
c.show();
cout<<"a!=b?\t"<<(a!=b)<<endl;
b--;
cout<<"b--\tb";
b.show();
cout<<"a!=b?\t"<<(a!=b)<<endl;
}
二、线性表类运算符成员函数重载
#include<iostream>
using namespace std;
class line
{
float * list;
int max;
int num;
public:
line(int b=0, int c=0)
{
max=b;
num=c;
if(num>max)
cout<<"error, num must be smaller than max!"<<endl;
list= new float[max];
for(int i=0;i<num;i++)
{
cout<<"total "<<num<<" data, it is "<<i<<" now.\n";
cin>>*(list+i);
}
}
line(line &a)
{
max=a.max;
num=a.num;
list = new float[max];
for(int i=0;i<num;i++)
*(list+i)=*(a.list+i);
cout<<"copy constructor"<<endl;
}
~line() {delete []list;}
void show()
{
for(int i=0;i<num;i++)
cout<<*(list+i)<<'\t';
cout<<endl;
}
line operator +(line &b)
{
line temp;
temp.num=num+b.num;
temp.max=max+b.max;
temp.list=new float[num+b.num];
for(int i=0;i<num;i++)
*(temp.list+i)=*(list+i);
for(int i=0;i<b.num;i++)
*(temp.list+num+i)=*(b.list+i);
return temp;
}
line operator =(line &b)
{
delete []list;
list = new float [b.max];
num=b.num;
max=b.max;
for(int i=0;i<num;i++)
*(list+i)=*(b.list+i);
return *this;
}
};
void main()
{
cout<<"input a:\n";
line a(5,2);
cout<<"input b\n";
line b(5,4);
cout<<"a:\n";
a.show();
cout<<"b\n";
b.show();
line c;
c=a+b;
a=b;
cout<<"a:\n";
a.show();
cout<<"b:\n";
b.show();
cout<<"c:\n";
c.show();
}
三、线性表类运算符友元函数重载
#include<iostream>
using namespace std;
class line
{
float * list;
int max;
int num;
public:
line(int b=0, int c=0)
{
max=b;
num=c;
if(num>max)
cout<<"error, num must be smaller than max!"<<endl;
list= new float[max];
for(int i=0;i<num;i++)
{
cout<<"total "<<num<<" data, it is "<<i<<" now.\n";
cin>>*(list+i);
}
}
line(line &a)
{
max=a.max;
num=a.num;
list = new float[max];
for(int i=0;i<num;i++)
*(list+i)=*(a.list+i);
cout<<"copy constructor"<<endl;
}
~line() {delete []list;}
void show()
{
for(int i=0;i<num;i++)
cout<<*(list+i)<<'\t';
cout<<endl;
}
line operator =(line &b)
{
delete []list;
list = new float [b.max];
num=b.num;
max=b.max;
for(int i=0;i<num;i++)
*(list+i)=*(b.list+i);
return *this;
}
friend line operator +(line &a,line &b);
};
line operator +(line &a,line &b)
{
line temp;
temp.num=a.num+b.num;
temp.max=b.max+b.max;
temp.list=new float[temp.max];
for(int i=0;i<temp.num;i++)
*(temp.list+i)=*(a.list+i);
for(int i=0;i<b.num;i++)
*(temp.list+a.num+i)=*(b.list+i);
return temp;
}
void main()
{
cout<<"input a:\n";
line a(5,2);
cout<<"input b\n";
line b(5,4);
cout<<"a:\n";
a.show();
cout<<"b\n";
b.show();
line c;
c=a+b;
a=b;
cout<<"a:\n";
a.show();
cout<<"b:\n";
b.show();
cout<<"c:\n";
c.show();
}