#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;
}
Comments
Leave a comment