第十一周项目4-类族的设计

按以下的提示,由基类的设计和测试开始,逐渐地完成各个类的设计,求出圆格柱体的表面积、体积并输出并且完成要求的计算任务:

(1)先建立一个Point(点)类,包含数据成员x,y(坐标点),实现需要的成员函数,并设计main函数完成测试;

/*
 *Copyright (c) 2015,烟台大学计算机学院
 *All gight reserved.
 *文件名称:Demo.cpp
 *作者:邵帅
 *完成时间:2015年05月25日
 *版本号:v1.0
*/
#include <iostream>
using namespace std;
class Point
{
public:
    Point(double x=0,double y=0);
    void setpoint(double x0,double y0)
    {
        x=x0;
        y=y0;
    }
    double getx(){return x;}
    double gety(){return y;}
private:
    double x,y;
};
Point::Point(double x0,double y0):x(x0),y(y0){}
int main()
{
    Point p;
    p.setpoint(2,3);
    cout<<"x="<<p.getx()<<endl;
    cout<<"y="<<p.gety()<<endl;
}


(2)以Point为基类,派生出一个Circle(圆)类,增加数据成员r(半径),以及求面积的成员函数area,实现其他需要的成员函数,设计main函数完成测试;

/*
 *Copyright (c) 2015,烟台大学计算机学院
 *All gight reserved.
 *文件名称:Demo.cpp
 *作者:邵帅
 *完成时间:2015年05月25日
 *版本号:v1.0
*/
#include <iostream>
using namespace std;
class Point
{
public:
    Point(double x=0,double y=0);
    void setpoint(double a,double b)
    {
        x=a;
        y=b;
    }
    double getx(){return x;}
    double gety(){return y;}
protected:
    double x,y;
};
Point::Point(double a,double b):x(a),y(b){}
class Circle:public Point
{
public:
    Circle(double x=0,double y=0,double r=0);
    void setradius(double);
    double getradius();
    double area();
protected:
    double radius;
};
Circle::Circle(double a,double b,double r):Point(a,b),radius(r){}
void Circle::setradius(double r0)
{
    radius=r0;
}
double Circle::getradius() {return radius;}
double Circle::area()
{
    double s;
    const double pi=3.1415;
    s=pi*radius*radius;
    return s;
}
int main()
{
    Circle p;
    p.setpoint(2,3);
    p.setradius(3);
    cout<<"圆心坐标:"<<endl;
    cout<<"x="<<p.Point::getx()<<endl;
    cout<<"y="<<p.Point::gety()<<endl;
    cout<<"圆的半径=";
    cout<<p.getradius()<<endl;
    cout<<"圆的面积=";
    cout<<p.area();
}


(3)再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,再增加数据成员h(高),,以及求圆柱表面积的成员函数area和求圆柱体积的成员函数volume,实现需要的成员函数,并设计main函数完成测试。

/*
 *Copyright (c) 2015,烟台大学计算机学院
 *All gight reserved.
 *文件名称:Demo.cpp
 *作者:邵帅
 *完成时间:2015年05月25日
 *版本号:v1.0
*/
#include <iostream>
using namespace std;
class Point
{
public:
    Point(double x=0,double y=0);
    void setpoint(double a,double b)
    {
        x=a;
        y=b;
    }
    double getx(){return x;}
    double gety(){return y;}
protected:
    double x,y;
};
Point::Point(double a,double b):x(a),y(b){}
class Circle:public Point
{
public:
    Circle(double x=0,double y=0,double r=0);
    void setradius(double);
    double getradius();
    double area();
protected:
    double radius;
};
Circle::Circle(double a,double b,double r):Point(a,b),radius(r){}
void Circle::setradius(double r0)
{
    radius=r0;
}
double Circle::getradius() {return radius;}
double Circle::area()
{
    double s;
    const double pi=3.1415;
    s=pi*radius*radius;
    return s;
}
class Cylinder:public Circle
{
public:
    Cylinder(double x=0,double y=0,double r=0,double h=0);
    void setheight(double);
    double getheight();
    double volume();
    double larea();
protected:
    double height;
};
Cylinder::Cylinder(double a,double b,double r,double h):Circle(a,b,r),height(h){}
void Cylinder::setheight(double h)
{
    height=h;
}
double Cylinder::getheight(){return height;}
double Cylinder::larea()
{
    double s;
    s=2*Circle::area( )+2*3.14159*radius*height;
    return s;
}
double Cylinder::volume()
{
    double v;
    v=area()*height;
    return v;
}
int main()
{
    Cylinder p;
    p.setpoint(2,3);
    p.setradius(3);
    p.setheight(5);
    cout<<"圆柱底圆圆心坐标:"<<endl;
    cout<<"x="<<p.Point::getx()<<endl;
    cout<<"y="<<p.Point::gety()<<endl;
    cout<<"圆柱底圆圆的半径=";
    cout<<p.getradius()<<endl;
    cout<<"圆柱的表面积=";
    cout<<p.larea()<<endl;
    cout<<"圆柱的高=";
    cout<<p.getheight()<<endl;
    cout<<"圆柱的体积=";
    cout<<p.volume();
}


@ Mayuko

posted @ 2015-05-25 20:58  麻麻麻麻鱼鱼  阅读(195)  评论(0编辑  收藏  举报