Answer to Question #185063 in C++ for Harshit Shrivastava

Question #185063

Create a class Student with following data members, name, roll, and marks as private data member. Create array of objects for three students, compare their marks but make sure allocating memory to all the objects during run time only and display the records of the student who is getting highest score. 


1
Expert's answer
2021-04-24T15:13:47-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

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS