Circle派生出Cylinder类

来源:互联网 发布:淘宝店铺积分怎么算 编辑:程序博客网 时间:2024/06/11 19:55

程序代码

#include <iostream>using namespace std;class Point//点类{public:    //使用初始化表初始化点类    Point(double a = 0, double b = 0):x(a), y(b){}    void setPoint(double a, double b);//设置点的坐标    double getX();    double getY();    //重载<<实现点的坐标的输出    friend ostream& operator<<(ostream &output, Point &p);protected:    double x;//横坐标    double y;//纵坐标};//设置点的坐标void Point::setPoint(double a, double b){    x = a;    y = b;}//得到x的值double Point::getX(){    return x;}//得到y的值double Point::getY(){    return y;}//重载<<实现点的坐标的输出ostream& operator<<(ostream &output, Point &p){    output<<"("<<p.x<<","<<p.y<<")"<<endl;    return output;}//Point派生出Circle类class Circle : public Point{public:    //构造函数(初始化半径的两个端点)    Circle(double a, double b, double r);   void SetRadius(double r);//设置圆的半径   double GetRadius();//得到圆的半径   double getArea();//计算圆的面积     //重载<<实现输出圆的面积  friend ostream& operator<<(ostream &output, Circle &c);protected:    double r;//圆的半径};//构造函数(使用初始化表初始化Circle类)Circle::Circle(double a, double b, double r): Point(a, b), r(r){}//设置圆的半径void Circle::SetRadius(double r){    this->r = r;}//得到圆的半径double Circle::GetRadius(){    return r;}//计算圆的面积double Circle::getArea(){    //计算圆的面积    double s = 3.14 * r * r;    return s;}//重载<<输出圆的面积ostream& operator<<(ostream &output, Circle &c){    output<<"圆的面积:"<<c.getArea()<<endl;    return output;}//圆类派生出圆柱类class Cylinder : public Circle{public:    Cylinder(double a = 0, double b = 0, double r = 0, double h = 0);//构造函数      void setHeight(double h);//设置圆柱体的高度    double SuperArea();//计算圆柱体的表面积    double setVolume();//计算圆柱体的的体积     //重载<<实现输出圆柱体的表面积和体积  friend ostream& operator<<(ostream &output, Cylinder &cy);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::SuperArea(){    double s;    s = 2 * Circle::getArea() + 2 * 3.14 * Circle::GetRadius() * height;    return s;}//计算圆柱体的的体积double Cylinder::setVolume(){    double v;    v = Circle::getArea() * height;    return v;}//重载<<实现输出圆柱体的表面积和体积ostream& operator<<(ostream &output, Cylinder &cy){    output<<"圆柱体的表面积:"<<cy.SuperArea()<<"\n"<<"圆柱体的体积:"<<cy.setVolume()<<endl;    return output;} void main(){    //定义一个圆柱体对象    Cylinder c1;    //设置点的坐标    c1.setPoint(1.0, 2.0);    //设置圆的半径    c1.SetRadius(2.0);    //设置圆柱体的高为1.0    c1.setHeight(1.0);    //输出圆柱体的表面积和体积    cout<<c1;    system("pause");}


执行结果


0 0