关于虚函数,错误

时间:2016-09-12 07:30:39

标签: c++

我想创建一个形状类来在Oxy和区域外绘制矩形和圆形。 我必须使用虚函数,它会出错:

  

43 10 D:\ Cpp \ TurboC4 \ OOPCpp \ shape.cpp [错误]无法声明字段' Circle :: r'是抽象的'形状'   6 7 D:\ Cpp \ TurboC4 \ OOPCpp \ shape.cpp因为以下虚拟函数在' Shape'中是纯粹的:   18 18 D:\ Cpp \ TurboC4 \ OOPCpp \ shape.cpp virtual float Shape :: area()

这是我的代码:

class Shape{
    public:
            int x,y;
    public:
            void set_data (int a,int b){
                x=a;
                y=b;
            }
            void input(){
                cout<<"x= "; cin>>x;
                cout<<"y= "; cin>>y;
            }
            virtual float area()=0;

};

class Rectangle: public Shape{

    public:
            Rectangle(){
                x=0;
                y=0;
            }
            Rectangle(int x,int y){
                this->x=x;
                this->y=y;
            }
            void input(){
                cout<<"Enter value of width and height: ";
                cin>>x;
                cin>>y;
            }
            float area(){
                return x*y;
            }
};

class Circle: public Shape{
    protected:
            Shape r;
    public:
            Circle(){
                r.x=0;
                r.y=0;

            }
            //center of Circle: default(0,0) , r is 1 point in the circle.
            Circle(int x,int y){
                r.x=x;
                r.y=y;
            }   
            void nhap(){
                        cout<<"Enter values x,y of r: ";
                        cin>>x;
                        cin>>y;
            }
            virtual float area(){
                float a=sqrt(r.x*r.x+r.y*r.y);
                return 3.14*a*a;
            }
};

class ArrayOfShape{
    private:
            int n;
            Shape **a;
    public:
            void nhap(){
                int hinh;
                cout<<"input number of shape: ";
                cin>>n;
                a=new Shape*[n];
                for (int i=0;i<n;i++){
                    cout<<"\nEnter shape (1:rectangle,2:circle): ";
                    cin>>hinh;
                    if (hinh==1){
                        Rectangle *p=new Rectangle();
                        p->nhap();
                        a[i]=p;
                    }
                    else{
                        if(hinh==2){
                        Circle *e=new Circle();
                        e->input();
                        a[i]=e;
                        }
                        else{
                            cout<<"Invilid input";
                        }
                    }
                }
            }
            void area(){
                for (int i=0;i<n;i++)
                cout<<"\nArea of shape"<<i+1<<" : "<<a[i]->area();
            }
};

int main(){
    ArrayOfShape a;
    a.input();
    a.area();
}

2 个答案:

答案 0 :(得分:1)

r中声明成员变量Circle时,您将声明Shape类的实例。由于Shape是一个抽象类,这是不可能的。

您需要使变量r成为引用或指针。

稍后阅读您的代码后,您可能根本不应在此处使用成员变量,而是使用{<1>}和x成员继承来自{ {1}}基类。就像你在y课程中已经做过的那样。

答案 1 :(得分:1)

问题在于:

class Circle: public Shape{
    protected:
            Shape r; // Wrong

因为Shape是纯虚函数,所以你不能创建它的实例(这正是Shape r试图做的事情)。

可能的解决方案:

class Circle: public Shape{
protected:
    int x, y
    ...
    public Circle() {
      x = y = 0;  // Do you really need "r" at all?  Why not just x and y?
      ...

另外:

如果您真的使用Turbo C ++,请不要这样做。它已经过时了20年......而且很重要。特别是如果你正在学习。有很多免费和/或开源C ++编译器可能会为您提供更好的服务......

相关问题