手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>C/C++>列表

C 习题和解析-重载

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
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)

----------------------------------------------------

[1] [2] [3] [4] 下一页




文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

热点关注
IDC资讯 虚拟主机 域名注册 托管租用 vps主机 智能建站
网站运营 建站经验 策划盈利 搜索优化 网站推广 免费资源
网站联盟 联盟新闻 联盟介绍 联盟点评 网赚技巧
行业资讯 业界动态 搜索引擎 网络游戏 门户动态 电子商务 广告传媒
网络编程 Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术 Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷 Internet Explorer
网页制作 FrontPages Dreamweaver Javascript css photoshop fireworks Flash
程序设计 Java技术 C/C++ VB delphi
网络知识 网络协议 网络安全 网络管理 组网方案 Cisco技术
操作系统 Win2000 WinXP Win2003 Mac OS Linux FreeBSD
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 价格总览 | 资讯中心 | 友情链接 | 网站地图 | 招贤纳士 | RSS