第十、十一周项目1 - 点-圆-圆柱类族的设计(3)

#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;  
}  

运行结果


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值