您的位置:首页 > 其它

2015年大一下第8周项目3-类族的设计

2015-05-14 16:00 225 查看
/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:Annpion.cpp
*作者:王耀鹏
*完成日期:2015年5月14日
*版本号:v1.0
*
*问题描述:由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积、体积并输出并且完成要求的计算任务。
*输入描述:无。
*输出描述:圆柱体的表面积,体积等信息。
*/
#include<iostream>
using namespace std;
class Point
{
public:
    Point(double a,double b):x(a),y(b) {};
    double getx();
    double gety();
    void setXY(double a,double b);
    friend ostream &operator <<(ostream &out,Point &p);
protected:
    double x,y;
};
double Point::getx()
{
    return x;
}
double Point::gety()
{
    return y;
}
void Point::setXY(double a,double b)
{
    x=a;
    y=b;
}
ostream &operator <<(ostream &out,Point &p)
{
    cout<<"("<<p.x<<","<<p.y<<")"<<endl;
    return out;
}
class Circle:public Point
{
public:
    Circle(double a,double b,double c):Point(a,b),r(c) {};
    double area();
    double getR();
    void setR(double a);
    friend ostream &operator <<(ostream & out ,Circle &c);
protected:
    double r;
};
double Circle::area()
{
    return 3.1415926*r*r;
}
double Circle::getR()
{
    return r;
}
void Circle::setR(double a)
{
    r=a;
}
ostream &operator <<(ostream & out ,Circle &c)
{
    cout<<"该圆是以"<<"("<<c.x<<","<<c.y<<")为圆心,"<<c.r<<"为半径的圆。该圆的面积为:"<<c.area()<<endl;
    return out;
}
class Cylinder:public Circle
{
public:
    Cylinder(double a,double b,double c,double d):Circle(a,b,c),h(d) {};
    double Sarea();
    double volume();
    double getH();
    void setH(double a);
    friend ostream &operator <<(ostream &out ,Cylinder &c);
private:
    double h;
};
double Cylinder::Sarea()
{
    return 3.1415926*2*r*h+3.1415926*r*r*2;
}
double Cylinder::volume()
{
    return 3.1415926*r*r*h;
}
double Cylinder::getH()
{
    return h;
}
void Cylinder::setH(double a)
{
    h=a;
}
ostream &operator<<(ostream &out ,Cylinder &c)
{
    cout<<"该圆柱是以"<<"("<<c.x<<","<<c.y<<")为中心,"<<c.r<<"为半径,"<<c.h<<"为高的圆柱体。该圆柱体的表面积为:"<<c.Sarea()<<",体积为:"<<c.volume()<<endl;
    return out;
}
int main( )
{
    Cylinder cy1(3.5,6.4,5.2,10);
    cout<<"\noriginal cylinder:\nx="<<cy1.getx( )<<", y="<<cy1.gety( )<<", r="
        <<cy1.getR( )<<", h="<<cy1.getH( )<<"\narea="<<cy1.Sarea()
        <<",volume="<<cy1.volume()<<endl;
    cy1.setH(15);
    cy1.setR(7.5);
    cy1.setXY(5,5);
    cout<<"\nnew cylinder:\n"<<cy1;
    return 0;
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: