#include <iostream>
using namespace std;
class shape
{public:
virtual float area() const {return 0.0;}
virtual float volume() const {return 0.0;}
virtual void shapename() const =0;
};
class point:public shape
{public:
point(float,float);
void setpoint(float,float);
float getX()const {return x;}
float getY()const {return y;}
virtual void shapename() const{cout<<"point:";}
friend ostream & operator << (ostream &,const point &);
protected:
float x,y;
};
point::point(float a,float b)
{x=a;y=b;}
void point::setpoint(float a,float b)
{x=a;y=b;}
ostream&operator<<(ostream &output,const point &p)
{
output<<"["<<p.x<<","<<p.y<<"]";
return output;
}
class circle:public point
{public:
circle(float,float,float);
void setRadius(float);
float getradius()const;
virtual float area() const;
virtual void shapename()const {cout<<"circle:";}
friend ostream&operator<<(ostream &,const circle &);
protected:
float radius;
};
circle::circle(float a,float b,float r):point(a,b),radius(r){}
void circle::setRadius(float r){radius=r;}
float circle::getRadius()const{return radius;}
float circle::area()const {return 3.14*radius*radius;}
ostream&operator<<(ostream &output,const circle &c)
{output<<"["<<c.x<<","<<c.y<<"],r="<<c.radius;
return output;
}
class cylinder:public circle
{public :
cylinder(float x,float y,float r,float h);
void setheight(float);
virtual float area()const;
virtual float volume()const;;
virtual void shapename()const{cout<<"cylinder:";}
friend ostream&operator<<(ostream &,const cylinder&);
protected:
float height;
};
cylinder::cylinder(float a,float b,float r,float h):circle(a,b,r),height(h){}
void cylinder::getheight(flaot h){height=h;}
float cylinder::area()const
{return 2*circle::area()+2*3.14*radius*height;}
float cylinder::volume()const
{return circle::area()*height;}
ostream&operator<<(ostream &output,const cylinder &cy)
{output<<"["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height;
return output;
}
int main()
{point point(3.2,4.3);
circle circle(2.4,1.2,5.6);
cylinder cylinder(3.2,4.4,5.4,4.6);
point.shapename();
cout<<point<<endl;
circle.shapename();
cout<<circle<<endl;
cylinder.shapename();
cout<<cylinder<<endl<<endl;
shape *pt;
pt=&point;
pt->shapename();
cout<<"x="<<point.getX()<<",y="<<point.getY()<<"\area="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
pt=&circle;
pt->shapename();
cout<<"x="<<circle.getX()<<",y="<<circle.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
pt=&cylinder;
pt->shapename();
cout<<"x="<<circle.getX()<<",y="<<circle.getY()<<"\narea="<<pt->area()
<<"\nvolume="<<pt->volume()<<"\n\n";
return 0;
}
找了半天不知道错在哪啊 我按书上打的啊
大家帮我看看