📄 cjj119.htm
字号:
color=#0000ff>04.设计一个点类Point,实现点对象之间的各种运算。</FONT><BR>解:<BR>
Point类提供了6个运算符重载函数(参加程序中的代码和注释),以实现相应的运算。<BR>本题程序如下:<BR>#include<iostream.h><BR>class
Point<BR>{<BR> int
x,y;<BR>
public:<BR>
Point(){x=y=0;}<BR>
Point(int i,int
j){x=i;y=j;}<BR>
Point(Point
&);<BR>
~Point(){}<BR>
void
offset(int,int); //
提供对点的偏移<BR>
void
offset(Point);
//
重载,偏移量用Point类对象表示<BR>
bool operator==(Point); //
运算符重载,判断两个对象是否相同<BR>
bool operator!=(Point); //
运算符重载,判断两个对象是否不相同<BR>
void operator+=(Point); //
运算符重载,将两个点对象相加<BR>
void operator-=(Point); //
运算符重载,将两个点对象相减<BR>
Point operator+(Point<FONT
color=#000000>); </FONT><FONT
color=#000000>// 运</FONT><FONT
color=#000000>算符重</FONT>载,相加并将结果放在左操作数中<BR>
Point operator-(Point); //
运算符重载,相减并将结果放在左操作数中<BR>
int getx(){return
x;}<BR>
int gety(){return
y;}<BR>
void
disp()<BR>
{<BR>
cout<<"("<<x<<","<<y<<")"<<endl;<BR>
}<BR>};<BR>Point::Point(Point
&p)<BR>{<BR> x=p.x;
y=p.y;<BR>}<BR>void Point::offset(int i,int
j)<BR>{<BR> x+=i;
y+=j;<BR>}<BR>void Point::offset(Point
p)<BR>{<BR> x+=p.getx();
y+=p.gety();<BR>}<BR>bool Point::operator==(Point
p)<BR>{<BR>
if(x==p.getx()&&y==p.gety())<BR>
return 1;<BR>
else<BR>
return 0;<BR>}<BR>bool Point::operator!=(Point
p)<BR>{<BR>
if(x!=p.getx()||y!=p.gety())<BR>
return
1;<BR> else <BR>
return 0;<BR>}<BR>void Point::operator+=(Point
p)<BR>{<BR> x+=p.getx();
y+=p.gety();<BR>}<BR>void Point::operator-=(Point
p)<BR>{<BR> x-=p.getx();
y-=p.gety();<BR>}<BR>Point Point::operator+(Point
p)<BR>{<BR> this->x+=p.x;
this->y+=p.y;<BR> return
*this;<BR>}<BR>Point Point::operator-(Point
p)<BR>{<BR> this->x-=p.x;this->y-=p.y;<BR> return
*this;<BR>}<BR>void
main()<BR>{<BR> Point
p1(2,3),p2(3,4),p3(p2);<BR>
cout<<"1:"; p3.disp();<BR>
p3.offset(10,10);<BR>
cout<<"2:"; p3.disp();<BR>
cout<<"3:"<<(p2==p3)<<endl;<BR>
cout<<"4:"<<(p2!=p3)<<endl;<BR>
p3+=p1;<BR> cout<<"5:";
p3.disp();<BR>
p3-=p2;<BR> cout<<"6:";
p3.disp();<BR>
p3=p1+p3; //
先将p1+p3的结果放在p1中,然后赋给p3,所以p1=p3<BR>
cout<<"7:"; p3.disp();<BR>
p3=p1-p2;<BR> cout<<"8:";
p3.disp();<BR>}<BR><BR>本程序的执行结果如下:<BR>
1:(3,4)<BR>
2:(13,14)<BR>
3:0<BR>
4:1<BR>
5:(15,17)<BR>
6:(12,13)<BR>
7:(14,16)<BR>
8:(11,12)<BR><BR>----------------------------------------------------<BR></P><BR><BR></TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD height=35 width="100%">
<DIV align=center></DIV>
</TD>
</TR>
</TBODY>
</TABLE>
</TD></TR></TBODY>
</TABLE>
</TD></TR></TBODY>
</TABLE>
</TD></TR></TBODY>
</TABLE>
<TABLE align=center border=0 cellPadding=0 cellSpacing=0 width=750>
<TBODY>
<TR>
<TD bgColor=#c1c1c1 height=1></TD>
</TR>
</TBODY>
</TABLE>
<TABLE align=center border=1 cellPadding=0 cellSpacing=1
style="BORDER-COLLAPSE: collapse" width=550>
<TBODY>
<td align=center>
<div align="center">
<p><a href=mailto:jjsun36@sohu.com ><span class="body14black">mailto:jjsun36@sohu.com</span></a><span class="body12red">
热线电话:0523-4864559</span></p>
</div>
</td>
</TBODY>
</TABLE>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -