Repository navigation
Expand file tree
/
Copy pathday9_errorhandling.cpp
More file actions
96 lines (91 loc) · 2.31 KB
/
Copy pathday9_errorhandling.cpp
File metadata and controls
96 lines (91 loc) · 2.31 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
#include <iostream>
#include <vector>
#include <string>
#include <numeric>
using namespace std;
class Person{
protected:
string name;
int age;
public:
Person (string n, int a):name(n),age(a){}
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:
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;
}
};
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);
s2.print_report();
t1.add_student(&s1);
t1.add_student(&s2);
t1.print_class();
s1.print_report();
return 0;
}