上机练习类 4
填空题
1. 定义“点”类,在下划线处按注释的要求,填上正确的语句成分。(4 分)
#include <iostream.h>
class Point
{ public:
Point(int xx=0,int yy=0)
{ X=xx; Y=yy; countP++; }
private:
int X ,Y;
//定义静态数据成员 countP
};
//用 0 初始化静态数据成员 countP
void main()
{
Point A( 5 , 8 );
}
2. 假设已定义一“点”(Point)类,现定义一个“距离”类,主要功能是计算两点的距离, 在下
划线处按注释的要求,填上正确的语句成分。(4 分)
#include <iostream.h>
#include <math.h>
class Distance
{
//将函数 dist 声明为类的友员
public:
Distance(Point xp1, Point xp2) :p1(xp1),p2(xp2){}
void Display( ){ cout<<"dist="<<dist<<endl; }
private:
Point p1,p2;
double dist;
};
void Dist (Distance d)
{
double x = double(d.p1.GetX()-d.p2.GetX());
double y = double(d.p1.GetY()-d.p2.GetY());
d.dist = ;//求两点距离;
d.Display();
}
3. 重载“+”运算符,完成两个复数的加法运算。在程序中空白处按注释的要求,填上正确