Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program should create Student objects and overload << to display Student ob

ID: 3920153 • Letter: T

Question

This program should create Student objects and overload << to display Student object data values. Find and fix the errors.

#include<iostream>

#include<string>

using namespace std;

class Student

{

ostream& operator<(ostream &, const Student);

private:

int stuID;

int year;

double gpa;

public:

Student(const int=9999, const int=4, const double=1.1);

};

Student::Student(int i, int y, double g)

{

stuID = i;

year = y;

gpa = g;

}

ostream &operator<<(ostream &out, const Student *otherStu)

{

out << otherStu.stuID << ", "

<< otherStu.year << ", "

<< otherStu.gpa << endl;

}

int main()

{

Student a(111, 2, 3.50), b(222, 1, 3.00);

cout << "Student a: " << a;

cout << "Student b: " << b;

return 0;

}

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;
class Student {
    friend ostream& operator<<(ostream&, const Student);

private:
    int stuID;
    int year;
    double gpa;

public:
    Student(const int = 9999, const int = 4, const double = 1.1);
};
Student::Student(int i, int y, double g)
{
    stuID = i;
    year = y;
    gpa = g;
}
ostream& operator<<(ostream& out, const Student otherStu)
{
    out << otherStu.stuID << ", "
        << otherStu.year << ", "
        << otherStu.gpa << endl;
}
int main()
{
    Student a(111, 2, 3.50), b(222, 1, 3.00);
    cout << "Student a: " << a;
    cout << "Student b: " << b;
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote