定义一个圆类,其中成员变量是point类的对象;
计算面积,输出坐标;
#include<iostream>
using namespace std;
class point
{
double x, y;
public:
point(double x_, double y_) :x(x_), y(y_) {}
point() = default;
double getx()
{
return x;
}
double gety()
{
return y;
}
};
class cicle
{
double r;
point p;
public:
cicle(double r_, double x2, double y2) :r(r_), p(x2, y2) {}
void area();
void getx1()
{
cout << p.getx() << endl;
}
void gety1()
{
cout << p.gety() << endl;
}
};
void cicle::area()
{
cout << 3.14* r * r << endl;
}
int main()
{
double r, x1, y1;
cin >> r >> x1 >> y1;
cicle c(r, x1, y1);
c.area();
c.getx1();
c.gety1();
return 0;
}
在主函数中不能直接p.getx()访问point的类的私有成员,则需在cicle类中另写输出函数,或者将cicle类声明为point类的友元可直接访问point类的私有成员但破坏了类的封装性。