Answer to Question #180181 in C++ for Manisha gupta

Question #180181

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. 


1
Expert's answer
2021-04-10T13:00:52-0400
#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;
}

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

Assignment Expert
12.04.21, 12:35

Dear RAVI MISHRA, please post a new question

RAVI MISHRA
12.04.21, 12:31

Explain with an example how one predefined data type can be converted to a user defined data type.

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS