We want to calculate the total marks of each student of a class in Physics,Chemistry and
Mathematics and the average marks of the class. The number of students in the class are entered
by the user. Create a class named Marks with data members for roll number, name and marks.
Create three other classes inheriting the Marks class, namely Physics, Chemistry and
Mathematics, which are used to define marks in individual subject of each student. Roll number
of each student will be generated automatically.
#include<iostream>
using namespace std;
class Marks {
public:
int roll_number = 0;
string name;
int marks;
};
class Physics: Marks {
public:
int input(){
cout<<"Enter marks for Physics: \n";
cin>>marks;
return marks;
}
};
class Chemistry: Marks{
public:
int input(){
cout<<"Enter marks for Chemistry: \n";
cin>>marks;
return marks;
}
};
class Mathematics: Marks{
public:
int input(){
cout<<"Enter marks for Mathematics: \n";
cin>>marks;
return marks;
}
};
int main(){
Physics p;
Chemistry c;
Mathematics m;
cout<<"Enter the number of students:\n";
int n;
cin>>n;
int total [n];
for(int i=0; i<n; i++){
int ph = p.input();
int ch = c.input();
int mat = m.input();
int sum = ph + ch + mat;
int av = sum /3;
total[i] = av;
}
for(int i=0; i<n; i++){
cout<<total[i]<<" ";
}
}
Comments
Leave a comment