用指针输出几何体
#include <iostream> using namespace std; class Point { public: Point(double x=0,double y=0); friend ostream & operator<<(ostream &,const Point &); protected: double x,y; }; Point::Point(double a,double b):x(a), y(b){} ostream & operator<<(ostream &output,const Point &p) { output<<" 点:["<<p.x<<","<<p.y<<"]"<<endl; return output; } class Circle:public Point { public: Circle(double x=0,double y=0,double r=0); double area ( ) const; friend ostream &operator<<(ostream &,const Circle &); protected: double radius; }; Circle::Circle(double a,double b,double r):Point(a,b),radius(r) { } double Circle::area( ) const { return 3.14159*radius*radius; } ostream &operator<<(ostream &output,const Circle &c) { output<<" 圆:Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area( )<<endl; return output; } class Cylinder:public Circle { public: Cylinder (double x=0,double y=0,double r=0,double h=0); double area( ) const; friend ostream& operator<<(ostream&,const Cylinder&); protected: double height; }; Cylinder::Cylinder(double a,double b,double r,double h):Circle(a,b,r),height(h) {} double Cylinder::area( ) const { return 2*Circle::area( )+2*3.14159*Circle::radius*height; } ostream &operator<<(ostream &output,const Cylinder& cy) { output<<" 圆柱:Center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height <<",area="<<cy.area( )<<endl; return output; } int main( ) { Cylinder cy1(3.5, 6.4, 5.2, 10); cout<<cy1<<endl; Point *p=&cy1; cout<<"用指向点的指针输出:"<<endl; cout<<(*p); Circle *c= &cy1; cout<<"用指向圆的指针输出:"<<endl; cout<<(*c); Cylinder *cy= &cy1; cout<<"用指向圆柱的指针输出:"<<endl; cout<<(*cy)<<endl; return 0; }
用引用输出几何体
#include <iostream> using namespace std; class Point { public: Point(double x=0,double y=0); friend ostream & operator<<(ostream &,const Point &); protected: double x,y; }; Point::Point(double a,double b):x(a), y(b){} ostream & operator<<(ostream &output,const Point &p) { output<<" 点:["<<p.x<<","<<p.y<<"]"<<endl; return output; } class Circle:public Point { public: Circle(double x=0,double y=0,double r=0); double area ( ) const; friend ostream &operator<<(ostream &,const Circle &); protected: double radius; }; Circle::Circle(double a,double b,double r):Point(a,b),radius(r) { } double Circle::area( ) const { return 3.14159*radius*radius; } ostream &operator<<(ostream &output,const Circle &c) { output<<" 圆:Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area( )<<endl; return output; } class Cylinder:public Circle { public: Cylinder (double x=0,double y=0,double r=0,double h=0); double area( ) const; friend ostream& operator<<(ostream&,const Cylinder&); protected: double height; }; Cylinder::Cylinder(double a,double b,double r,double h):Circle(a,b,r),height(h) {} double Cylinder::area( ) const { return 2*Circle::area( )+2*3.14159*Circle::radius*height; } ostream &operator<<(ostream &output,const Cylinder& cy) { output<<" 圆柱:Center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height <<",area="<<cy.area( )<<endl; return output; } int main( ) { Cylinder cy1(3.5, 6.4, 5.2, 10); cout<<cy1<<endl; cout<<"使用引用:"<<endl; Point &pRef=cy1; cout<<"用点的引用输出:"<<endl; cout<<pRef<<endl; Circle &cRef=cy1; cout<<"用圆的引用输出:"<<endl; cout<<cRef<<endl; Cylinder &cyRef=cy1; cout<<"用圆柱的引用输出:"<<endl; cout<<cyRef<<endl; return 0; }
输出几何体面积
#include <iostream> using namespace std; class Point { public: Point(double x=0,double y=0); friend ostream & operator<<(ostream &,const Point &); protected: double x,y; }; Point::Point(double a,double b):x(a), y(b){} ostream & operator<<(ostream &output,const Point &p) { output<<" 点:["<<p.x<<","<<p.y<<"]"<<endl; return output; } class Circle:public Point { public: Circle(double x=0,double y=0,double r=0); double area ( ) const; friend ostream &operator<<(ostream &,const Circle &); protected: double radius; }; Circle::Circle(double a,double b,double r):Point(a,b),radius(r) { } double Circle::area( ) const { return 3.14159*radius*radius; } ostream &operator<<(ostream &output,const Circle &c) { output<<" 圆:Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area( )<<endl; return output; } class Cylinder:public Circle { public: Cylinder (double x=0,double y=0,double r=0,double h=0); double area( ) const; friend ostream& operator<<(ostream&,const Cylinder&); protected: double height; }; Cylinder::Cylinder(double a,double b,double r,double h):Circle(a,b,r),height(h) {} double Cylinder::area( ) const { return 2*Circle::area( )+2*3.14159*Circle::radius*height; } ostream &operator<<(ostream &output,const Cylinder& cy) { output<<" 圆柱:Center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height <<",area="<<cy.area( )<<endl; return output; } int main( ) { Cylinder cy1(3.5, 6.4, 5.2, 10); Circle *c= &cy1; Cylinder *cy= &cy1; cout<<"关于面积:"<<endl; cout<<"用指向圆的指针输出:"<<endl; cout<<c->area()<<endl; cout<<"用指向圆柱的指针输出:"<<endl; cout<<cy->area()<<endl<<endl; return 0; }
时间: 2024-09-30 07:02:03