Help with programming

Consider the class specification below. Write the prototype (i.e. header) of a member function to overload the insertion operator (i.e. <<). The << operator is to output the data members of an instance of class StudentTestScores into an output stream. Your definition should allow for chaining of output operations (e.g. cout << x << y; where x and y are of type StduentTestScires).

#include <string>

using namespace std;
class StudentTestScores{
private:
string studentName;
float *testScores; // used to point to an array of test scores
int numTestScores; // number of test scores
public:
// Class default constructor
// Class copy constructor
// Class destructor
//class accessors
//class mutators
//overloaded insertion operator
// other functions
};
Code snip:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class StudentTestScores
{
...
  public:
    friend std::ostream& operator<<(std::ostream& ostr, const StudentTestScores &s1);
...
};

std::ostream& operator << (std::ostream& ostr, const StudentTestScores &s1)
{
  ostr << std::endl;
  ostr << "Student Name: " << s1.studentName << std::endl;
  for(int i=0; i< s1.numTestScores; ++i)
    ostr << "Test " << i+1 << " score: " << s1.testScores[i] << std::endl;
  return ostr;
}

int main()
{
  StudentTestScores s1, s2;
  ...
  std::cout << s1 << s2;
  ...
}


Sample output:

Student Name: student_1
Test 1 score: 60
Test 2 score: 70
Test 3 score: 80
Test 4 score: 90

Student Name: student_2
Test 1 score: 65
Test 2 score: 75
Test 3 score: 85
Test 4 score: 95

Topic archived. No new replies allowed.