Create a class student with name, roll number, marks of 5 subjects
as data variables and use friend functions to read and display
student details.
Overload <, <= , > and > = operators to compare the total marks
scored by the student.
For example, if 40,60,50,50,100 are marks of students S1, and
100,100,100,100,100 are the marks of student S2 then S1 < S2, S1
<= S2 should return true and S1 > S2, S1 >= S2 should return false.
1
Expert's answer
2021-08-24T16:44:03-0400
#include<iostream>usingnamespacestd;
classStudent{
private:
string name;
int rollNo;
int marks[5];
public:
friend voidinput();
friend voiddisplay();
doubletotalMarks(){
double total = 0.0;
for(int i=0; i<5; i++){
total += marks[i];
}
return total;
}
booloperator <(Student s1){
if(totalMarks() < s1.totalMarks()){
returntrue;
}
else{
returnfalse;
}
}
booloperator >( Student s1){
if(totalMarks() > s1.totalMarks()){
returntrue;
}
else{
returnfalse;
}
}
booloperator <=(Student s1){
if(totalMarks() <= s1.totalMarks()){
returntrue;
}
else{
returnfalse;
}
}
booloperator >=(Student s1){
if(totalMarks() >= s1.totalMarks()){
returntrue;
}
else{
returnfalse;
}
}
};
voidinput(){
Student s;
cout<<"Enter student name\n";
cin>>s.name;
cout<<"Enter the Roll Number\n";
cin>>s.rollNo;
cout<<"Enter the marks for the student\n";
for(int i=0; i<5; i++){
cout<<"Mark "<<(i + 1)<<endl;
cin>>s.marks[i];
}
}
voiddisplay(){
Student s;
cout<<"Student name is\t"<<s.name<<endl;
cout<<"Student Roll Number is\t"<<s.rollNo<<endl;
cout<<"The marks for the student are: \n";
for(int i=0; i<5; i++){
cout<<s.marks[i]<<" ";
}
}
intmain(){
input();
display();
}
Comments