c++ - 不允许抽象类类型 "Rectangle"的对象

标签 c++ abstract-class virtual-functions

//QuizShape.h
#ifndef QUIZSHAPE_H
#define QUIZHAPE_H
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class QuizShape
{
protected:
    //outer and inner symbols, and label
    char border, inner;
    string quizLabel;
public:
    //base class constructor with defaults
    QuizShape(char out = '*', char in = '+', string name = "3x3 Square")
    {
        border = out;
        inner = in;
        quizLabel = name;
        cout << "base class constructor, values set" << endl << endl;
    };

    //getters
    char getBorder() const
    { return border; }
    char getInner() const
    { return inner; }
    string getQuizLabel() const
    { return quizLabel; }

    //virtual functions to be defined later
    virtual void draw( ) = 0;
    virtual int getArea( ) = 0;
    virtual int getPerimeter( ) = 0;

};


class Rectangle : public QuizShape
{
protected:
    //height and with of a rectangle to be drawn
    int height, width;
public:
    //derived class constructor
    Rectangle(char out, char in, string name,
                int h = 3, int w = 3):QuizShape(out, in, name)
    {
        height = h;
        width = w;
        cout << "derived class constructor, values set" << endl << endl;
    }

    //getters
    int getHeight() const
    { return height; }
    int getWidth() const
    { return width; }

    //*********************************************
    virtual void draw(const Rectangle &rect1)
    {
        cout << "draw func" << endl;
        cout << rect1.height << endl;
        cout << rect1.getWidth() << endl;
        cout << rect1.getQuizLabel() << endl;
    }

    virtual int getArea(const Rectangle &rect2)
    {
        cout << "area func" << endl;
        cout << rect2.getInner() << endl;
        cout << rect2.getBorder() << endl;
    }

    virtual int getPerimeter(const Rectangle &rect3)
    {
        cout << "perim func" << endl;
        cout << rect3.height << endl;
        cout << rect3.getWidth() << endl;
        cout << rect3.getQuizLabel() << endl;   
    }
    //************************************************
};



#endif

这些是目前为止的类类型。

//QuizShape.cpp
#include "QuizShape.h"

这目前除了桥接文件外什么都不做。

//pass7.cpp
#include "QuizShape.cpp"

int main()
{
    Rectangle r1('+', '-', "lol", 4, 5);

    cout << r1.getHeight() << endl;
    cout << r1.getWidth() << endl;
    cout << r1.getInner() << endl;
    cout << r1.getBorder() << endl;
    cout << r1.getQuizLabel() << endl;

    system("pause");
    return 0;
}

由于 Rectangle 应该是一个抽象类,因此代码无法编译,当鼠标悬停在 main 中的 r1 声明上时,我收到错误消息

"Object of abstract class type "Rectangle" is not allowed".

我检查了本网站和其他网站上的其他答案,但没有找到解决问题的方法。

注意:我理解虚函数的语句以 =0 结尾;使类成为抽象类。 QuizShape 应该是抽象的。我已经在 Rectangle 中定义了虚函数,但它仍然是一个抽象类。

如何修改 Rectangle 类的虚函数,使 Rectangle 不再抽象?

最佳答案

抽象类 QuizShape 中的方法是:

virtual void draw( ) = 0;
virtual int getArea( ) = 0;
virtual int getPerimeter( ) = 0;

但在 Rectangle 中,它们将 const Rectangle &rect1 作为参数,因此您可以隐藏方法而不是覆盖抽象方法。 Rectangle 中的方法需要与抽象基类中的方法具有相同的签名。

关于c++ - 不允许抽象类类型 "Rectangle"的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16073156/

相关文章:

c++ - MinGW 搞砸了 COLORREF 和 RGB

php - 如何实例化抽象类的子单例?

c# - C# 中的动态转换

c++ - 为什么私有(private)成员会被继承?

c++ - 实例化类的正确方法

c++ - 与位置无关的代码和 vtable

c++ - 在派生类中声明非虚拟析构函数是否安全

c++ - 声明具有所有虚函数的类的对象

c++ - 为什么这个组合程序要求输入两次值?

java - 抽象类中的main方法有什么用?