. 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.
#include <iostream>
using namespace std;
class Student{
int roll, marks;
string name;
public:
Student(int r, int m, string s):roll(r), marks(m), name(s){}
void display(){
cout<<"Name: "<<name<<endl;
cout<<"Roll: "<<roll<<endl;
cout<<"Marks: "<<marks<<endl;
}
};
int main(){
Student *students[3];
int r, m, max = INT_MIN, pos = 0;
string s;
for(int i = 0; i < 3; i++){
cout<<"Enter name of student "<<i + 1<<": ";
cin>>s;
cout<<"Enter roll of student "<<i + 1<<": ";
cin>>r;
cout<<"Enter marks of student "<<i + 1<<": ";
cin>>m;
students[i] = new Student(r, m, s);
if(m > max){
max = m;
pos = i;
}
cout<<endl;
}
cout<<"Student with the highest score is\n";
students[pos]->display();
return 0;
}
Comments
Leave a comment