代码什么的要哭了 2022-04-26 10:11 采纳率: 67.9%
浏览 723
已结题

【c++】错误提示:[Error] cannot declare variable 'one' to be of abstract type 'triangle'

错误提示:[Error] cannot declare variable 'one' to be of abstract type 'triangle'

img


各位可以帮忙解决一下报错吗,实在是看不出来应该怎么改,感谢!!
源程序如下:


/*设shape类为表示形状的抽象类,

里面有一个成员函数area( )用于求图形面积的函数,

为纯虚函数total( )则是一个通用的函数,

用于求不同形状的图形面积总和

shape类派生三角形类(triangle),矩形类(rectangle), 

并给出具体的求面积函数,

请将程序补充完整*/

//StudybarCommentBegin
//#include<iostream.h>
#include<math.h>
#include<iostream> 
using namespace std;
class shape
{
//StudybarCommentEnd
protected: 
    double x,y;
public:
    shape(){};
    shape(double a, double b)
    {
        x = a;
        y = b;
    }
    double area(){};
    virtual double total() = 0;
};
//StudybarCommentBegin
class rectangle:public shape   //矩形类 
{
private:
    int width,length;
public:
    rectangle(int c=10,int d=5)//:shape(a,b)
       {width=c;length=d;}
    double area()
       {return width*length;}
};

class triangle:public shape  //三角形类 
{
private:
    double n, height;
public:
    triangle(double nn, double h)
    {
        n = nn;
        height = h;
    }
    double area(){return (1/2)*n*height;}
    
};
double total(shape *s[],int n) 
{ 
    double sum=0.0; 
    for(int i=0;i<n;i++)
        sum=sum+s[i]->area();
    return sum; 
}


int main()
{ 
    shape *sp[2] ;
    int x,y;
    cin>>x>>y;
    triangle one(x,y);
    rectangle two;
    sp[0]=&one;
    sp[1]=&two;
    cout<<"the total area is "<<total(sp,2)<<endl;
}
//StudybarCommentEnd
  • 写回答

2条回答 默认 最新

  • bostonAlen 2022-04-26 10:50
    关注

    子类必须实现父类声明的纯虚函数
    virtual double total() = 0;

    #include<math.h>
    #include<iostream> 
    using namespace std;
    class shape
    {
    //StudybarCommentEnd
    protected: 
        double x,y;
    public:
        shape(){};
        shape(double a, double b)
        {
            x = a;
            y = b;
        }
        double area(){};
        virtual double total() = 0;
    };
    //StudybarCommentBegin
    class rectangle:public shape   //矩形类 
    {
    private:
        int width,length;
    public:
        rectangle(int c=10,int d=5)//:shape(a,b)
           {width=c;length=d;}
        double area()
           {return width*length;}
        double total(){}
    };
     
    class triangle:public shape  //三角形类 
    {
    private:
        double n, height;
    public:
        triangle(double nn, double h)
        {
            n = nn;
            height = h;
        }
        double area(){return (1/2)*n*height;}
        double total(){}
        
    };
    double total(shape *s[],int n) 
    { 
        double sum=0.0; 
        for(int i=0;i<n;i++)
            sum=sum+s[i]->area();
        return sum; 
    }
     
     
    int main()
    { 
        shape *sp[2] ;
        int x,y;
        cin>>x>>y;
        triangle one(x,y);
        rectangle two;
        sp[0]=&one;
        sp[1]=&two;
        cout<<"the total area is "<<total(sp,2)<<endl;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月4日
  • 已采纳回答 4月26日
  • 创建了问题 4月26日

悬赏问题

  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗