inheritquiz.cpp

来自「C++&datastructure书籍源码,以前外教提供现在与大家共享」· C++ 代码 · 共 49 行

CPP
49
字号
#include <iostream>
#include <string>
using namespace std;

#include "tvector.h"
#include "prompt.h"
#include "randgen.h"
#include "mathquestface.h"

// prototype quiz program for demonstrating inheritance

void GiveQuestion(MathQuestion& quest)
// post: quest is asked once, correct response is given
{
    string answer;
    cout << endl << quest.Description() << endl;
    cout << "type answer after the question" << endl;
    quest.Create();
    quest.Ask();
    cin >> answer;
    if (quest.IsCorrect(answer))
    {    cout << "Excellent!, well done" << endl;
    }
    else
    {    cout << "I'm sorry, the answer is " << quest.Answer() << endl;
    }
}

int main()
{
    tvector<MathQuestion *> questions;       // fill with questions
    
    questions.push_back(new MathQuestion());
    questions.push_back(new CarryMathQuestion());
    questions.push_back(new HardMathQuestion()); 
    
    int qCount = PromptRange("how many questions",1,10);
    RandGen gen;
    int k;
    for(k=0; k < qCount; k++)
    {    int index = gen.RandInt(0,questions.size()-1);
         GiveQuestion(*questions[index]);
    } 
    for(k=0; k < questions.size(); k++)
    {   delete questions[k];
    }
    return 0;   
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?