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.
#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;
}
Comments
Leave a comment