-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarks_inheritance.cpp
136 lines (120 loc) · 2.71 KB
/
Marks_inheritance.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
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
#include<iostream>
using namespace std;
class Physics;
class Chemistry;
class Mathematics;
class Marks{
protected:
int rollno;
string name;
int marks[3];
public:
Marks(){
}
Marks(int roll, string n){
rollno = roll;
name = n;
}
void display(){
cout<<"Roll Number: "<<rollno<<" \n ";
cout<<"Name: "<<name<<" \n ";
}
friend int getTotalMarks(Marks &, Physics&, Chemistry&, Mathematics&);
};
class Physics: public Marks{
private:
int mark;
public:
Physics(){
}
Physics(int mark){
this->mark = mark;
}
friend int getPhysicsMarks(Physics &);
};
class Chemistry: public Marks{
private:
int mark;
public:
Chemistry(){
}
Chemistry(int mark){
this->mark = mark;
}
friend int getChemistryMarks(Chemistry &);
};
class Mathematics: public Marks{
private:
int mark;
public:
Mathematics(){
}
Mathematics(int mark){
this->mark = mark;
}
friend int getMathematicsMarks(Mathematics &);
};
//The function to return Physics marks
int getPhysicsMarks(Physics &p){
return p.mark;
}
//The function to return Chemistry marks
int getChemistryMarks(Chemistry &c){
return c.mark;
}
//The function to return Mathematics mark
int getMathematicsMarks(Mathematics &m){
return m.mark;
}
int getTotalMarks(Marks&ma,Physics&p, Chemistry&c, Mathematics&m){
int first = getPhysicsMarks(p);
int second = getChemistryMarks(c);
int third = getMathematicsMarks(m);
ma.marks[0] = first;
ma.marks[1] = second;
ma.marks[2] = third;
int sum = 0.0;
for(int i=0; i<3; i++){
sum += ma.marks[i];
}
return sum;
}
int main(){
int n;
cout<<"Enter the total number of students in the class\n";
cin>>n;
double totalSum = 0.0;
int physics;
int chemistry;
int maths;
string name;
Marks marks[n];
double total [n];
for(int i=0; i<n; i++){
cout<<"Student "<<(i+1)<<endl;
cout<<"Enter the student name: "<<endl;
cin>>name;
Marks J(i+1,name);
marks[i] = J;
cout<<"Enter the student Physics marks: "<<endl;
cin>>physics;
cout<<"Enter the student Chemistry marks: "<<endl;
cin>>chemistry;
cout<<"Enter the student Mathematics marks: "<<endl;
cin>>maths;
Physics p1(physics);
Chemistry c1(chemistry);
Mathematics m1(maths);
total[i] = getTotalMarks(marks[i],p1,c1,m1);
}
for(int i=0; i<n; i++){
cout<<"Student: "<<(i+1)<<endl;
marks[i].display();
cout<<"Total marks: "<<total[i]<<endl;
}
double average = 0.0;
for(int i=0; i<n; i++){
totalSum += total[i];
}
cout<<"Total marks of the class is: "<<totalSum<<"\nThe average marks of the class is: "<<totalSum /n;
}