Question

In: Computer Science

C++ polymorphism. Create a program that issues a quiz through the terminal. The quiz will ask...

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.

Solutions

Expert Solution

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:


Related Solutions

C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class definitions, one base class and three derived classes. The derived classes will have an inheritance relationship (the “is a” relationship) with the base class. You will use base and derived classes. The base class will have at least one constructor, functions as necessary, and at least one data field. At least one function will be made virtual. Class members will be declared public and...
Create a C++ program that will ask the user for how many test scores will be...
Create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. (no fstream) The data needs to include the student’s first name, student number test score the fields should be displayed with a total width of 15. The prompt should be printed with a header in the file explaining what each is: ex. First Name student number Test Score 1) mike 6456464   98 2) phill...
Please create a c++ program that will ask a high school group that is made of...
Please create a c++ program that will ask a high school group that is made of 5 to 17 students to sell candies for a fund raiser. There are small boxes that sell for $7 and large ones that sell for $13. The cost for each box is $4 (small box) and $6 (large box). Please ask the instructor how many students ended up participating in the sales drive (must be between 5 and 17). The instructor must input each...
Please create a C++ program that will ask a high school group that is made of...
Please create a C++ program that will ask a high school group that is made of 5 to 17 students to sell candies for a fund raiser. There are small boxes that sell for $7 and large ones that sell for $13. The cost for each box is $4 (small box) and $6 (large box). Please ask the instructor how many students ended up participating in the sales drive (must be between 5 and 17). The instructor must input each...
Need a program in java that creates a random addition math quiz The program should ask...
Need a program in java that creates a random addition math quiz The program should ask the user to enter the following The smallest and largest positive numbers to be used when generating the questions - The total number of questions to be generated per quiz - The total number of the quiz's to create from then the program should generate a random math (Just addition) quiz from what the user entered
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
C++ Create a program that simulates a coin being flipped. Ask the user to guess between...
C++ Create a program that simulates a coin being flipped. Ask the user to guess between heads and tails. Let the user input 0 for heads and 1 for tails. Use a random generator to get random guesses every time. If the user guesses correctly, give them 1pt. Use a counter and initialize it to 0.   If the user does not guess correctly, subtract a point. Create a menu that allows the user to continue guessing, view the current score...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask user input the values of the array's element using for loop 3. pass the array to void function. in void function do the following: a. Find the maximum of the array. b. Compute the element average c. Find out how many numbers are above the average d. Find out and print how many numbers are below the average e. find out how many numbers...
Create a program that ask to input two users and the result will vary on their...
Create a program that ask to input two users and the result will vary on their name with similar digits. In a game of F.L.A.M.E.S , it will count and repeat depends on their name that has a similar digit. For an Example (JOE RIZAL) and (JACKLYN BRACKEN) - JOE RIZAL has - 5 similar digits , while JACKLYN BRACKEN has 6 similar digits so a total of 11. F - Friends - 1,7 L - Lover - 2,8 A...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT