Answer to Question #188129 in C++ for DKkumar

Question #188129

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.


1
Expert's answer
2021-05-02T08:35:46-0400


#include<iostream>
#include<bits/stdc++.h>
#include<string>
using namespace std;

class Marks
{


	public:
	int rollno;
	int marks;
	string name;
	
	void input()
	{
		cout << "Enter name: "<<endl;
	    cin>>name;
		cout<<"Enter Roll No: "<<endl;
	    cin>>rollno;
	        
	}




};


class Physics: public Marks{
	public:
		int phy;
		
		Physics()
		{
			cout<<"Enter physics marks : "<<endl;
			cin>>phy;
		}
};


class Chemistry: public Marks{
	public:
		int chem;
		
		Chemistry()
		{
			cout<<"Enter Chemistry marks : "<<endl;
			cin>>chem;
		}
};


class Mathematics: public Marks{
	public:
		int maths;
		
		Mathematics()
		{
			cout<<"Enter Mathematics marks : "<<endl;
			cin>>maths;
		}
};




int main()
{
	int n;
	cout<<"Enter number of students in a class : "<<endl;
	cin>>n;
	
	Marks s[n];
	int sum =0;
	int sum1 =0;
	int sum2 =0;
	int sum3 =0;
	cout<<endl;
	for(int i=0; i<n; i++)
	{
		cout<<"Enter details of student"<<i+1<<endl;
		cout<<"----------------------------"<<endl;
		s[i].input();
		Physics p;
		Chemistry c;
		Mathematics m;
		
		s[i].marks = p.phy + c.chem + m.maths;
		sum += s[i].marks;
		sum1 += p.phy;
		sum2 += c.chem;
		sum3 += m.maths;
		cout<<endl;
		
	}
	
	int avgp = sum1/n;
	int avgc = sum2/n;
	int avgm = sum3/n;
	cout<<endl<<"Physics Average marks of the class is : "<<avgp;
	cout<<endl<<"Chemistry Average marks of the class is : "<<avgc;
	cout<<endl<<"Mathematics Average marks of the class is : "<<avgm<<endl<<endl;
}





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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS