Jamesyoung36 2017-05-31 07:43 采纳率: 21.1%
浏览 790

用基类数组指向派生类,产生了错误

 #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条回答 默认 最新

  • shen_wei 2017-05-31 08:06
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog