请指教:试了N次,编译通不过。,书上的例子,帮忙看一下错在那?
#include<iostream.h>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=0,float=0);
void setpoint(float =0,float =0);
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;
float 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.getX()<<","<<p.getY()<<"]";
return output;
}
class Circle:public Point
{public:
Circle(float x=0,float y=0,float r=0 );
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.14159*radius*radius;}
ostream &operator<<(ostream &output,Circle &c)
{output<<"["<<c.getX()<<","<<c.getY()<<"],r="<<c.getradius();
return output;
}
class Cylinder:public Circle
{public:
Cylinder(float x=0,float y=0,float r=0,float h=0);
void setheight(float);
virtual float area()const;
virtual float volume() const;
virtual void shapename() const{cout<<"Cylinder:";}
friend ostream &operator<<(ostream &,Cylinder &);
protected:
float heigth;
};
Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r){height=h;}
void Cylinder::setheight(float h)
{height=h;}
float Cylinder::area() const
{return 2*Circle::area()+2*3.14159*radius*height;}
float Cylinder::volume()
{return Circle::area()*height;
}
ostream &operator<<(ostream &output,Cylinder &cy)
{output<<"["<<cy.getX()<<","<<cy.getY()<<"],r="<<cy.getradius()<<",h="<<cy.getheight();
return output;
}
int main()
{Point point(3.2,4.5);
Circle circle(2.4,1.2,5.6);
Cylinder(3.5,6.4,5.2,10.5);
point.shapename();
cout<<point<<endl;
circle.shapename();
cout<<circle<<endl;
cylinder.shapename();
cout<<cylinder<<endl;
Shape *p;
p->shapename();
cout<<"x="<<point.getX()<<"y="<<point.getY()<<"\narea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
p=&circle;
p->shapename();
cout<<"x="<<circle.getX()<<"y="<<circle.getY()<<"\nnarea="<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
p=&cylinder;
cout<<"x="<<cylinder.getX()<<"y="<<cylinder.getY()<<"\narea"<<p->area()<<"\nvolume="<<p->volume()<<"\n\n";
return 0;
}