1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| #include<iostream> #include<iomanip> using namespace std; class Point { friend ostream& operator<<(ostream& output, const Point& p); protected: double x; double y; public: Point(double d1 = 0.0, double d2 = 0.0) :x(d1), y(d2) {} void setPoint(double d1, double d2) { x = d1, y = d2; return; } double getX()const { return x; } double getY()const { return y; } }; class Circle :public Point { friend ostream& operator<<(ostream& output, const Circle& c); protected: double radius; public: Circle(double r = 0.0, double d1 = 0.0, double d2 = 0.0):radius(r),Point(d1,d2) {} void setRadius(double r) { radius = (r > 0 ? r : 0); return; } double getRadius()const { return radius; } double area()const { return acos(-1) * radius * radius; } }; class Cylinder :public Circle { friend ostream& operator<<(ostream& output, const Cylinder& c); protected: double height; public: Cylinder(double h=0.0,double r=0.0,double d1=0.0,double d2=0.0):height(h),Circle(r,d1,d2){} void setHeight(double h) { height = (h > 0 ? h : 0); return; } double getHeight()const { return height; } double area()const { return 2 * Circle::area() + 2 * acos(-1) * radius * height; } double volume()const { return Circle::area() * height; }
};
ostream& operator<<(ostream& output, const Point& p) { output << "Point: (" << p.x << "," << p.y << ")" << endl << endl; return output; } ostream& operator<<(ostream& output, const Circle& c) { output << "Circle:" << endl; output << "Center: (" << c.x << "," << c.y << ")" << endl; output << "Radius:" << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << c.radius << endl; return output; } ostream& operator<<(ostream& output, const Cylinder& c) { output << "Cylinder:" << endl; output << "Center: (" << c.x << "," << c.y << ")" << endl; output << "Radius:" << setiosflags(ios::fixed | ios::showpoint) << setprecision(2) << c.radius << endl; output << "Height:" << c.height << endl; return output; } int main() { Point p(2.5, 3.5); cout << "p:" << endl; cout << p; p.setPoint(9, 10); cout << "change Point p:" << endl; cout << p;
Circle c(8.1, 3.1, 4.1); cout << "c:" << endl; cout << c; cout << "c area:" << c.area() << endl; cout << endl; c.setRadius(8); c.setPoint(1, 2); cout << "chaneg Circle c:" << endl; cout << c; cout << "c area:" << c.area() << endl; cout << endl;
Cylinder cy(5.5, 2.5, 3.5, 4.5); cout << "cy:" << endl; cout << cy; cout << "cy area:" << cy.area() << endl; cout << "cy volume:" << cy.area() << endl; cout << endl; cy.setHeight(5); cy.setRadius(2); cy.setPoint(3, 4); cout << "chaneg Cylinder cy:" << endl; cout << cy; cout << "cy area:" << cy.area() << endl; cout << "cy volume:" << cy.volume() << endl;
return 0; }
|