回 帖 发 新 帖 刷新版面

主题:求助:一道简单的c++程序,老是调试出错,望高手指点,谢谢!

我写了一段简单的代码,主要是学习纯虚函数的使用,结果怎是编译通不过,望高手指点
感激不尽!
#include<iostream.h>
class shape  //基类
{
    protected :
        float x,y;
    public  :
        shape()
        {}
        shape(float X,float Y)
        {
            x=X;
            y=Y;
        }
        virtual float area()=0;//计算面积
        virtual float perim()=0;//计算周长
};
class rectangle:public shape//矩形
{
    public:
    rectangle(float xx,float yy):shape(xx,yy)
    {}
    float area()
    {
        return x*y;
    }
    float perim()
    {
        return 2*(x+y);
    }
};
class circle:public shape//圆
{
    public :
    circle(float xx,float yy):shape(xx,yy)
    {}
    float aera()
    {
        return 3.14*x*x;
    }
    float perim()
    {
        return 6.28*x;
    }
};

int main()
{
    shape *point;
    rectangle A(3.5,2.0);
    point = &A;
    cout<<"the area of the rectangle is "<<point->area()<<endl;
    cout<<"the perim of the rectangle  is "<<point->perim()<<endl;
    [color=FF0000]circle B(2.0,0.0);[/color]
    point = &B;
    cout<<"the area of the circle is "<<point->area()<<endl;
    cout<<"the perim of the circle is "<<point->perim()<<endl;
    return 0;
}

编译结果总是:
[color=800080]e:\my files\procedur\新建文件夹\ok.cpp(53) : error C2259: 'circle' : cannot instantiate abstract class due to following members:
        e:\my files\procedur\新建文件夹\ok.cpp(31) : see declaration of 'circle'[/color]
如果把     [color=FF0000]circle B(2.0,0.0);[/color]
    注释掉就没有问题,不知道是为什么! 搞半天了都郁闷了,谢谢高手指点!
    
    

回复列表 (共2个回复)

沙发

circle类的:
  float aera()
    {
        return 3.14*x*x;
    }
中的aera拼写错误。改为area。拼写错误的话,就是没有重载对应的纯虚函数,自然就出错了。

板凳

[quote]circle类的:
  float aera()
    {
        return 3.14*x*x;
    }
中的aera拼写错误。改为area。拼写错误的话,就是没有重载对应的纯虚函数,自然就出错了。[/quote]
谢谢哟,这么明显的错误,我没有看出来,真是丢人 ,呵呵
谢谢!

我来回复

您尚未登录,请登录后再回复。点此登录或注册