首页 > 解决方案 > c ++函数重新定义(代码不起作用 - 逻辑错误)

问题描述

我目前正在学习 c++,我现在正在研究继承。我必须制作一个常规问题类以及一个数字问题和多项选择问题的派生类。我在代码中提出问题,然后将它们一一显示给用户。然后用户回答问题,程序应该检查答案是否正确。

#include <iostream>
#include <string>
using namespace std;

class Question {
protected:
    string text;
    string answer;
    string input;
public:
    Question(string inText, string inAnswer) {
        text = inText;
        answer = inAnswer;
    }
    Question() {
        text = "blank question";
        answer = " ";
    }
    void setQuestion(string txt) {
        text = txt;
    }
    void setAnswer(string answr){
        answer = answr;
    }
    void userAnswer(string ans) {
        input = ans;
    }
    string getAnswer() {
        return answer;
    }
    string getQuestion() {
        return text;
    }

    void displayQuestion() {
        cout << getQuestion() << endl;
    }
    void isCorrect() {
        cout << "default function" << endl;
        if (input.compare(answer) == 0)
            cout << "True" << endl;
        else
            cout << "False" << endl;
    }
};
class NumericQuestion : public Question {
protected:
    double ans;
    double inp;
public:
    NumericQuestion(string inText, double inAns) {
        text = inText;
        ans = inAns;
    }
    void userAnswer(string ans) {
        inp = stod(ans);
    }

    void isCorrect() {
        cout << "numeric function" << endl;
        if (inp == ans)
            cout << "True" << endl;
        else if ((inp - ans) <= 0.01)
        cout << "False" << endl;
        else
            cout << "False" << endl;
}
};
class MultipleChoice : public Question {
protected:
    string qA, qB, qC, qD;
public:
    MultipleChoice(string inText, string qA, string aB, string qC, string qD,         char inAnswer) {
        text = inText;
        answer = inAnswer;
    }
    void displayQuestion() {
        cout << text << endl;
        cout << "a) " << qA << "    " << "b) " << qB << endl;
        cout << "c) " << qC << "    " << "d) " << qD << endl;
    }
};

int main() {
    string ans;

    Question q1("whats 2+2", "four");
    NumericQuestion q2("2+2", 4);

    MultipleChoice q3("The Right Answer is C", "answer A", "thisisB", "thats C", "Wrong", 'c');

    Question arr[] = { q1,q2,q3};
    for (int i = 0; i < 3; i++) {
        arr[i].displayQuestion();
        cin >> ans;
        arr[i].userAnswer(ans);
        arr[i].isCorrect();
    }

getchar();
return 0;
}

NumericQuestion 类的成员函数 isCorrect() 和 MultipleChoice 类的 displayQuestion() 没有被使用,而是使用 Question 类的成员函数,这会导致我的代码出现逻辑错误。

标签: c++functionclassinheritanceoverriding

解决方案


当您将 s 的子类按值分配给 when 的数组时,您正在对对象进行切片。这意味着即使您的某些派生对象具有基类没有的额外成员,它们也会被分配到数组中而被截断。另一个问题是,因为被声明为保存简单的对象,编译器将假定调用 like将始终引用,而不是派生方法。这里有几件事要解决。QuestionQuestionQuestion arr[] = { q1, q2, q3 };QuestionarrQuestionarr[i].isCorrect();Question::isCorrect()

使可覆盖函数virtual

class Question {
    ...
    virtual void isCorrect() {
        cout << "default function" << endl;
        if (input.compare(answer) == 0)
            cout << "True" << endl;
        else
            cout << "False" << endl;
    }

并在子类中覆盖它们:

class NumericQuestion : public Question {
    ...
    void isCorrect() override {
        cout << "numeric function" << endl;
        if (inp == ans)
            cout << "True" << endl;
        else if ((inp - ans) <= 0.01)
            cout << "False" << endl;
        else
            cout << "False" << endl;
    }

最后,通过将基类指针存储到您的Questions. 在这里,我使用std::shared_ptr来避免手动内存管理的麻烦。还出现了一个ranged-for 循环

auto q1 = std::make_shared<Question>("whats 2+2", "four");
auto q2 = std::make_shared<NumericQuestion> q2("2+2", 4);

auto q3 = std::make_shared<MultipleChoice>("The Right Answer is C", "answer A", "thisisB", "thats C", "Wrong", 'c');

// even though q1...q3 are shared_ptrs to derived classes, they can be safely cast to shared_ptrs to the base class
std::vector<std::shared_ptr<Question>> questions { q1, q2, q3 };

for (const auto& question: questions) {
    question->displayQuestion();
    cin >> ans;
    question->userAnswer(ans);
    question->isCorrect();
}

推荐阅读