Create a class Student with following data members- name, roll no and marks as private data
member. Create array of objects for three students, compare their marks using operator
overloading, and display the records of the student who is getting highest score.
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int rollNo;
int mark;
public:
Student(){}
Student(string name,int rollNo,int mark){
this->name=name;
this->rollNo=rollNo;
this->mark=mark;
}
void display() {
cout<<"\nStudent name: "<<name<<"\n";
cout<<"Student roll no: "<<rollNo<<"\n";
cout<<"Student mark: "<<mark<<"\n\n";
}
//compare marks using operator overloading
bool operator>(const Student &s1)
{
return (this->mark>s1.mark);
}
};
int main(){
//array of objects for three students
Student students[3];
students[0]=Student("Peter",12332,90);
students[1]=Student("Mary",546454,75);
students[2]=Student("Mike",874565,93);
Student studentHighestScore=students[0];
cout<<"All students: \n";
for(int i=0;i<3;i++){
students[i].display();
if(students[i]>studentHighestScore){
studentHighestScore=students[i];
}
}
cout<<"\nThe student who is getting the highest score.\n";
studentHighestScore.display();
return 0;
}
Comments
Leave a comment