-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExer17_11_quiz.h
48 lines (48 loc) · 1.22 KB
/
Exer17_11_quiz.h
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
#ifndef EXER17_11_QUIZ_H
#define EXER17_11_QUIZ_H
#include <cstddef>
#include <iostream>
#include <string>
#include <bitset>
#include <stdexcept>
template <std::size_t N> class Quiz;
// friend overloaded << operator
template <std::size_t N>
std::ostream& operator<<(std::ostream&os, const Quiz<N>&);
// friend to calculate grades
template <std::size_t N>
std::size_t grades(const Quiz<N>&, const Quiz<N>&);
template <std::size_t N>
class Quiz {
friend std::ostream& operator<< <N>(std::ostream&, const Quiz<N>&);
friend std::size_t grades<N>(const Quiz<N>&, const Quiz<N>&);
public:
Quiz() = default;
Quiz(const std::string &s) : q(s) {}
void updataQuiz(std::size_t n, bool a) {
if(n >= N)
throw std::out_of_range("out of range!");
q.set(n, a);
}
private:
std::bitset<N> q;
};
// friend overloaded << operator
template <std::size_t N>
std::ostream& operator<<(std::ostream& os, const Quiz<N> &quiz)
{
os << quiz.q;
}
// friend to calculate grades
template <std::size_t N>
std::size_t grades(const Quiz<N> &ans, const Quiz<N> &test)
{
std::size_t grd = 0;
for(std::size_t i = 0; i != N; ++i)
{
if(ans.q[i] == test.q[i])
++grd;
}
return grd;
}
#endif