编程中的错误问题
#include<iostream.h>class Shape
{
public:
virtual float area() {return 0.0;}
virtual float volume() {return 0.0;}
virtual void ShapeName()=0;
};
class Point
{
public:
Point(float=0,float=0);
void set_point(float,float);
float getX() {return x;}
float getY() {return y;}
virtual void ShapeName() {cout<<"Point:";}
friend ostream&operator << (ostream &,Point &);
protected:
float x;
float y;
};
Point::Point(float a,float b)
{x=a;y=b;}
void Point::set_point(float a,float b)
{x=a;y=b;}
ostream&operator << (ostream &output,Point &p)
{cout<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;}
class Circle:public Point
{
public:
Circle(float=0,float=0,float=0);
void set_circle(float);
float getradius() {return radius;}
virtual float area();
virtual void ShapeName() {cout<<"Circle:";}
friend ostream&operator << (ostream &,Circle &);
protected:
float radius;
};
Circle::Circle(float a,float b,float c):Point(a,b)
{radius=c;}
void Circle::set_circle(float c)
{radius=c;}
float Circle::area()
{return (3.14159*radius*radius);}
ostream&operator << (ostream &output,Circle &c)
{cout<<"["<<c.x<<","<<c.y<<"], r="<<c.radius<<endl;
return output;}
class Cylinder:public Circle
{
public:
Cylinder(float=0,float=0,float=0,float=0);
void set_cylinder(float);
float getheight() {return height;}
virtual float area();
virtual float volume();
virtual void ShapeName() {cout<<"Cylinder:";}
friend ostream&operator << (ostream &,Cylinder &);
private:
float height;
};
Cylinder::Cylinder(float a,float b,float c,float h):Circle(a,b,c)
{height=h;}
void Cylinder::set_cylinder(float h)
{height=h;}
float Cylinder::area()
{return (2*Circle::area()+2*3.14159*radius*height);}
float Cylinder::volume()
{return (Circle::area()*height);}
ostream&operator << (ostream &output,Cylinder &cy)
{cout<<"["<<cy.x<<","<<cy.y<<"], r="<<cy.radius<<", h="<<cy.height<<endl;
return output;}
int main()
{
Point pt(3.2,4.5);
pt.ShapeName();
cout<<pt<<endl;
Circle c(2.4,1.2,5.6);
c.ShapeName();
cout<<c<<endl;
Cylinder cy(3.5,6.4,5.2,10.5);
cy.ShapeName();
cout<<cy<<endl;
Shape *pt1;
pt1=&pt;
pt1->ShapeName();
cout<<"x="<<pt.getX()<<",y="<<pt.getY()<<"\narea="<<pt1->area()<<"\nvolume="<<pt1->volume()<<endl;
pt1=&c;
pt1->ShapeName();
cout<<"x="<<c.getX()<<",y="<<c.getY()<<"\narea="<<pt1->area()<<"\nvolume="<<pt1->volume()<<endl;
pt1=&cy;
pt1->ShapeName();
cout<<"x="<<cy.getX()<<",y="<<cy.getY()<<"\narea="<<pt1->area()<<"\nvolume="<<pt1->volume()<<endl;
return 0;
}
这段程序编译时出现以下错误:
D:\工具安装包\vc++\MSDev98\MyProjects\例6_4\例6_4.cpp(123) : error C2440: '=' : cannot convert from 'class Point *' to 'class Shape *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
D:\工具安装包\vc++\MSDev98\MyProjects\例6_4\例6_4.cpp(126) : error C2440: '=' : cannot convert from 'class Circle *' to 'class Shape *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
D:\工具安装包\vc++\MSDev98\MyProjects\例6_4\例6_4.cpp(129) : error C2440: '=' : cannot convert from 'class Cylinder *' to 'class Shape *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
执行 cl.exe 时出错.
请问该如何解决!谢谢