#include <iostream> using namespace std; const double pi = 3.14; class Point { protected: double x,y; public: Point( double xx = 0, double yy = 0 ): x(xx), y(yy) {} void SetPoint( double a, double b ) { x = a; y = b; } double GetX() { return x; } double GetY() { return y; } friend ostream & operator << ( ostream &output, const Point &p ) { output << "[" << p.x << "," << p.y << "]" << endl; return output; } }; class Circle:public Point { protected: double r; public: Circle( double xx = 0, double yy = 0, double rr = 0 ):Point( xx, yy ),r(rr) {} void SetR( double rr ) { r = rr; } double GetR() { return r; } double Area() { double ans = pi * r * r; return ans; } friend ostream & operator << ( ostream &output, const Circle &c ) { output << "[" << c.x << "," << c.y << "]" << ' ' << c.r << endl; return output; } }; class Cyclinder:public Circle { protected: double height; public: Cyclinder( double xx = 0, double yy = 0, double rr = 0, double hh = 0):Circle(xx, yy, rr), height(hh) {} void SetHeght( double hh ) { height = hh; } double GetHeight() { return height; } double Area() { return 2.0 * Circle::Area() + 2 * pi * r * height; } double volume() { return Circle::Area() * height; } friend ostream& operator << ( ostream &output, Cyclinder& cy ) { output << "[" << cy.x << "," << cy.y << "]" << "r = " << cy.r << " area = " << cy.Area() << " volume = " << cy.volume() << endl; return output; } }; int main() { Point a(1,2); double x = a.GetX(); double y = a.GetY(); cout << a << endl; cout << x << ' ' << y << endl; Circle c(1,2,3); cout << c << endl; double area = c.Area(); cout << area << endl; Cyclinder c1(3.5,6.4,5.2,10); cout << c1 << endl; return 0; }
c++ 类的继承与派生 随便写的练习
猜你喜欢
转载自blog.csdn.net/xutian_curry/article/details/80483816
今日推荐
周排行