#include<iostream> #include<cmath> using namespace std; #define PI 3.1415926 class Point { public: void setPoint(float i,float j); float getX(){return x;} float getY(){return y;} float dr(Point &p1,Point &p2); private: float x; float y; }; void Point::setPoint(float i,float j) { x=i; y=j; } float Point::dr(Point &p1,Point &p2) { float dr=0,dx=0,dy=0; dx=p1.x-p2.x; dy=p1.y-p2.y; dr=sqrt(dx*dx+dy*dy); return dr; } class Circle:public Point { public: Circle(float x1,float y1,float x2,float y2){midpoint.setPoint(x1,y1);aroundpoint.setPoint(x2,y2);} float setR(); void circle_area(); float getR(){return r;} float getarea(){return area;} float lcircle(); private: float area; float r; Point midpoint; Point aroundpoint; }; float Circle::setR() { r=dr(midpoint,aroundpoint); return r; } void Circle::circle_area() { r=setR(); area=PI*r*r; cout<<"底面圆的面积为:"<<area<<endl; } float Circle::lcircle() { r=setR(); float l; l=2*PI*r; return l; } class Cylinder:public Circle { public: Cylinder(float x1,float y1,float x2,float y2,float h1):Circle(x1,y1,x2,y2),h(h1){} void cylinderarea(); void cylinder_volume(); private: float h; }; void Cylinder::cylinderarea() { float s; s=2*getarea()+lcircle()*h; cout<<"圆柱体的表面积为:"<<s<<endl; } void Cylinder::cylinder_volume() { float v; v=getarea()*h; cout<<"圆柱体的体积为:"<<v<<endl; } int main() { Cylinder cy1(0,0,3,4,1.0); cy1.circle_area(); cy1.cylinderarea(); cy1.cylinder_volume(); return 0; }
运行结果
时间: 2024-11-06 19:17:47