自学C++中的类时候出现的问题,请帮下忙了。。
#include<iostream> //题目是这样的,先建立一个抽象的基类就是这个shape,然后派生出point,circle,volum等派生类。最后在主函数里定义shape类的指针pt,使它#include<string> 可以指向基类和派生类的数据及函数,但是我这个主函数没写2句就错误了。。我定义的shape 指针pt无法指向shape的派生类。。
using namespace std; 特别请教错误啊。。
class shape
{
public:
virtual float area() {return 0;}
virtual float volume() {return 0;}
virtual void shapename() =0;
};
class point :public shape
{
public:
point(float a=0,float b=0):x(a),y(b){}
void setnumber()
{
cout<<"please input the x"<<endl;
cin>>x;
cout<<"please input the y"<<endl;
cin>>y;
}
virtual void shapename()
{
cout<<"this porject is a point"<<endl;
}
friend ostream &operator <<(ostream &,point &);
protected:
float x;
float y;
};
class circle :public point
{
public:
circle(float a=0,float b=0,float r=0):point(a,b),radius(r){}
void setradius()
{cout<<"please input the radius"<<endl;
cin>>radius;
}
virtual float area()
{
return(3.14*radius*radius);
}
virtual void shapename()
{
cout<<"this project is circle"<<endl;
}
friend ostream &operator <<(ostream &,circle &);
protected:
float radius;
};
ostream &operator <<(ostream &output,circle &c1)
{output<<"("<<c1.x<<","<<c1.y<<")"<<c1.radius<<c1.area()<<endl;
return output;
}
class cylinder :public circle
{
public:
cylinder(float a=0,float b=0,float r=0,float h=0):circle(a,b,r),height(h){}
void setheight()
{cout<<"please input the height"<<endl;
cin>>height;
}
virtual float volume()
{
return(3.14*radius*radius*height);
}
virtual void shapename()
{
cout<<"this project is cyliner"<<endl;
}
friend ostream &operator <<(ostream &,cylinder &);
protected:
float height;
};
ostream &operator <<(ostream &output,cylinder &c1)
{output<<"("<<c1.x<<","<<c1.y<<")"<<c1.radius<<c1.area()<<" "<<c1.height<<" "<<c1.volume()<<endl;
return output;
}
int main()
{
shape *pt;
pt=&point;//!这就是错误的地方,pt指针无法变为指向point类的。