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

“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");
    }

     

    评论
    解决 1 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题