Question: Create a class for a multiple-choice question. Use the following diagram to identify the requirements. MCquestion -text: string +choices: string[4] -correctAnswer: int +setText(string): void +getText()

Create a class for a multiple-choice question. Use the following diagram to identify the requirements.

MCquestion

-text: string

+choices: string[4]

-correctAnswer: int

+setText(string): void

+getText() const: string

+setCorrectAnswer(int): void

+getCorrectAnswer() const: int

+isCorrect(int) const: bool

+display() const: void

+MCquestion()

+MCquestion(string)

As seen in the diagram, MC question has 2 private member variables: text, and correctAnswer; and a public member array, choices. Choices is an array that stores the possible answer for a questions answer. This array contains 4 strings. correctAnswer stores the position in the array which contains the correct answer.

Also, it has two mutators: setText, and setCorrectAnswer; and two accessors: getText, and getCorrectAnswer. In setCorrectAnswer function, do a validation. If the arguments value is not between 0-3 (inclusive), do not change the correct answer value.

In addition, this class has a function called isCorrect, which given an integer argument, it will compare it with the correctAnswer, and returns whether the values are the same (true or false). Before doing the comparison, decrease the integer argument by one (-1). This is because the choices are stored in a 0-based index array, and the choices will be displayed in a 1-based index list.

Also, the class has a display function which shows the question and the choices (with an index, 1-4).

Finally, it has 2 constructors: a no-argument constructor, and a one-argument constructor whose argument is the questions text. The one-argument construction will only initialize the text of the question.

Class files

The class must be separated in a .h file (header, or definition of variables and function prototypes), and a .cpp (source code).

The main program

Use that class in a main program that creates the following question.

In Pirates of the Caribbean, what was Captain Jack Sparrows ships name?

  1. The Marauder
  2. The Black Pearl
  3. The Black Python
  4. The Slytherin

Define the correct answer as The Black Pearl.

Then,

  • display the question to the user,
  • ask user what is the correct choice (integer value),
  • display whether the user choice was correct or not (to determine whether the user choice is correct, use the isCorrect function).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!