运算符重载.docx
《运算符重载.docx》由会员分享,可在线阅读,更多相关《运算符重载.docx(15页珍藏版)》请在冰豆网上搜索。
![运算符重载.docx](https://file1.bdocx.com/fileroot1/2023-6/15/c2ae586b-7087-4f29-919c-ee1f1003869c/c2ae586b-7087-4f29-919c-ee1f1003869c1.gif)
运算符重载
实验二运算符重载
1.实验目的:
1.学习定义和使用重载运算符。
2.熟悉拷贝构造函数。
2.实验任务:
1、对比通过函数来实现复数相加和通过重载运算符实现复数相加的不同之处。
2、定义RMB类,数据成员有yuan、jf,请为该类定义构造函数、并重载“+”、“-”、“<<”、“>>”。
选做:
定义账户类,有账号、户名、余额等属性,具有存款、取款等操作,可以把RMB类对象作为成员,进行存取款操作。
3、以下程序实际要输出0~9之间每个数的平方,请用增加拷贝构造函数的方法避免存在的问题。
#include
usingnamespacestd;
classVector{
public:
Vector(ints=100);
Vector(Vector&v);
voidDisplay();
voidSet();
~Vector();
protected:
intsize;
int*buffer;
};
Vector:
:
Vector(ints){
buffer=newint[size=s];
for(inti=0;ibuffer[i]=i*i;
}
Vector:
:
Vector(Vector&v)
{
size=v.size;
buffer=newint[size];
for(inti=0;ibuffer[i]=v.buffer[i];}
voidVector:
:
Display(){
for(intj=0;jcout<voidVector:
:
Set(){
for(intj=0;jbuffer[j]=j+1;
}
Vector:
:
~Vector(){delete[]buffer;}
intmain()
{Vectora(10);
Vectorb(a);
a.Set();
b.Display();
return0;
}
3.列出代码
1、
(1)通过函数:
#include"iostream"
usingnamespacestd;
classComplex
{
public:
Complex(){real=0;imag=0;};
Complex(doubler,doublei){real=r;imag=i;}
ComplexAdd(Complex&c2);
voiddisplay();
private:
doublereal;
doubleimag;
};
ComplexComplex:
:
Add(Complex&c2)
{
Complexc;
c.real=c2.real+real;
c.imag=c2.imag+imag;
returnc;
}
voidComplex:
:
display()
{
cout<}
intmain()
{
Complexc1(3,4),c2(5,-10),c3;
c3=c1.Add(c2);
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";c3.display();
return0;
}
(2)通过运算符重载:
#include"iostream"
usingnamespacestd;
classComplex
{
public:
Complex(){real=0;imag=0;};
Complex(doubler,doublei){real=r;imag=i;}
Complexoperator+(Complex&c2);
voiddisplay();
private:
doublereal;
doubleimag;
};
ComplexComplex:
:
operator+(Complex&c2)
{
Complexc;
c.real=c2.real+real;
c.imag=c2.imag+imag;
returnc;
}
voidComplex:
:
display()
{
cout<}
intmain()
{
Complexc1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";c3.display();
return0;
}
2、
#include"iostream"
usingnamespacestd;
classRMB
{
public:
RMB(){yuan=0;j=0;f=0;};
RMB(intr,inti){yuan=r;j=i;f=i;}
RMBoperator+(RMB&c2);
RMBoperator-(RMB&c2);
voiddisplay();
public:
intyuan;
intj;
intf;
};
classAD:
publicRMB
{
public:
ADqu(RMB&c1,RMB&c2);
ADcun(RMB&C1,RMB&c2);
};
ADAD:
:
qu(RMB&c1,RMB&c2)
{
ADb;
b.f=c1.f-c2.f;
if(b.f<0)
{
c1.j=c1.j-1;
b.f=b.f+10;
b.j=c1.j-c2.j;
if(b.j<0)
{
c1.yuan=c1.yuan-1;
b.j=b.j+10;
b.yuan=c1.yuan-c2.yuan;
}
else
{
b.yuan=c1.yuan-c2.yuan;
}
}
else
{
b.j=c1.j-c2.j;
if(b.j<0)
{
c1.yuan=c1.yuan-1;
b.j=b.j+10;
b.yuan=c1.yuan-c2.yuan;
}
else
{
b.yuan=c1.yuan-c2.yuan;
}
}
returnb;
}
ADAD:
:
cun(RMB&c1,RMB&c2)
{
ADb;
b.yuan=c1.yuan+c2.yuan;
b.f=c2.f+c1.f;
if(b.f>=10)
{
b.j=b.j+1;
b.f=b.f-10;
}
b.j=c2.j+c1.j;
if(b.j>=10)
{
b.yuan=b.yuan+1;
b.j=b.j-10;
}
returnb;
}
RMBRMB:
:
operator+(RMB&c2)
{
RMBc;
c.yuan=c2.yuan+yuan;
c.j=c2.j+j;
c.f=c2.f+f;
returnc;
}
RMBRMB:
:
operator-(RMB&c2)
{
RMBc;
c.yuan=c2.yuan-yuan;
c.j=c2.j-j;
c.f=c2.f-f;
returnc;
}
voidRMB:
:
display()
{
cout<<"共计:
";
cout<}
intmain()
{
inti,n;
ADc1,c2,c3;
cout<<"请输入警戒余额:
";
cin>>n;
cout<<"请输入当前存款"<cout<<"请输入元:
";
cin>>c1.yuan;
cout<<"请输入角:
";
cin>>c1.j;
cout<<"请输入分:
";
cin>>c1.f;
cout<<"请选择操作:
1、存款2、取款;"<cin>>i;
if(i==1)
{
cout<<"请输入元:
";
cin>>c2.yuan;
cout<<"请输入角:
";
cin>>c2.j;
cout<<"请输入分:
";
cin>>c2.f;
c3=c1.cun(c1,c2);
c3.display();
if(c3.yuan{
cout<<""<cout<<"警示:
账户余额少于"<"<cout<<""<}
else
cout<}
elseif(i==2)
{
cout<<"请输入元:
";
cin>>c2.yuan;
cout<<"请输入角:
";
cin>>c2.j;
cout<<"请输入分:
";
cin>>c2.f;
c3=c1.qu(c1,c2);
c3.display();
if(c3.yuan{
cout<<""<cout<<"警示:
账户余额少于"<"<cout<<""<}
else
cout<}
else
{
cout<<"输入错误"<}
return0;
}
3、
#include
usingnamespacestd;
classVector{
public:
Vector(ints=100);
Vector(Vector&v);
voidDisplay();
voidSet();
~Vector();
protected:
intsize;
int*buffer;
};
Vector:
:
Vector(ints){
buffer=newint[size=s];
for(inti=0;ibuffer[i]=i*i;
}
Vector:
:
Vector(Vector&v)
{
size=v.size;
buffer=newint[size];
for(inti=0;ibuffer[i]=v.buffer[i];}
voidVector:
:
Display(){
for(intj=0;jcout<voidVector:
:
Set(){
for(intj=0;jbuffer[j]=j+1;
}
Vector:
:
~Vector(){delete[]buffer;}
intmain()
{Vectora(10);
Vectorb(a);
a.Set();
b.Display();
return0;
}
4.结果分析
(1)
不同之处:
通过比较,发现用运算符重载在代码形式上更加简便,更加直观。
(2)
(3)