/
13-3Student_info.cpp
85 lines (70 loc) · 1.48 KB
/
13-3Student_info.cpp
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
// source file for Student_info-related functions
#include "Student_info.h"
#include "core.h"
#include "grad.h"
#include <memory>
#include <string>
using std::istream; using std::vector;
using std::cout; using std::endl;
using std::string;
Student_info::Student_info(const Student_info& s): cp(0)
{
if (s.cp) cp = s.cp->clone();
}
Student_info& Student_info::operator=(const Student_info& s)
{
if (&s != this) {
delete cp;
if (s.cp)
cp = s.cp->clone();
else
cp = 0;
}
return *this;
}
istream& Student_info::read(istream& is)
{
delete cp;
char ch;
is >> ch;
if (ch == 'U') {
cp = new Core(is);
} else {
cp = new Grad(is);
}
return is;
}
// Modified for Ch. 13
string Core::name() const { return n; }
istream& Core::read(istream& in)
{
read_common(in);
read_hw(in, homework);
return in;
}
istream& Core::read_common(istream& in)
{
in >> n >> midterm >> final;
return in;
}
istream& Grad::read(istream& in)
{
Core::read_common(in);
in >> thesis;
read_hw(in, Core::homework);
return in;
}
istream& read_hw(istream& in, vector<double>& hw)
{
if (in) {
// get rid of previous contents
hw.clear();
// read homework grades
double x;
while (in >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
in.clear();
}
return in;
}