利用LOCATION类,使用继承定义圆类CIRCLE,圆由圆心和半径构成。提供得到圆心坐标和半径的成员函数、以及计算圆的周长和面积的成员函数。在主程序中创建两个圆A和B,圆心坐标分别为(0,3)、(3,7),按如下格式输出两个圆的圆心坐标、周长和面积,并计算和输出两个圆的圆心之间的距离。
A:(x1,y1,r1),Girth=g1,Area=a1
B:(x2,y2,r2),Girth=g2,Area=a2
A(xl,y1),B(x2,y2),Distance=d
定义LOCATION类,该类没有“继承”等现象。注意二维坐标点之间的距离公式。
class LOCATION{
double x,y;
public:
virtual double getx()const{return x;};
virtual double gety()const{return y;};
virtual double dist(LOCATION &s)const;
LOCATION(double x,double y):x(x),y(y){};
};
double LOCATION::dist(LOCATION &s)const{
double xd=s.x-x,yd=s.y-y;
return sqrt(xd*xd+yd*yd);
}
#INCLUDE<IOSTREAM.H>
#INCLUDE<MATH.H>
CLASS LOCATION{
DOUBLE X,Y;
PUBLIC:
VIRTUAL DOUBLE GETX()CONST{RETURN X;};
VIRTUAL DOUBLE GETY()CONST{RETURN Y;};
VIRTUAL DOUBLE DIST(LOCATION &S)CONST;
LOCATION(DOUBLE X,DOUBLE Y):X(X),Y(Y){};
};
DOUBLE LOCATION::DIST(LOCATION &S)CONST{
DOUBLE XD=S.X - X,YD=S.Y - Y;
RETURN SQRT(XD*XD+YD*YD);
}
CLASS CIRCLE:PUBLIC LOCATION{
PRIVATE:
DOUBLE R;
PUBLIC:
CIRCLE(DOUBLE X,DOUBLE Y,DOUBLE R):LOCATION(X,Y),R(R){}
DOUBLE GETR()CONST{RETURN R;}
DOUBLE GETGIRTH()CONST{RETURN 2*3.14*R;}
DOUBLE GETAREA()CONST{RETURN 3.14*R*R;}
};
VOID MAIN(){
CIRCLE A(0,3,2);
CIRCLE B(3,7,5);
COUT<<"A:
("<<A.GETX()<<","<<A.GETY()<<"),GIRTH="<<A.GETGIRTH()<<",AREA="<<A.GETAREA()<<ENDL;
COUT<<"B:
("<<B.GETX()<<","<<B.GETY()<<"),GIRTH="<<B.GETGIRTH()<<",AREA="<<B.GETAREA()<<ENDL;
&NBSP;COUT<<"A("<<A.GETX()<<","<<A.GETY()<<"),B("<<B.GETX()<<","<<B.GETY()<<"), DISTANCE="<<A.DIST(B)<<ENDL;
}