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.
#include<iostream>
using namespace std;
class Student{
private:
string name;
int rollNo;
int marks[5];
public:
friend void input();
friend void display();
double totalMarks(){
double total = 0.0;
for(int i=0; i<5; i++){
total += marks[i];
}
return total;
}
bool operator <(Student s1){
if(totalMarks() < s1.totalMarks()){
return true;
}
else{
return false;
}
}
bool operator >( Student s1){
if(totalMarks() > s1.totalMarks()){
return true;
}
else{
return false;
}
}
bool operator <=(Student s1){
if(totalMarks() <= s1.totalMarks()){
return true;
}
else{
return false;
}
}
bool operator >=(Student s1){
if(totalMarks() >= s1.totalMarks()){
return true;
}
else{
return false;
}
}
};
void input(){
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];
}
}
void display(){
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]<<" ";
}
}
int main(){
input();
display();
}
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!
Learn more about our help with Assignments:
C++