Answer to Question #223409 in C++ for Dunga

Question #223409
Write a C++ program to calculate the total marks of each student of a class. This class takes the following subjects Physics, Chemistry and Mathematics. Your program shall be able to calculate the total marks for each student,

Create a class named Marks with data members “student_number”, “name” and “mark”. 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. The student number of each student will be generated automatically starting with a prefix STDN followed by 5 integer numbers. The values of the 5 numbers shall be created in increasing order from 00000 to 99999. If a user enters a number larger the 99999 for the number of students in this class, a warning shall be given and program exits. The number of students in this class shall be entered by the user. When done, your program shall commit and print to a .txt file the name of the subject, the highest ranked student.
1
Expert's answer
2021-08-05T03:01:52-0400
#include <iostream>
#include <string>
#include <iomanip>
#include <stdlib.h>    
#include <time.h>      
#include <fstream>
using namespace std;




class Marks{
private:
	int student_number;
	string name;
	int mark;
	int totalMark;
public:
	Marks(string name,int mark){
		this->student_number = rand() %10000 + 88889;
		this->name=name;
		this->mark=mark;
	}
	~Marks(){}
	int getMark(){
		return this->mark;
	}
	int getTotalMark(){
		return this->totalMark;
	}
	void setTotalMark(int totalMark){
		this->totalMark=totalMark;
	}
	string getName(){
		return this->name;
	}
	virtual void display(){
		cout<<"Student number: "<<student_number<<endl;
		cout<<"Student name: "<<name<<endl;
		cout<<"Student total mark: "<<this->totalMark<<endl;
	}
};
class Physics : public Marks{
public:
	Physics(string name,int mark) : Marks(name,mark){}
	void display(){
		Marks::display();
		cout<<"Student mark of Physics: "<<getMark()<<endl;
	}
};




class Chemistry : public Marks{
public:
	Chemistry(string name,int mark) : Marks(name,mark){}
	void display(){
		Marks::display();
		cout<<"Student mark of Chemistry: "<<getMark()<<endl;
	}
};




class Mathematics : public Marks {
public:
	Mathematics(string name,int mark) : Marks(name,mark){}
	void display(){
		Marks::display();
		cout<<"Student mark of Mathematics: "<<getMark()<<endl;
	}
};


int main(){
	srand (time(NULL));
	int numberStudentsClass;
	cout<<"Enter the number of students in the class: ";
	cin>>numberStudentsClass;


	//If a user enters a number larger the 99999 for the number of students in this class, a warning shall be given and program exits. 
	if(numberStudentsClass>99999){
		cout<<"You have entered a number larger the 99999 for the number of students in this class";
		return 0;
	}else{
		numberStudentsClass*=3;
		Marks** marks=new Marks*[numberStudentsClass];
		string name;
		int mark;


		for(int i=0;i<numberStudentsClass;i+=3){
			int totalMark=0;
			cin.ignore();
			cout<<"Enter information about student "<<(i+1)<<"\n";
			cout<<"Enter the student's name: ";
			getline(cin,name);
			cout<<"Enter the student's mark of Physics: ";
			cin>>mark;
			totalMark+=mark;
			marks[i]=new Physics(name,mark);
			cout<<"Enter the student's mark of Chemistry: ";
			cin>>mark;
			totalMark+=mark;
			marks[i+1]=new Chemistry(name,mark);
			cout<<"Enter the student's mark of Mathematics: ";
			cin>>mark;
			totalMark+=mark;
			marks[i+2]=new Mathematics(name,mark);
			marks[i]->setTotalMark(totalMark);
			marks[i+1]->setTotalMark(totalMark);
			marks[i+2]->setTotalMark(totalMark);
			cout<<"\n";
		}
		cout<<"\n";
		int max=marks[0]->getTotalMark();
		int maxIndex=0;
		for(int i = 0; i < numberStudentsClass; i++){
			if(max<marks[i]->getTotalMark()){
				max=marks[i]->getTotalMark();
				maxIndex=i;
			}
		}
		cout<<"The highest ranked student is "<<marks[maxIndex]->getName();
		ofstream highestRankedStudentFile;
		highestRankedStudentFile.open ("highestRankedStudent.txt");
		highestRankedStudentFile<<"The highest ranked student is "<<marks[maxIndex]->getName();
		highestRankedStudentFile.close();
		for(int i = 0; i < numberStudentsClass; i++){
			delete marks[i];
		}
		delete[] marks;
	}


	
	int i;
	cin>>i;
	return 0;
}

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