1. 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 record of the student who is getting highest score.
#include <iostream>
using namespace std;
class Student
{
string _name;
int _roll;
int _mark;
public:
void setName(string name)
{
_name = name;
}
void setRoll(int roll)
{
_roll = roll;
}
void setMark(int mark)
{
_mark = mark;
}
string getName()
{
return _name;
}
int getRoll()
{
return _roll;
}
int getMark()
{
return _mark;
}
friend bool operator> (const Student &s1, const Student &s2);
friend bool operator< (const Student &s1, const Student &s2);
friend bool operator== (const Student &s1, const Student &s2);
};
bool operator> (const Student &s1, const Student &s2)
{
return s1._mark > s2._mark;
}
bool operator< (const Student &s1, const Student &s2)
{
return s1._mark < s2._mark;
}
bool operator== (const Student &s1, const Student &s2)
{
return s1._mark == s2._mark;
}
int main()
{
Student students[3];
int i;
for(i = 0; i < 3; i++)
{
string name;
int roll;
int mark;
cout << "Enter name of student #" << i+1 << ": ";
cin >> name;
students[i].setName(name);
cout << "Enter roll of student #" << i+1 << ": ";
cin >> roll;
students[i].setRoll(roll);
cout << "Enter mark of student #" << i+1 << ": ";
cin >> mark;
students[i].setMark(mark);
}
bool one, two, three;
one = students[0] > students[1] && students[0] > students[2];
two = students[1] > students[0] && students[1] > students[2];
three = students[2] > students[0] && students[2] > students[1];
if(one)
cout << "Name: " << students[0].getName() << endl << "Roll: " << students[0].getRoll() << endl << "Mark: " << students[0].getMark();
else if(two)
cout << "Name: " << students[1].getName() << endl << "Roll: " << students[1].getRoll() << endl << "Mark: " << students[1].getMark();
else if(three)
cout << "Name: " << students[2].getName() << endl << "Roll: " << students[2].getRoll() << endl << "Mark: " << students[2].getMark();
return 0;
}
Comments
Leave a comment