In: Computer Science
C++ polymorphism.
Create a program that issues a quiz through the terminal. The quiz will ask a variety of C++ questions to the user and prompt them for a response. The response will differ based on the type of question, such as true or false, multiple-choice, or fill in the blank.
Part 1: Designing the Quiz
Design this program using a polymorphic vector of questions. Create a Question base class that will serve to point to three different derived classes: TrueFalseQuestion, MultipleChoiceQuestion, and FillInTheBlank. Your base class should only contain members that are common aspects of all three of the derived classes. The vector that contains the list of questions should be composed of Question class pointers. Test your quiz class by manually initialize your questions with text and correct answers and then issue each question one at a time to the user.
Part 2: Making a Quiz Class
Once you have finished designing your question class and derived forms, create a quiz class which will contain the question polymorphic array. The quiz class should have at least 3 (probably 5) public member functions: One for issuing the quiz to the user, one for adding new questions to the quiz, another for retrieving the quiz score in terms of a percentage. Properly implement the "rule of three" for the quiz class so that this quiz class does not create memory leaks or shallow copies.
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Question
{
public:
Question(string text);
~Question();
virtual void print()
{
cout << "\n " <<
m_text;
}
virtual bool promptAnswer() = 0;
private:
string m_text;
};
Question::Question(string text):m_text(text)
{
}
Question::~Question()
{
}
class TrueFalseQuestion : public Question
{
public:
TrueFalseQuestion(string question,bool
answer):Question(question)
,m_answer(answer)
{
}
void print() override
{
Question::print();
}
bool promptAnswer() override
{
bool answer;
cout << "\nEnter your answer
";
cin >> answer;
return
checkAnswerCorrect(answer);
}
private:
bool m_answer;
bool checkAnswerCorrect(bool answer)
{
if (answer != m_answer)
return
false;
return true;
}
};
class MultiChoiceQuestion : public Question
{
public:
MultiChoiceQuestion(string question ,string
options[4],char choice = '\n'):Question(question),
m_choice(choice)
{
for (size_t i = 0; i < 4;
i++)
{
m_options[i] =
options[i];
}
}
void print() override
{
Question::print();
cout << "\n Options :";
for (size_t i = 0; i < 4;
i++)
{
cout <<
"\n" << m_options[i];
}
}
bool promptAnswer() override
{
char answer;
cout << "\nEnter your answer
";
cin >> answer;
return
checkAnswerCorrect(answer);
}
private:
string m_options[4];
char m_choice;
bool checkAnswerCorrect(char choice)
{
if (choice != m_choice)
return
false;
return true;
}
};
class FillinBlanksQuestion : public Question
{
public:
FillinBlanksQuestion(string question,string
answer):Question(question),
m_answer(answer)
{
}
void print() override
{
Question::print();
}
bool promptAnswer() override
{
string answer;
cout << "\nEnter your answer
";
cin >> answer;
return
checkAnswerCorrect(answer);
}
private:
string m_answer;
bool checkAnswerCorrect(string answer)
{
if (answer != m_answer)
return
false;
return true;
}
};
class Quiz
{
public:
void addQuestion(Question* q)
{
if (nullptr != q)
vQuestions.push_back(q);
}
void issueQuestions()
{
for (Question* q :
vQuestions)
{
q->print();
//Check if the
answer is correct if it is correct then add score;
if
(q->promptAnswer())
m_quizScore += 1;
}
}
float getQuizScore()
{
return
(m_quizScore/vQuestions.size()) * 100;
}
private:
vector<Question*> vQuestions;
float m_quizScore;
};
int main()
{
Quiz q;
Question* q1 = new TrueFalseQuestion("Is 1 + 2 is 3?
Enter 1 for true and 0 for false", true);
string options[4] = {"a . New york", "b . Texas", "c .
Paris", "d . Hawaii"};
Question* q2 = new MultiChoiceQuestion("What is the
Capital of America ?", options,'a');
Question* q3 = new FillinBlanksQuestion("What is 2 *
3", "6");
q.addQuestion(q1);
q.addQuestion(q2);
q.addQuestion(q3);
q.issueQuestions();
cout << "\nQuiz Score is : "<< q.getQuizScore();
}
OUTPUT: