第十一周项目1.3— 点 圆 圆柱类族的设计

问题及代码:

/*

*Copyright(c) 2016.烟台大学计算机与控制工程学院

*ALL rights  reserved.

*文件名称:main.cpp

*作者:郝昱猛

*完成日期:2016年5月18日

*问题描述:问题描述:按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积、
           体积并输出并且完成要求的计算任务:
          (1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试;
          (2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其
           他需要的成员函数,设计main函数完成测试;
          (3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高),,以及求圆柱表面
           积的成员函数area和求圆柱体积的成员函数volume,实现需要的成员函数,并设计main函数完成测试。

*/
#include <iostream>
using namespace std;
#define  PI 3.1415926
class Point
{
public:
    Point(double xx,double yy):x(xx),y(yy){}
    double getX( )
    {
        return x;
    }
    double getY( )
    {
        return y;
    }
    void show();
protected:
    double x,y;
};
void Point::show()
{
    cout<<"("<<x<<","<<y<<")"<<endl;
}
class Circle:public Point //定义Point的派生类Circle
{
public:
    Circle(double xx,double yy,double rr):Point(xx,yy),r(rr){}
    void setR(double rr);
    double getR()
    {
        return r;
    }
    double area();
    void show();
protected:
    double r;
};
void Circle::setR(double rr)
{
    r=rr;
}
double Circle::area()
{
    return PI*r*r;
}
void Circle::show()
{
    cout<<"中心为:("<<x<<","<<y<<") 半径为:"<<r<<"面积为:"<<area()<<endl;
}
class Cylinder:public Circle
{
 public:
     Cylinder(double xx,double yy,double rr,double hh):Circle(xx,yy,rr),h(hh){}
     void setH(double hh);
     double getH()
     {
         return h;
     }
     double area();
     double volume();
     void show();
 protected:
    double h;
};
void Cylinder::setH(double hh)
{
    h=hh;
}
double Cylinder::area()
{
    return 2*Circle::area()+2*PI*r*h;
}
double Cylinder::volume()
{
    return Circle::area()*h;
}
void Cylinder::show()
{
    cout<<"面积为:"<<area()<<"体积为:"<<volume()<<endl;
}
int main()
{
    Point p1(3,5);
    cout<<"点"<<endl;
    p1.show();
    Circle c(1,1,2);
    cout<<"圆形"<<endl;
    c.show();
    cout<<"圆柱"<<endl;
    Cylinder cy(1,1,2,3);
    cy.show();
    return 0;
}

运行结果:



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值