实验十_运算符重载
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
while(day>dys[dt.mo-1])
{day-=dys[dt.mo-1];//cout<<day<<endl;
if(++dt.mo==13)
{dt.mo=1;
dt.yr++;
}
}
//cout<<day<<endl;
//dt.disp();
dt.da=day;
return dt;
}
};
void main(void)
};
void point::print()
{cout<<"x= "<<x<<" "<<"y= "<<y<<endl;}
point point::operator++()
{++x;++y;
return * this;
}
point point::operator++(int)
{point temp(* this);
{
date d1(5,31,2009),d2;
//d1.disp();
d2=d1+20;
d2.disp();
}
程序运行结果:
6/20/2009
Press any key to continue
3)p2565.145.15 5.16
5.14
#include<iostream>
using namespace std;
}
int main()
{Words word("This is C++ book.");
word.disp();
cout<<"第1个字符: ";
cout<<word[0]<<endl;
cout<<"第16个字符: ";
cout<<word[15]<<endl;
cout<<"第26个字符: ";
cout<<word[25]<<endl;
rationalNumber r;
if(r1.fz==0)
{r.fz=-r2.fz;
r.fm=r2.fm;
return r;
}
else
{r.fm=r1.fm*r2.fm;
r.fz=(r1.fz*r2.fm)-(r2.fz*r1.fm);
return r;
}
}
rationalNumber operator* (rationalNumber& r1,rationalNumber& r2){
friend rationalNumber operator -(rationalNumber& r1,rationalNumber& r2);
friend rationalNumber operator *(rationalNumber& r1,rationalNumber& r2);
friend rationalNumber operator /(rationalNumber& r1,rationalNumber& r2);
obj3.print();
obj2.print();
return 0;
}
程序运行的结果是:
x= 6 y= 9
x= 6 y= 9
x= 7 y= 10
x= 7 y= 10
x= 9 y= 7
x= 8 y= 6
x= 8 y= 6
x= 7 y= 5
Press any key to continue
2)构造一个分数类rationalNumber,该类中包括分子和分母两个成员数据,并具有下述功能:
public:
date(int m,int d,int y){mo=m;da=d;yr=y;}
date(){}
void disp()
{cout<<mo<<"/"<<da<<"/"<<yr<<endl;}
date operator+(int day)
{
date dt=*this;
day+=dt.da;//cout<<day<<endl;
}
A operator -- (A &a)
{--a.x;
return a;
}
void A::print()
{cout<<x<<endl;}
int main()
{
A a(7);
++a;
a.print();
--a;
a.print();
return 0;
}
运行结果:
7
6
Press any key to continue
public:
Length(int m)
{meter=m;
}
operator double()
{return (1.0*meter/1000);
}
};
int main()
{Length a(1500);
double m=float(a);
cout<<"m="<<m<<"千米"<<endl;
return 0;
int k;
if(m>n)k=n;
else k=m;
for(;k>0;k--)
if(m%k==0&&n%k==0)return k;
return 1;
}
程序代码:
#include <iostream.h>
#include<math.h>
#include<stdlib.h>
class rationalNumber{
rationalNumber r;
if(r1.fz==0)
return r2;
else
{r.fm=r1.fm*r2.fm;
r.fz=(r1.fz*r2.fm)+(r2.fz*r1.fm);
return r;
}
}
rationalNumber operator- (rationalNumber& r1,rationalNumber& r2){
}
void simple();
void display(){
if(abs(fm)==abs(fz)||abs(fz)%abs(fm)==0||fz==0)
cout<<fz/fm<<endl;
else
cout<<fz<<"/"<<fm<<endl;
}
friend rationalNumber operator +(rationalNumber& r1,rationalNumber& r2);
实验十运算符重载
1.实验目的及要求
1)掌握运算符重载的基本概念和方法。
2)熟习几种特殊的运算符的重载。
2.实验内容
1.分析下面的程序,指出程序运行的结果:
1)
#include<iostream.h>
class point{
int x,y;
public:
point(int vx,int vy){
x=vx; y=vy;}
class A{
public:
A(int i):x(i){}
A()
{x=0;
}
friend A operator ++ (A a);
friend A operator -- (A &a);
void print();
private:
int x;
};
A operator ++ (A a)
{++a.x;
return a;
{ int k,i;
if(abs(fz)<fm)
k=abs(fz);
else
k=fm;
for(i=1;i<=k;i++)
{
if(fz%i==0&&fm%i==0)
{
fz=fz/i;
fm=fm/i;
}
}
}
//m,n的最大公约数,假定m、n的最大公约数是v,则它们的最小公倍数就是m*n/v。
int fun (int m,int n){
return 0;
}
运行结果:
This is C++ book.
第1个字符: T
第16个字符: k
第26个字符:数组下标超界!
Press any key to continue
5.16
#include<iostream>
using namespace std;
class Length{
int meter;
1建立构造函数,它能防止分母为0,当分数不是最简形式时进行约分,并避免分母为负数。
2重载加法、减法、乘法和除法运算符。
3重载关系运算符:>、<、==等。
提示:分数约分函数:
class rationalNumber//分数类
{intfm,fz;//fm—分母fz—分子
……
};
void simple()//约分函数
}
运行结果:
m=1.5千米
Press any key to continue
三.编写并调试程序.
定义一个描述平面上一个点的类point,重载“++”和“--”运算符,并区分这两种运算符的前置和后置操作,构成一个完整的程序。(参考例5.8)
程序代码:
#include<iostream.h>
class point{
运行结果为:
30,30
Press any key to continue
2)分析下面程序,指出程序运行的结果:
#include<iostream.h>
static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31};
class date
{
int mo,da,yr;
{
if(m==0)
{
cout<<"分母为零!"<<endl;
exit(1);
}
else if(m<0)
{fz=-z;
fm=-m;
}
else
{fm=m;fz=z;
}
simple();
}
rationalNumber operator+ (rationalNumber& r1,rationalNumber& r2){
5.15
#include<iostream>
using namespace std;
class Words{
public:
Words(char*s)
{str=new char[strlen(s)+1];
strcpy(str,s);
len=strlen(s);
}
void disp();
char operator[](int n);
private:
int len;
char*str;
};
char Words::operator[](int n)
{if(n<0||n>len-1)
{cout<<"数组下标超界!\n";
return ' ';
}
else
return * (str+n);
}
void Words::disp()
{cout<<str<<endl;
rationalNumber r;
r.fm=r1.fm*r2.fm;
r.fz=r1.fz*r2.fz;
return r;
}
rationalNumber operator/ (rationalNumber& r1,rationalNumber& r2){
x++;y++;
return temp;
}
point point::operator--()
{--x;--y;
return * this;
}
point point::operator--(int)
{point temp(* this);
x--;y--;
return temp;
}
int main()
{
int fm,fz;
public:
rationalNumber(int z=1,int m=2);
int fun(int fz,int fm)
{int k;
if(fz>fm)
k=fm;
else k=fBaidu Nhomakorabea;
for(;k>0;k--)
if(fz%k==0&&fm%k==0)
return k;
return 1;
public:
point(int x1=0,int y1=0)
{x=x1;y=y1;}
void print();
point operator++();
point operator++(int);
point operator--();
point operator--(int);
private:
int x,y;
bool operator >(const rationalNumber& r1);
bool operator <(const rationalNumber& r1);
bool operator ==(const rationalNumber& r1);
};
rationalNumber::rationalNumber(int z,int m)
return point(px,py);
}
void print(){cout<<x<<","<<y<<endl;}
};
void main(void)
{
point p1(10,10),p2(20,20);
p1=p1+p2;
p1.print();
//p2.print();
//p3.print();
}
程序分析:
point(){x=0,y=0;}
point operator+(point p1)
{
int px=x+p1.x;
int py=y+p1.y;
return point(px,py);
}
point operator-(point p1)
{
point p;
int px=x-p1.x;
int py=y-p1.y;
point obj(5,8),obj1,obj2(9,7),obj3;
++obj;
obj.print();
obj1=obj++;
obj1.print();
obj.print();
obj.print();
cout<<endl;
obj2.print();
--obj2;
obj2.print();
obj3=obj2--;
{day-=dys[dt.mo-1];//cout<<day<<endl;
if(++dt.mo==13)
{dt.mo=1;
dt.yr++;
}
}
//cout<<day<<endl;
//dt.disp();
dt.da=day;
return dt;
}
};
void main(void)
};
void point::print()
{cout<<"x= "<<x<<" "<<"y= "<<y<<endl;}
point point::operator++()
{++x;++y;
return * this;
}
point point::operator++(int)
{point temp(* this);
{
date d1(5,31,2009),d2;
//d1.disp();
d2=d1+20;
d2.disp();
}
程序运行结果:
6/20/2009
Press any key to continue
3)p2565.145.15 5.16
5.14
#include<iostream>
using namespace std;
}
int main()
{Words word("This is C++ book.");
word.disp();
cout<<"第1个字符: ";
cout<<word[0]<<endl;
cout<<"第16个字符: ";
cout<<word[15]<<endl;
cout<<"第26个字符: ";
cout<<word[25]<<endl;
rationalNumber r;
if(r1.fz==0)
{r.fz=-r2.fz;
r.fm=r2.fm;
return r;
}
else
{r.fm=r1.fm*r2.fm;
r.fz=(r1.fz*r2.fm)-(r2.fz*r1.fm);
return r;
}
}
rationalNumber operator* (rationalNumber& r1,rationalNumber& r2){
friend rationalNumber operator -(rationalNumber& r1,rationalNumber& r2);
friend rationalNumber operator *(rationalNumber& r1,rationalNumber& r2);
friend rationalNumber operator /(rationalNumber& r1,rationalNumber& r2);
obj3.print();
obj2.print();
return 0;
}
程序运行的结果是:
x= 6 y= 9
x= 6 y= 9
x= 7 y= 10
x= 7 y= 10
x= 9 y= 7
x= 8 y= 6
x= 8 y= 6
x= 7 y= 5
Press any key to continue
2)构造一个分数类rationalNumber,该类中包括分子和分母两个成员数据,并具有下述功能:
public:
date(int m,int d,int y){mo=m;da=d;yr=y;}
date(){}
void disp()
{cout<<mo<<"/"<<da<<"/"<<yr<<endl;}
date operator+(int day)
{
date dt=*this;
day+=dt.da;//cout<<day<<endl;
}
A operator -- (A &a)
{--a.x;
return a;
}
void A::print()
{cout<<x<<endl;}
int main()
{
A a(7);
++a;
a.print();
--a;
a.print();
return 0;
}
运行结果:
7
6
Press any key to continue
public:
Length(int m)
{meter=m;
}
operator double()
{return (1.0*meter/1000);
}
};
int main()
{Length a(1500);
double m=float(a);
cout<<"m="<<m<<"千米"<<endl;
return 0;
int k;
if(m>n)k=n;
else k=m;
for(;k>0;k--)
if(m%k==0&&n%k==0)return k;
return 1;
}
程序代码:
#include <iostream.h>
#include<math.h>
#include<stdlib.h>
class rationalNumber{
rationalNumber r;
if(r1.fz==0)
return r2;
else
{r.fm=r1.fm*r2.fm;
r.fz=(r1.fz*r2.fm)+(r2.fz*r1.fm);
return r;
}
}
rationalNumber operator- (rationalNumber& r1,rationalNumber& r2){
}
void simple();
void display(){
if(abs(fm)==abs(fz)||abs(fz)%abs(fm)==0||fz==0)
cout<<fz/fm<<endl;
else
cout<<fz<<"/"<<fm<<endl;
}
friend rationalNumber operator +(rationalNumber& r1,rationalNumber& r2);
实验十运算符重载
1.实验目的及要求
1)掌握运算符重载的基本概念和方法。
2)熟习几种特殊的运算符的重载。
2.实验内容
1.分析下面的程序,指出程序运行的结果:
1)
#include<iostream.h>
class point{
int x,y;
public:
point(int vx,int vy){
x=vx; y=vy;}
class A{
public:
A(int i):x(i){}
A()
{x=0;
}
friend A operator ++ (A a);
friend A operator -- (A &a);
void print();
private:
int x;
};
A operator ++ (A a)
{++a.x;
return a;
{ int k,i;
if(abs(fz)<fm)
k=abs(fz);
else
k=fm;
for(i=1;i<=k;i++)
{
if(fz%i==0&&fm%i==0)
{
fz=fz/i;
fm=fm/i;
}
}
}
//m,n的最大公约数,假定m、n的最大公约数是v,则它们的最小公倍数就是m*n/v。
int fun (int m,int n){
return 0;
}
运行结果:
This is C++ book.
第1个字符: T
第16个字符: k
第26个字符:数组下标超界!
Press any key to continue
5.16
#include<iostream>
using namespace std;
class Length{
int meter;
1建立构造函数,它能防止分母为0,当分数不是最简形式时进行约分,并避免分母为负数。
2重载加法、减法、乘法和除法运算符。
3重载关系运算符:>、<、==等。
提示:分数约分函数:
class rationalNumber//分数类
{intfm,fz;//fm—分母fz—分子
……
};
void simple()//约分函数
}
运行结果:
m=1.5千米
Press any key to continue
三.编写并调试程序.
定义一个描述平面上一个点的类point,重载“++”和“--”运算符,并区分这两种运算符的前置和后置操作,构成一个完整的程序。(参考例5.8)
程序代码:
#include<iostream.h>
class point{
运行结果为:
30,30
Press any key to continue
2)分析下面程序,指出程序运行的结果:
#include<iostream.h>
static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31};
class date
{
int mo,da,yr;
{
if(m==0)
{
cout<<"分母为零!"<<endl;
exit(1);
}
else if(m<0)
{fz=-z;
fm=-m;
}
else
{fm=m;fz=z;
}
simple();
}
rationalNumber operator+ (rationalNumber& r1,rationalNumber& r2){
5.15
#include<iostream>
using namespace std;
class Words{
public:
Words(char*s)
{str=new char[strlen(s)+1];
strcpy(str,s);
len=strlen(s);
}
void disp();
char operator[](int n);
private:
int len;
char*str;
};
char Words::operator[](int n)
{if(n<0||n>len-1)
{cout<<"数组下标超界!\n";
return ' ';
}
else
return * (str+n);
}
void Words::disp()
{cout<<str<<endl;
rationalNumber r;
r.fm=r1.fm*r2.fm;
r.fz=r1.fz*r2.fz;
return r;
}
rationalNumber operator/ (rationalNumber& r1,rationalNumber& r2){
x++;y++;
return temp;
}
point point::operator--()
{--x;--y;
return * this;
}
point point::operator--(int)
{point temp(* this);
x--;y--;
return temp;
}
int main()
{
int fm,fz;
public:
rationalNumber(int z=1,int m=2);
int fun(int fz,int fm)
{int k;
if(fz>fm)
k=fm;
else k=fBaidu Nhomakorabea;
for(;k>0;k--)
if(fz%k==0&&fm%k==0)
return k;
return 1;
public:
point(int x1=0,int y1=0)
{x=x1;y=y1;}
void print();
point operator++();
point operator++(int);
point operator--();
point operator--(int);
private:
int x,y;
bool operator >(const rationalNumber& r1);
bool operator <(const rationalNumber& r1);
bool operator ==(const rationalNumber& r1);
};
rationalNumber::rationalNumber(int z,int m)
return point(px,py);
}
void print(){cout<<x<<","<<y<<endl;}
};
void main(void)
{
point p1(10,10),p2(20,20);
p1=p1+p2;
p1.print();
//p2.print();
//p3.print();
}
程序分析:
point(){x=0,y=0;}
point operator+(point p1)
{
int px=x+p1.x;
int py=y+p1.y;
return point(px,py);
}
point operator-(point p1)
{
point p;
int px=x-p1.x;
int py=y-p1.y;
point obj(5,8),obj1,obj2(9,7),obj3;
++obj;
obj.print();
obj1=obj++;
obj1.print();
obj.print();
obj.print();
cout<<endl;
obj2.print();
--obj2;
obj2.print();
obj3=obj2--;