10.3 Point(点)类 的多层派生

来源:互联网 发布:汽车电脑编程设备 编辑:程序博客网 时间:2024/05/22 02:00

程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 第十周 任务三
* 作 者:       杨森
* 完成日期: 2012年 04 月 25 日
* 版 本 号: V 1.0

 

源程序:

#include<iostream>     #define pi 3.14     using namespace std;    class Point   {  protected:      double x;        double y;  public:        Point(double x0 = 0,double y0 = 0):x(x0),y(y0){}        friend ostream& operator <<(ostream& output, Point& cp);        Point getPoint(double,double);        double getX(double);        double getY(double);          ~Point();  };  class Circle : public Point  {  protected:      double r;    public:        Circle(double x0, double y0 , double r0):Point(x0,y0)      {          r = r0;      }      friend ostream& operator <<(ostream& output, Circle& cri);        Point getCri_cen(double,double);        double getRadii(double);        ~Circle();  };  class Cylinder : public Circle  {  private:      double h;  public:      Cylinder(double x0, double y0, double r0, double h0):Circle(x0,y0,r0)      {          h = h0;      }      friend ostream& operator <<(ostream& output, Cylinder& cyl);        Circle getCircle(double,double,double);        double getHigh(double);        void Voluem();        void Area();        ~Cylinder();  };  //Point类的有关定义   ostream& operator <<(ostream& output, Point& cp)  {      output << '(' << cp.x << ',' << cp.y << ')' ;        return output;  }  Point Point::getPoint(double x1,double y1)  {      cout <<"请输入要修改为的点的坐标:" <<endl;        cin >> x1 >> y1;        x = x1;        y = y1;        return Point(x, y);  }  double Point::getX(double x1)  {      cout << "请输入要改的横坐标:" <<endl;        cin >> x1;        return (x = x1);  }  double Point::getY(double y1)  {      cout << "请输入要改的纵坐标:" <<endl;        cin >> y1;        return(y = y1);  }  Point::~Point(){}    //Circle类的有关定义   ostream& operator <<(ostream& output, Circle& cir)  {      output << "圆心是:" << '(' << cir.x << ',' << cir.y << ')';        output << "半径是:" << cir.r;        return output;  }  Point Circle::getCri_cen(double x1,double y1)  {      cin >> x1 >> y1;        x = x1;        y = y1;        return Point(x, y);  }    double Circle::getRadii(double r1)  {      cin >> r1;        return (r = r1);  }    Circle::~Circle(){}    //Cylinder类的有关定义   ostream& operator <<(ostream& output, Cylinder& cyl)  {      output << "底面圆心是:" << '(' << cyl.x << ','<< cyl.y << ')';        output << "  底面半径是:" << cyl.r;        output << "  高是:" << cyl.h;            return output;  }    Circle Cylinder::getCircle(double x1,double y1,double r1)  {      cin >> x1 >> y1 >> r1;        return Circle(x = x1, y = y1, r = r1);  }    double Cylinder::getHigh(double h1)  {      cin >> h1;        return (h = h1);  }    void Cylinder::Voluem()  {      cout << (pi* r * r * h);  }    void Cylinder::Area()  {      cout << ( 2 * pi * r * r + 2 * pi * r * h);  }    Cylinder::~Cylinder(){};  int main()  {      Point cp1(-2,5), cp2(3,7);        double x1 = 0, y1 = 0, r1 = 0, h1 = 0;        cout << "对Point类进行测试:" <<endl;        cout << "该点坐标是:" << cp1 <<endl;        cp1.getPoint(x1,y1);        cout << "改正后的点为:" << cp1 <<endl;        cp1.getX(x1);        cout << "改正后的点为:" << cp1 <<endl;        cp1.getY(y1);        cout << "改正后的点为:" << cp1 <<endl;              Circle cir1(2,2,5);        cout << "对Circle类进行测试:" <<endl;          cout <<"圆的信息为:" <<endl;        cout << cir1 <<endl;        cout << "请输入想要设定的圆心:" <<endl;        cir1.getCri_cen(x1,y1);        cout << cir1;        cout <<endl;        cout << "请输入想要设定的半径:"<<endl;        cir1.getRadii(r1);        cout << cir1;        cout <<endl;            Cylinder cyl1(2,2,4,3);        cout << "以下是对Cylinder类的测试:"<<endl;        cout << "圆柱体信息为:" ;        cout << cyl1;        cout <<endl;        cout << "圆柱体的体积是:";        cyl1.Voluem();        cout <<endl;        cout << "圆柱体的表面积是:" ;        cyl1.Area();        cout <<endl;        cout << "请输入想要设定的底面:" ;        cyl1.getCircle(x1,y1,r1);        cout <<endl;        cout << "请输入想要设定的高:" ;        cyl1.getHigh(h1);        cout <<endl;        cout << "修改后圆柱体的体积是:" ;        cyl1.Voluem();        cout <<endl;        cout << "修改后圆柱体的表面积是:" ;        cyl1.Area();        cout <<endl;        system("pause");        return 0;  }  


运行结果:

 

对Point类进行测试:  该点坐标是:(-2,5)  请输入要修改为的点的坐标:  2 3  改正后的点为:(2,3)  请输入要改的横坐标:  5  改正后的点为:(5,3)  请输入要改的纵坐标:  7  改正后的点为:(5,7)  对Circle类进行测试:  圆的信息为:  圆心是:(2,2)半径是:5  请输入想要设定的圆心:  3 4  圆心是:(3,4)半径是:5  请输入想要设定的半径:  6  圆心是:(3,4)半径是:6  以下是对Cylinder类的测试:  圆柱体信息为:底面圆心是:(2,2)  底面半径是:4  高是:3  圆柱体的体积是:150.72  圆柱体的表面积是:175.84  请输入想要设定的底面:3 5 7    请输入想要设定的高:2    修改后圆柱体的体积是:307.72  修改后圆柱体的表面积是:395.64  请按任意键继续. . 


小感:今天才发现,竟然忘了交······

原创粉丝点击