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条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分