码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-05-12 20:57:47      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

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

运行结果

技术分享

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

标签:

原文地址:http://blog.csdn.net/ccxucong/article/details/51355540

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!