首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
使用VC6打开考生文件夹下的工程MyProj4。此工程包含一个源程序文件 MyMain4.cpp,该程序将通过把类Distance定义为类Point的友元类来实现计算两点之间距离的功能。但程序中定义的类并不完整。请按要求完成下列操作,把类的定义补充完整。
使用VC6打开考生文件夹下的工程MyProj4。此工程包含一个源程序文件 MyMain4.cpp,该程序将通过把类Distance定义为类Point的友元类来实现计算两点之间距离的功能。但程序中定义的类并不完整。请按要求完成下列操作,把类的定义补充完整。
admin
2009-01-15
29
问题
使用VC6打开考生文件夹下的工程MyProj4。此工程包含一个源程序文件 MyMain4.cpp,该程序将通过把类Distance定义为类Point的友元类来实现计算两点之间距离的功能。但程序中定义的类并不完整。请按要求完成下列操作,把类的定义补充完整。
①把类Distance定义为类Point的友元类。请在注释“//**1**”之后添加适当的语句。
②定义类Point的构造函数,完成给私有数据成员x和y的赋值,并且两个参数的默认值都为0。请在注释“//**2**”之后添加适当的语句。
③完成类Distance的成员函数Dis(Point &p,Point &q)的定义,并在其中计算点p、q之间的距离,并且返回结果。假设两点之间的距离distance=sqrt((p.x-q.x)* (p.x-q.x)+(p.y-q.y)*(p.y-q.y))。请在注释“//**3**”之后添加适当的语句。
源程序文件MyMain4.cpp中的程序清单如下:
//MyMain4. cpp
#include<iostream>
#include<cmath>
using namespace std;
class Point
{
public:
/ /* * 1 * *
/ /定义类 Point 的构造函数
/ /* * 2 * *
void pint()
{
cout<<"x="<<x<<end1;
cout<<"y="<<y<<end1;
}
private:
float x,y;
};
class Distance
{
public:
float Dis(Point &p, Point &q);
};
float Distance :: Dis(Point &p, Point &q)
{
//* * 3 * *
}
int main ( )
{
Point p(10,10),q(20,20);
Distance d;
cout<<d.Dis(p,q)<<end1;
return 0;
}
选项
答案
类Point的定义如下: class Point { public: friend class Distance; Point (float a=0, float b=0) { x=a; y=b; } void pint() { cout<<"x="<<x<<end1; cout<<"y="<<y<<end1; } private: float x,y; };
解析
此道综合应用题主要考核友元类的定义与使用。
①第1处是完成友元类的声明,根据友元类的声明格式已知此处可填入:
friend class Distance;
②第2处是完成类Point的构造函数,此处还要注意类Point的构造函数中还应该有参数默认值。此处应填入:
Point(float a=0,float b=0){x=a;y=b}
③第3处是完成类Distance成员函数Dis()的定义,函数Dis()的功能是计算两点之间的距离。根据题目给出的计算公式,已知第3处应填入:
float result;result=sqrt((p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));return result;
转载请注明原文地址:https://kaotiyun.com/show/tKID777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
日本历史上的哪个时期与中世纪西欧相似?从政治制度、经济制度、社会结构等方面比较。(厦门大学2014年历史学基础真题)
实验组——控制组的实验设计中常用的统计方法是()
下列关于新教育发展时期的说法错误的是
下列选项中不是乌申斯基主张的教学原则是
“要尽量多地要求一个人,也要尽可能地尊重一个人”体现了马卡连柯的
20世纪60年代,英国()建议应为所有在能力和成绩方面合格的,并愿意接受高等教育的人提供高等教育课程,为此英国兴办了许多大学。
教育的个体个性化功能主要体现在:它促进人的主体意识的发展,促进人的个体特征的发展,以及促进人的
下列说法错误的是
根据CAPM模型,假定市场组合收益率为15%,无风险利率为6%,某证券的Beta系数为1.2,期望收益率为18%,则该证券()。
公开市场操作
随机试题
张某,女,50岁,单位体检时心电图示:ST段压低,T波倒置,测血压150/90mmHg,有吸烟史。此患者可诊断
市场失灵
下列不属于确定权重的方法是()。
187,268,457,592,763,()
根据我国《合同法》的规定,下列合同中确定地属于无效的有()。
社会资本简单再生产的实现过程,通过()实现。
Themajortaskfacingadolescentsistocreateastableidentity.Therearesomedevelopmentaltasksthatenablethemtocreate
Mydog,Hero,isn’tafraidofmostthings.Butheisafraidofthevacuumcleaner(真空吸尘器).HealwayshidehimselfwhenMumcleanst
SomeYemenisbuilttheirownpowerplantstoproduceelectricity.
Inrecentyears,traditionalprintednewspaperhasfacedaseriouschallenge.TheChristianScienceMonitorbecamethefirstnat
最新回复
(
0
)