#include #include #include #include #include class Question { public: Question(std::string const& title, std::string const& description) : title{title}, description{description} { } virtual ~Question() = default; virtual void print(std::ostream& os) const { os << title << "\n\n" << description << "\n" << std::endl; } virtual bool handle(std::ostream& os, std::istream& is) = 0; private: std::string title; std::string description; }; class Multi_Question : public Question { public: Multi_Question(std::string const& title, std::string const& description, std::vector const& alternatives, int answer) : Question{title, description}, alternatives{alternatives}, answer{answer} { } void print(std::ostream& os) const override { os << "Multi question: "; Question::print(os); } bool handle(std::ostream& os, std::istream& is) override { os << "Select all correct alternatives:\n"; for (unsigned i { 0 }; i < alternatives.size(); ++i) { os << (i + 1) << ". " << alternatives[i] << std::endl; } os << "\nEnter alternative: "; int user_answer{}; is >> user_answer; is.ignore(1000, '\n'); return user_answer == answer; } private: std::vector alternatives; int answer; }; class Textual_Question : public Question { public: Textual_Question(std::string const& title, std::string const& description, std::string const& answer) : Question{title, description}, answer{answer} { } void print(std::ostream& os) const override { os << "Textual question: "; Question::print(os); } bool handle(std::ostream& os, std::istream& is) override { os << "Enter your answer: "; std::string user_answer; std::getline(is, user_answer); return user_answer == answer; } private: std::string answer; }; int main() { std::vector questions { new Multi_Question { "Question 1", "Which of these datatypes represents a number?", { "int", "char", "string", "bool" }, 1}, new Textual_Question { "Question 2", "What is the course code for this course?", "TDP004" } }; for (Question* question : questions) { question->print(std::cout); bool correct { question->handle(std::cout, std::cin) }; std::cout << "Your answer was: "; if (correct) std::cout << "Correct!\n" << std::endl; else std::cout << "Wrong...\n" << std::endl; delete question; } }