11. 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 is 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>
#include<string>
#include<vector>
using namespace std;
class Marks
{
int rollNum;
string name;
public:
double marks;
Marks(string _name,int _rollNum):name(_name), rollNum(_rollNum){}
void DisplayMarks()
{
cout << rollNum << " " << name << " " << marks << endl;
}
};
class Physics:public Marks
{
public:
Physics():Marks("Physics",1){}
};
class Chemistry :public Marks
{
public:
Chemistry() :Marks("Chemistry", 2) {}
};
class Mathematics :public Marks
{
public:
Mathematics() :Marks("Mathematics", 3) {}
};
struct Student
{
int roolNo;
Physics p;
Chemistry c;
Mathematics m;
Student(){}
};
int main()
{
int numStd;
cout << "Please, enter the number of students: ";
cin >> numStd;
vector<Student>vs;
for (int i = 0; i < numStd; i++)
{
Student temp;
temp.roolNo = i+1;
cout << "Please, enter the marks on Physics of student "<<i+1<<": ";
cin>>temp.p.marks;
cout << "Please, enter the marks on Chemistry of student " << i + 1 << ": ";
cin >> temp.c.marks;
cout << "Please, enter the marks on Mathematics of student " << i + 1 << ": ";
cin >> temp.m.marks;
vs.push_back(temp);
}
vector<Student>::iterator it;
cout << "\nTotal marks of Students:\n";
for (it = vs.begin(); it != vs.end(); it++)
{
cout <<"Stud rollno "<< it->roolNo <<endl;
it->p.DisplayMarks();
it->c.DisplayMarks();
it->m.DisplayMarks();
}
cout << "\nAverage marks of Students:\n";
double averMarks[3] = {0,0,0};
for (it = vs.begin(); it != vs.end(); it++)
{
averMarks[0] += it->p.marks;
averMarks[1] += it->c.marks;
averMarks[2] += it->m.marks;
}
averMarks[0] /= numStd;
cout << "\nPhysics average marks is " << averMarks[0];
averMarks[1] /= numStd;
cout << "\nChemistry average marks is " << averMarks[1];
averMarks[2] /= numStd;
cout << "\nMathematics average marks is " << averMarks[2];
}
Comments
Leave a comment