CodeBlanks Logo Icon CodeBlanks

Quiz model

Design the skeleton of Question and Quiz classes.

Quiz model

In this five-lesson project you will build a Quiz app. Each lesson adds one piece, and by the final lesson you will have a working application.

First we define a Question struct. A question has a text and an answer: struct Question { std::string text; std::string answer; };. Then we define an empty Quiz class. In the following lessons we will add methods for adding questions, running the quiz and calculating the score. Each lesson's starting code will contain the previous lesson's solution.

Struct and class

This project uses both struct and class. Question is a struct that holds simple data; Quiz is a class with behavior.

The first lesson prepares only the skeleton: you define the Question struct and an empty Quiz class. In main you print std::cout << "Quiz ready"; to confirm the program runs. In the next lessons we will add methods on top of this skeleton, and the output will grow each lesson.

Practice Define the Question and Quiz skeleton

Define a Question struct with std::string text; and std::string answer; members. Define an empty Quiz class. In main print std::cout << "Quiz ready";.

Starter code

main.cpp
#include <iostream>
#include <string>

// Define the Question struct and Quiz class here

int main() {
    // Print the Quiz ready message
    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <string>

struct Question {
    std::string text;
    std::string answer;
};

class Quiz {
};

int main() {
    std::cout << "Quiz ready";
    return 0;
}

Expected output

Quiz ready

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which fields does the Question struct contain?

main.cpp
struct Question { ??? };
  • title and author
  • name and age
  • id and email
  • text and answer Correct answer

Explanation: The Question struct contains text and answer fields.

2 Complete the Question struct

Question
struct Question { std::string ____; std::string answer; };
Answer
struct Question { std::string text; std::string answer; };

Explanation: The question text is stored in the text field.

3 Match each piece to its role

Term Meaning
struct Question Data structure
class Quiz Behavior class
std::string Text type
std::cout Writing output

All lessons in Intermediate