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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.