您现在的位置: 中国男护士网 >> 考试频道 >> 计算机等级 >> 二级辅导 >> C十十 >> 辅导 >> 正文    
  C++习题与解析(重载-01) 【注册男护士专用博客】          

C++习题与解析(重载-01)

www.nanhushi.com     佚名   不详 

01.分析以下程序执行结果 
#include<iostream.h> 
int add(int x,int y) 

return x+y; 

double add(double x,double y) 

return x+y; 

void main() 

int a=4,b=6; 
double c=2.6,d=7.4; 
cout<<add(a,b)<<","<<add(c,d)<<endl; 

解: 
本题说明函数重载的使用方法, 这里有两个add()函数,一个add()函数的参数与返回值为int型,另一个的参数与返回值为double型,它们是根据参数类型自动区分的。 
所以输出为: 10,10 



02.分析以下程序的执行结果 
#include<iostream.h> 
class Sample 

int i; 
double d; 
public: 
void setdata(int n){i=n;} 
void setdata(double x){d=x;} 
void disp() 

cout<<"i="<<i<<",d="<<d<<endl; 

}; 
void main() 

Sample s; 
s.setdata(10); 
s.setdata(15.6); 
s.disp(); 

解: 
本题说明重载成员函数的使用方法。setdata()成员函数有两个,根据其参数类型加以区分。 
所以输出为:i=10, d=15.6 



03.分析以下程序的执行结果 
#include<iostream.h> 
class Sample 

int n; 
public: 
Sample(){} 
Sample(int i){n=i;} 
Sample &operator =(Sample); 
void disp(){cout<<"n="<<n<<endl;} 
}; 
Sample &Sample::operator=(Sample s) 

Sample::n=s.n; 
return *this; 

void main() 

Sample s1(10),s2; 
s2=s1; 
s2.disp(); 

解: 
本题说明重载运算符(=)的使用方法。operator=成员函数实现两个对象的赋值。 
所以输出为: n=10 



04.设计一个点类Point,实现点对象之间的各种运算。 
解: 
Point类提供了6个运算符重载函数(参加程序中的代码和注释),以实现相应的运算。 
本题程序如下: 
#include<iostream.h> 
class Point 

int x,y; 
public: 
Point(){x=y=0;} 
Point(int i,int j){x=i;y=j;} 
Point(Point &); 
~Point(){} 
void offset(int,int); // 提供对点的偏移 
void offset(Point); // 重载,偏移量用Point类对象表示 
bool operator==(Point); // 运算符重载,判断两个对象是否相同 
bool operator!=(Point); // 运算符重载,判断两个对象是否不相同 
void operator+=(Point); // 运算符重载,将两个点对象相加 
void operator-=(Point); // 运算符重载,将两个点对象相减 
Point operator+(Point ); // 运 算符重 载,相加并将结果放在左操作数中 
Point operator-(Point); // 运算符重载,相减并将结果放在左操作数中 
int getx(){return x;} 
int gety(){return y;} 
void disp() 

cout<<"("<<x<<","<<y<<")"<<endl; 

}; 
Point::Point(Point &p) 

x=p.x; y=p.y; 

void Point::offset(int i,int j) 

x+=i; y+=j; 

void Point::offset(Point p) 

x+=p.getx(); y+=p.gety(); 

bool Point::operator==(Point p) 

if(x==p.getx()&&y==p.gety()) 
return 1; 
else 
return 0; 

bool Point::operator!=(Point p) 

if(x!=p.getx()||y!=p.gety()) 
return 1; 


else 
return 0; 

void Point::operator+=(Point p) 

x+=p.getx(); y+=p.gety(); 

void Point::operator-=(Point p) 

x-=p.getx(); y-=p.gety(); 

Point Point::operator+(Point p) 

this->x+=p.x; this->y+=p.y; 
return *this; 

Point Point::operator-(Point p) 

this->x-=p.x;this->y-=p.y; 
return *this; 

void main() 

Point p1(2,3),p2(3,4),p3(p2); 
cout<<"1:"; p3.disp(); 
p3.offset(10,10); 
cout<<"2:"; p3.disp(); 
cout<<"3:"<<(p2==p3)<<endl; 
cout<<"4:"<<(p2!=p3)<<endl; 
p3+=p1; 
cout<<"5:"; p3.disp(); 
p3-=p2; 
cout<<"6:"; p3.disp(); 
p3=p1+p3; // 先将p1+p3的结果放在p1中,然后赋给p3,所以p1=p3 
cout<<"7:"; p3.disp(); 
p3=p1-p2; 
cout<<"8:"; p3.disp(); 


本程序的执行结果如下: 
1:(3,4) 
2:(13,14) 
3:0 
4:1 
5:(15,17) 
6:(12,13) 
7:(14,16) 
8:(11,12) 

 

文章录入:杜斌    责任编辑:杜斌 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     

    联 系 信 息
    QQ:88236621
    电话:15853773350
    E-Mail:malenurse@163.com
    免费发布招聘信息
    做中国最专业男护士门户网站
    最 新 热 门
    最 新 推 荐
    相 关 文 章
    没有相关文章
    专 题 栏 目

      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)                            【进男护士社区逛逛】
    姓 名:
    * 游客填写  ·注册用户 ·忘记密码
    主 页:

    评 分:
    1分 2分 3分 4分 5分
    评论内容:
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。