用基类数组指向派生类,产生了错误
 #include<iostream>
using namespace std;
//定义shape基类
class shape
{
public:
    virtual double  calculate() = 0;//定义虚函数calculate
};
//定义trapezoid类
class trapezoid
{
protected:
    double length1, length2, high;
public:
    trapezoid(double l1, double l2, double h)
    {
        length1 = l1; length2 = l2; high = h;
    }
    virtual double const calculate()
    {
        return (((length1 + length2)*high) / 2);
    }
};

//定义circle类
class circle :public shape
{
protected:
    double radius;
public:
    circle(double r)
    {
        radius = r;
    }
    virtual double calculate()
    {
        return(3.1415926*radius);
    }
};
//定义rectangule类
class rectangle
{
protected:
    double width, length;
public:
    rectangle(double w, double l)
    {
        width = w; length = l;
    }
    virtual double  calculate()
    {
        return(width*length);
    }
};

void main()
{
    rectangle r(10, 10);
    circle c(10);
    trapezoid t(10, 10, 10);
    shape* a[3];
    a[1] = &r; 
}

在main函数的最后一行出错,求大神指点

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问