|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
简单概括这段代码:
有point,circle两个对象
circle对象里面定义了一个point对象;
问题:怎么在point中x,y是私有数据的情况下
在circle对象里实现对point的x,y的初始化
- #include<bits/stdc++.h>
- using namespace std;
- double PI=3.14;
- class Point
- {
- public:
- Point(double xx,double yy); //constructor
- void Display(); //display point
- private:
- double x,y; //平面的点坐标x,y
- };
- //请在下面实现Point类的成员函数
- Point::Point(double xx=0,double yy=0)
- {
- x=xx;
- y=yy;
- }
- void Point::Display()
- {
- cout<<"Center:Point("<<x<<","<<y<<")"<<endl;
- }
- class Circle
- {
- private:
- Point p; //圆心点
- double r; //圆半径
- public:
- Circle(double x1,double y1,double r1);
- void Display();
- double Area();
- double Perimeter();
- };
- //请在下面实现Circle类的成员函数
- Circle::Circle(double x1,double y1,double r1)
- {
- Point p(x1,y1);
- r=r1;
- }
- void Circle::Display()
- {
- p.Display();
- cout<<"Radius:"<<r<<endl;
- cout<<"Area:"<<Area()<<endl;
- cout<<"Perimeter:"<<Perimeter()<<endl;
- }
- double Circle::Area()
- {
- return PI*r*r;
- }
- double Circle::Perimeter()
- {
- return PI*2*r;
- }
- int main()
- {
- double x,y,r;
- cin>>x>>y>>r; //圆心的点坐标及圆的半径
- Circle C(x,y,r);
- C.Display(); //输出圆心点坐标,圆的半径,圆的面积,圆的周长
- return 0;
- }
复制代码
|
|