#include <iostream.h> //using namespace std; class Point { private: double x; double y; public: Point(double x1,double y1) { x=x1; y=y1; } ~Point()//析构函数 { cout<<"The constructor of Point was called"<<endl; } }; class Circle:public Point { public: double r; Circle(double x1,double y1,double r1):Point(x1,y1)//派生类构造函数 { r=r1;//新增量 } ~Circle() { cout<<"The constructor of Circle was called"<<endl; } }; class Cylinder:public Circle { private: double h; public: Cylinder(double x1,double y1,double r1,double h1):Circle(x1,y1,r1) { h=h1;//新增量 } ~Cylinder() { cout<<"The constructor of Cylinder was called"<<endl; } double set_volume();//求体积 double set_area();//求面积 friend ostream &operator<<(ostream &output,Cylinder &);//重载输出流 //void show(); }; double Cylinder::set_area() { return(2*3.14*r*(h+r)); } double Cylinder::set_volume() { return(3.14*r*r*h); } ostream &operator<<(ostream &output,Cylinder &c) { output<<"The area of the Cylinder is "<<c.set_area()<<endl; output<<"The volume of the Cylinder is "<<c.set_volume()<<endl; return output; } int main() { Cylinder c1(3.2,3,2,5); c1.set_area(); c1.set_volume(); cout<<c1; return 0; }
点Point 圆Circle 圆柱Cylinder的继承与派生
努力加载评论中...
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步