Answer to Question #186774 in C++ for Ishan

Question #186774

. 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-30T11:32:51-0400
#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;
}

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