Repository navigation
Expand file tree
/
Copy pathday8_inheritance.cpp
More file actions
159 lines (144 loc) · 4.04 KB
/
Copy pathday8_inheritance.cpp
File metadata and controls
159 lines (144 loc) · 4.04 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <vector>
#include <string>
#include <numeric>
#include <fstream>
#include <sstream>
#include <stdexcept>
using namespace std;
class Person{
protected:
string name;
int age;
public:
string get_name() const {return name;}
int get_age() const {return age;}
Person (string n, int a):name(n),age(a){}
virtual ~Person() {}
void introduce(){
cout<<"Hi i am "<<name<<" , "<<age<<" years old .\n";
}
};
class Student: public Person{
private:
string grade;
vector<string>subjects;
vector<double>marks;
public:
string get_grade() const {return grade;}
Student(string n,int a,string g): Person (n,a),grade(g){}
void study(){
cout<<name<<" is studying Grade"<<grade<<".\n";
}
void add_marks(string sub,int mark){
marks.push_back(mark);
subjects.push_back(sub);
}
double get_average(){
if (marks.empty()){
return 0.0;
}
double total=accumulate(marks.begin(),marks.end(),0.0);
return total/marks.size();
}
void print_report(){
introduce();
cout<<"Grade :"<<grade<<"\n-----marks-----\n";
for (size_t i=0;i<marks.size();++i){
cout<<" "<<subjects[i]<<" -> "<<marks[i]<<"\n";
}
cout<<"Average "<<get_average()<<"\n----------\n";
}
};
class Teacher: public Person{
private:
string subject;
vector<Student*>students;
public:
Teacher(string n,int a,string sub): Person(n,a){
subject=sub;
}
void teach(){
cout<<"hi i am "<<name<<" teaching "<<subject<<endl;
}
void add_student(Student* student){
students.push_back(student);
}
double class_average(){
if (students.empty()) return 0.0;
double total=0.0;
for (size_t i=0;i<students.size();++i){
total+=students[i]->get_average();
}
return total/students.size();
}
virtual void print_class(){
introduce();
cout<<"subject: "<<subject<<endl;
for (auto student:students){
student->introduce();
}
cout<<"average "<<class_average()<<endl;
}
};
void save_all_students(const vector<Student*>&student_list) {
ofstream file("student.txt");
if (!file){
throw runtime_error("database is missing!");
}
for (auto student:student_list){
file<<student->get_name()<<","<<student->get_age()<<","<<student->get_grade()<<endl;
}
cout<<"\n[system: database syncronised succesfully!]"<<endl;
file.close();
}
void load_students_from_files(){
ifstream file("student.txt");
if (!file){
throw runtime_error("database file missing");
}
string line;
cout<<"\n-----reconstructed csv roster (industrial csv loaded)-----"<<endl;
while (getline(file,line)){
if (line.empty()) continue;
stringstream ss(line);
string name,age_str,grade;
getline(ss,name,',');
getline(ss,age_str,',');
getline(ss,grade,',');
if (!age_str.empty()){
int age=stoi(age_str);
cout<<"loaded "<<"("<<age<<")"<<" grade "<<grade<<endl;
}
else{
cout<<"[skip the unseperable part: "<<line<<"]"<<endl;
}
}
file.close();
}
int main(){
Student s1("Amlan",19,"A");
Student s2("Riya",17,"A");
Teacher t1("Mr.Roy",45,"maths");
s1.add_marks("physics",92);
s1.add_marks("maths",88);
s2.add_marks("physics",90);
s2.add_marks("maths",86);
s1.print_report();
s2.print_report();
t1.add_student(&s1);
t1.add_student(&s2);
t1.print_class();
vector<Student*>roster={&s1, &s2};
try{
save_all_students(roster);
load_students_from_files();
}
catch (const runtime_error& e){
cout<<"caught exception: "<<e.what()<<endl;
cout<<"system action creating a fresh new 'student.txt' database file"<<endl;
ofstream new_file("student.txt");
new_file.close();
}
return 0;
}