【问题标题】:About Virtual function, errors关于虚函数,错误
【发布时间】:2016-09-12 07:30:39
【问题描述】:

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

43 10 D:\Cpp\TurboC4\OOPCpp\shape.cpp [错误] 不能将字段 'Circle::r' 声明为抽象类型 'Shape' 6 7 D:\Cpp\TurboC4\OOPCpp\shape.cpp 因为以下虚函数在“Shape”中是纯函数: 18 18 D:\Cpp\TurboC4\OOPCpp\shape.cpp 虚拟浮点数 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();
}

【问题讨论】:

  • C++ 中不允许使用对象抽象类。
  • 您的问题是当您尝试添加Circle 的受保护成员时,如Shape r。这不可能,因为Shape 是抽象类。
  • Shape 是一个抽象类,您尝试在 Circle 类中创建抽象类对象。
  • 我希望r 是圆的半径,而不是Shape
  • 感谢您的帮助!明白了

标签: c++


【解决方案1】:

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

您需要将变量 r 设为引用或指针。


再阅读您的代码后,您可能根本不应该在这里使用成员变量,而是使用您从Shape 基类继承xy 成员.就像您在 Rectangle 课程中所做的那样。

【讨论】:

    【解决方案2】:

    问题出在这里:

    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++ 编译器可能会更好地为您服务...

    【讨论】:

    • 啊,我只是想看看“r”,因为它看起来很专业。 :) 我不知道,也许 :3 对我的情结感到抱歉。谢谢 !我会清除它!我的英语不好!
    猜你喜欢
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多