-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple-choice-test.cpp
More file actions
138 lines (122 loc) · 2.95 KB
/
multiple-choice-test.cpp
File metadata and controls
138 lines (122 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* @file multiple-choice-test.cpp
* @author nirmeet baweja
* @brief Multiple choice test has several multiple choice questions.
* Each question can have only one correct answer.
* Additionally, timed multiple choice test can specify the time
* allowed for solving each question in the test.
*
* The code below satisfies this specification,
* but the customer complained that the memory usage of the program
* constantly increases. Fix this problem.
*
* @version 0.1
* @date 2022-02-11
*
* @copyright Copyright (c) 2022
*
*/
#include <iostream>
#include <string>
class MultipleChoiceTest
{
public:
/**
* @brief Construct a new Multiple Choice Test object
*
* @param questionsCount number of questions in the test
*/
MultipleChoiceTest(int questionsCount) // constructor
{
this->questionsCount = questionsCount;
answers = new int[questionsCount];
for (int i = 0; i < questionsCount; i++)
{
answers[i] = -1;
}
}
/**
* @brief Destroy the Multiple Choice Test object
* adding the virtual keyword stops memory leaks,
* by invoking the correct destructor
*/
virtual ~MultipleChoiceTest() // destructor
{
// Deallocate the memory that was previously reserved for this string.
delete[] answers;
}
void setAnswer(int questionIndex, int answer)
{
answers[questionIndex] = answer;
}
int getAnswer(int questionIndex) const
{
return answers[questionIndex];
}
protected:
int questionsCount;
private:
int *answers;
};
class TimedMultipleChoiceTest : public MultipleChoiceTest
{
public:
/**
* @brief Construct a new Timed Multiple Choice Test object
*
* @param questionsCount number of questions in the test
*/
TimedMultipleChoiceTest(int questionsCount)
: MultipleChoiceTest(questionsCount) // constructor
{
times = new int[questionsCount];
for (int i = 0; i < questionsCount; i++)
{
times[i] = 0;
}
}
/**
* @brief Destroy the Timed Multiple Choice Test object
* At code execution-time, the correct destructor is looked up in an object
* known as a vtable.
* Hence the destructor associated with derived class will be called prior
* to a further call to the destructor associated with base class.
*/
virtual ~TimedMultipleChoiceTest() //destructor
{
// Deallocate the memory that was previously reserved for this pointer.
delete[] times;
}
void setTime(int questionIndex, int time)
{
times[questionIndex] = time;
}
int getTime(int questionIndex) const
{
return times[questionIndex];
}
private:
int *times;
};
#ifndef RunTests
void executeTest()
{
MultipleChoiceTest test(5);
for (int i = 0; i < 5; i++)
{
test.setAnswer(i, i);
}
for (int i = 0; i < 5; i++)
{
std::cout << "Question " << i + 1 << ", correct answer: " << test.getAnswer(i) << "\n";
}
}
int main()
{
for (int i = 0; i < 3; i++)
{
std::cout << "Test: " << i + 1 << "\n";
executeTest();
}
}
#endif