m0_54926006 2021-06-04 19:46 采纳率: 100%
浏览 43
已采纳

“Square”"Circle": 不能实例化抽象类怎么修改

#include<iostream>
using namespace std;
#include<cstdlib>
class Shape
{
public:
    virtual float area()=0;
    virtual void setData(float,float=0)=0;
};
class Triangle:public Shape
{
    float w,h;
public:
    Triangle(float w=0,float h=0)
    {this->w=w;this->h=h;};
    virtual void setData(float ww,float hh)
    {w=ww;h=hh;};
    virtual float area()
    {return w*h/2;};
};
class Rectangle:public Shape
{
    float w,h;
public:
    Rectangle(float w=0,float h=0)
    {this->w=w;this->h=h;};
    virtual float area()
    {return w*h;};
    virtual void setData(float ww,float hh)
    {w=ww;h=hh;};
};
class Square:public Shape
{
    float s;
public:
    Square(float s=0,float a=0)
    {this->s=s;};
    virtual float area()
    {return s*s;};
    virtual void setData(float ss)
    {s=ss;};
};
class Circle:public Shape
{
    float r;
public:
    Circle(float r=0,float a=0)
    {this->r=r;};
    virtual float area()
    {return r*r*3.14;};
    virtual void setData(float ss)
    {r=ss;};
};
void main()
{
    Triangle T;
    Rectangle R;
    Square S;
    Circle C;
    Shape *p=new Triangle;
    p->setData(3.0,4.0);
    cout<<"area1="<<p->area()<<endl;
    p=new Rectangle;
    p->setData(3.0,4.0);
    cout<<"area2="<<p->area()<<endl;
    p=new Square;
    p->setData(3.0,4.0);
    cout<<"area3="<<p->area()<<endl;
    p=new Circle;
    p->setData(3.0,4.0);
    cout<<"area4="<<p->area()<<endl;
    system("pause");
}

  • 写回答

3条回答 默认 最新

  • CSDN专家-Time 2021-06-04 19:48
    关注

    抽象类是不能实例化的,只有继承Shape类的类,可以实例化父类中的抽象函数。

    我看了一下代码,你两个类里没有默认的构造函数

    #include <vector>
    #include <stdio.h>
    #pragma warning(disable:4996)
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include<stdio.h>
    #include<iostream>
    using namespace std;
    #include<cstdlib>
    class Shape
    {
    public:
        virtual float area() = 0;
        virtual void setData(float = 0, float = 0) = 0;
    };
    class Triangle :public Shape
    {
        float w, h;
    public:
        Triangle(float w = 0, float h = 0)
        {
            this->w = w; this->h = h;
        };
        virtual void setData(float ww, float hh)
        {
            w = ww; h = hh;
        };
        virtual float area()
        {
            return w * h / 2;
        };
    };
    class Rectangle :public Shape
    {
        float w, h;
    public:
        Rectangle(float w = 0, float h = 0)
        {
            this->w = w; this->h = h;
        };
        virtual float area()
        {
            return w * h;
        };
        virtual void setData(float ww, float hh)
        {
            w = ww; h = hh;
        };
    };
    class Square :public Shape
    {
        float s;
    public:
      
        Square(float s = 0, float a = 0)
        {
            this->s = s;
        };
        virtual float area()
        {
            return s * s;
        };
        virtual void setData(float ss,float s)
        {
            s = ss;
        };
    };
    class Circle :public Shape
    {
        float r;
    public:
        Circle(float r = 0, float a = 0)
        {
            this->r = r;
        };
        virtual float area()
        {
            return r * r * 3.14;
        };
        virtual void setData(float ss,float s)
        {
            r = ss;
        };
    };
    void main()
    {
        Triangle T;
        Rectangle R;
     /*   Square S;
        Circle C;*/
        Shape* p = new Triangle;
        p->setData(3.0, 4.0);
        cout << "area1=" << p->area() << endl;
        p = new Rectangle;
        p->setData(3.0, 4.0);
        cout << "area2=" << p->area() << endl;
        p = new Square(1,2);
        p->setData(3.0, 4.0);
        cout << "area3=" << p->area() << endl;
        p = new Circle;
        p->setData(3.0, 4.0);
        cout << "area4=" << p->area() << endl;
        system("pause");
    }

     

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮