1
//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 应该是一个抽象类,因此代码无法编译,当将鼠标悬停在 in 的声明上r1main,我收到错误

“不允许抽象类类型“矩形”的对象”。

我已经检查了该站点和其他站点上的其他答案,但没有遇到解决问题的方法。

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

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

4

2 回答 2

2

您在抽象类中的方法QuizShape是:

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

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

于 2013-04-18T01:45:48.653 回答
0

被覆盖的方法必须具有完全相同的签名,在派生类中您已为它们提供了参数。

于 2013-04-18T01:45:15.467 回答