Answer to Question #188735 in C++ for Shubham kumar jha

Question #188735

2. 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-05-05T02:23:28-0400
#include <iostream>
#include <string>
using namespace std;

class Student {
private:
    string name;
    int roll;
    int marks;

public:
    Student(string name, int roll, int marks);
    string getName() const {
        return name;
    }
    int getRoll() const {
        return roll;
    }
    int getMarks() const {
        return marks;
    }
    void display() const;
};

Student::Student(string name, int roll, int marks) :
    name(name), roll(roll), marks(marks)
{}

void Student::display() const
{
    cout << "Name: " << name << endl;
    cout << "Roll #: " << roll << endl;
    cout << "Makes " << marks << endl;
}

int main() {
    Student* A = new Student("Alice", 123, 98);
    Student* B = new Student("Bobe", 124, 87);
    Student* C = new Student("Charly", 125, 89);

    Student* best = A->getMarks() > B->getMarks() ? A : B;
    best = best->getMarks() > C->getMarks() ? best : C;
    cout << "The student with the highest score:" << endl;
    best->display();

    delete A;
    delete B;
    delete C;
}

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
APPROVED BY CLIENTS