Answer to Question #105848 in C++ for Aliasghar

Question #105848
• 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.
1
Expert's answer
2020-03-25T12:52:11-0400
#include <iostream>
#include <vector>
#include <numeric>


using namespace std;


class Marks
{
public:
    Marks(int roll_number,const string& name,const std::vector<int>& marks)
        :roll_number(roll_number),name(name),marks(marks)
    {
    }


    double average(){
        return static_cast<double>(totalMarks())/marks.size();
    }
    int totalMarks(){
        int sum=0;
        return std::accumulate(marks.begin(),marks.end(),sum);
    }
private:
     int roll_number;
     string name;
     std::vector<int> marks;
};


class Physics : public Marks
{
public:
    Physics(int roll_number,const string& name,const std::vector<int>& marks)
        :Marks(roll_number,name,marks)
    {}
};


class Chemistry : public Marks
{
public:
    Chemistry(int roll_number,const string& name,const std::vector<int>& marks)
        :Marks(roll_number,name,marks)
    {}
};


class Mathematics : public Marks
{
public:
    Mathematics(int roll_number,const string& name,const std::vector<int>& marks)
        :Marks(roll_number,name,marks)
    {}
};

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
16.04.20, 17:49

Dear visitor, please use panel for submitting new questions

Murad Siddique
15.04.20, 23:56

Can you plzzzz share the main function too ???

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS