Answer to Question #229112 in C++ for Karthi

Question #229112
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>
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS