#include<iostream> using namespace std; const double PI=3.14159265358979323846; class Point{ public: Point(double xx=0,double yy=0):x(xx),y(yy){} friend istream& operator>>(istream& is,Point&p); friend ostream& operator<<(ostream& os, const Point&p); protected: double x,y; }; istream& operator>>(istream& is,Point& p) { is>>p.x>>p.y; return is; } ostream& operator<<(ostream& os,const Point& p) { os<<"("<<p.x<<","<<p.y<<")"; return os; } class Circle:public Point{ public: Circle(double xx=0,double yy=0,double rr=0):Point(xx,yy),r(rr){} double area() const{return PI*r*r;} friend istream& operator>>(istream&is,Circle&c); friend ostream& operator<<(ostream&os,Circle&c); protected: double r; } istream& operator>>(istream& is,Circle& c){ is>>static_cast<Point&>(c)>>c.r; return is; } ostream& operator<<(ostream& os,const Circle& c) { os<<"Center :"<<static_cast<Point&>(c)<<". Radius:"<<c.r; return os; } class Cylinder:public Point{ public: Cylinder(double xx=0,dpuble yy=0,double rr=0,double hh=0):Circle(xx,yy,rr),h(hh){} double volume() const{return Circle::area()*h;} friend istream& operator>>(istream&is,Cylinder&cy); friend ostream& operator<<(ostream&os,const Cylinder&cy); protected: double h; } istream& operator>>(istream& is,Cylinder& cy){ is>>static_cast<Point&>(cy)>>cy.h; return is; } ostream& operator>>(ostream& os,const Cylinder& cy) { os<<"Base :"<<static_cast<Point&>(cy)<<". Height:"<<cy.h; return os; } int main() { Circle c(0,0,1); cout<<"Input Circle info:"; cin>>c; cout<<c<<endl; cout<<"Circle area:"<<c.area()<<endl; Cylinder cy(0,0,1,2); cout<<"Input Cylinder info"; cin>>cy; cout<<cy<<endl; cout<<"Cylinder volume:"<<cy.volume()<<endl; return 0; }
时间: 2024-04-26 20:21:44 浏览: 61
这是一个C++程序,实现了一个基本的图形类的继承关系,包括Point(点)、Circle(圆)、Cylinder(圆柱体)三个类。其中,Circle和Cylinder类都继承自Point类,表示圆心和圆柱底部的位置。Circle类实现了圆的面积计算,Cylinder类实现了圆柱体的体积计算。程序通过重载输入输出运算符实现了用户输入和输出图形信息的功能。
阅读全文