C++定义Point,Circle,Cylinder类.用友元求(friend)圆面积,圆柱体体积.一定要用友元哦,顺便带上main函数的类容
来源:学生作业帮助网 编辑:六六作业网 时间:2025/01/13 04:42:50
C++定义Point,Circle,Cylinder类.用友元求(friend)圆面积,圆柱体体积.一定要用友元哦,顺便带上main函数的类容
C++定义Point,Circle,Cylinder类.用友元求(friend)圆面积,圆柱体体积.
一定要用友元哦,顺便带上main函数的类容
C++定义Point,Circle,Cylinder类.用友元求(friend)圆面积,圆柱体体积.一定要用友元哦,顺便带上main函数的类容
#include <iostream>
using namespace std;
const double PI = 3.141592653;
class Point
{
public:
double X;
double Y;
public:
Point() : X(0), Y(0){};
Point(double x, double y) : X(x), Y(y){};
};
class Circle : public Point
{
double R;
friend double GetArea(Circle*);
public :
Circle() : R(0){};
Circle(double r) : R(r){};
inline double GetR() { return R; };
inline void SetR(double r) { R = r; };
};
class Cylinder : public Circle
{
double H;
friend double GetVolume(Cylinder*);
public:
Cylinder() : H(0) {};
Cylinder(double r, double h) : Circle(r), H(h){};
inline double GetH() { return H; };
inline void SetH(double r) { H = r; };
};
void main()
{
double r,h;
cout << "请输入圆半径:"<<endl;
cin >> r;
Circle c(r);
cout << "请输入圆柱底面半径和圆柱高:"<<endl;
cin >> r >> h;
Cylinder cy(r, h);
cout << "圆面积为:" << GetArea(&c) << endl;
cout << "圆柱底面积为:" << GetArea(&cy) << "圆柱体积为:" << GetVolume(&cy) << endl;
}
double GetArea(Circle* c)
{
return PI*c->R*c->R;
}
double GetVolume(Cylinder* c)
{
return GetArea(c)*c->H;
}