第十周C++实验报告(三)

来源:互联网 发布:mac粉色系口红 编辑:程序博客网 时间:2024/06/06 03:34
[cpp] view plaincopy
  1. #include<iostream>  
  2. #define pi 3.1415  
  3. using namespace std;  
  4. class Point  
  5. {  
  6. public:  
  7.     int x;  
  8.     int y;  
  9. //public:  
  10.     Point (int xx = 0, int yy = 0):x(xx), y(yy){};  
  11.     ~Point();  
  12.     void setPoint(int a, int b);  
  13.     friend ostream & operator << (ostream & out, Point &a);  
  14.   
  15. };  
  16. Point::~Point ()  
  17. {  
  18.   
  19. }  
  20. ostream & operator << (ostream & out, Point & a)  
  21. {  
  22.     out << "(" << a.x << "," << a.y << ")" << endl;  
  23.     return out;  
  24.   
  25. }  
  26. void Point::setPoint (int a, int b)  
  27. {  
  28.     x = a;  
  29.     y = b;  
  30. }  
  31. class Circle: public Point   
  32. {  
  33. public:  
  34.     int r;  
  35. public:  
  36.     Circle (int xx, int yy, int r1):Point (xx, yy){r = r1;}  
  37.     ~Circle();  
  38.     void setCircle(int a, int b, int c);  
  39.     friend ostream & operator << (ostream & out, Circle & a);  
  40.   
  41. };  
  42.   
  43. Circle::~Circle()  
  44. {  
  45.   
  46. }  
  47.   
  48. void Circle::setCircle(int a, int b, int c)  
  49. {  
  50.     x = a;  
  51.     y = b;  
  52.     r = c;  
  53. }  
  54.   
  55. ostream & operator << (ostream & out, Circle & a)  
  56. {  
  57.     out << "圆心:"<< "(" << a.x << "," << a.y << ")" << endl;  
  58.     out << "半径:" << a.r;  
  59.     return out;  
  60. }  
  61. class Cylinder: public Circle  
  62. {  
  63. private:  
  64.     int h;  
  65. public:  
  66.     Cylinder(int xx, int yy, int r1, int h1):Circle(xx, yy, r1){h = h1;}  
  67.     ~Cylinder();  
  68.     void setCylinder(int a, int b, int c, int d);  
  69.     friend ostream & operator << (ostream &out, Cylinder &a);  
  70.     double Carea();  
  71.     double Cvolume();  
  72. };  
  73. Cylinder::~Cylinder()  
  74. {  
  75.   
  76. }  
  77.   
  78. void Cylinder::setCylinder(int a, int b, int c, int d)  
  79. {  
  80.     x = a;  
  81.     y = b;  
  82.     r = c;  
  83.     h = d;  
  84. }  
  85. ostream & operator << (ostream &out, Cylinder &a)  
  86. {  
  87.     out <<"圆心:" << "("<< a.x <<"," <<a.y <<")" << endl;  
  88.     out<<"半径:" << a.r << endl;  
  89.     out<< "高: " << a.h << endl;  
  90.     return out;  
  91. }  
  92.   
  93. double Cylinder::Carea()  
  94. {  
  95.     double m;  
  96.     m = 2 * r * pi;  
  97.     return (m * h);  
  98. }  
  99.   
  100. double Cylinder::Cvolume()  
  101. {  
  102.     return (r * r * pi * h);  
  103. }  
  104. int main()  
  105. {  
  106.     Cylinder c(2, 2, 2 ,2);  
  107.     cout << c;  
  108.     cout<< "表面积:" << c.Carea () << endl;  
  109.     cout << "体积:" << c.Cvolume () << endl;  
  110.     system("pause");  
  111.     return 0;  
  112. }  

原创粉丝点击