Overloaded Operator Assignment

Hello all,

I'm in a computer science class currently, and am in need of help in writing an overloaded operator for the following code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  #include <iomanip>
#include "student.h"
#include "section.h"
 
//default constructor
Student::Student()
{
  id(-1);  //indicates an uninitialized student
}
 
//copy constructor
Student::Student(const Student &s)
{
  //copy the student information
  _id = s.id();
  _firstName = s.firstName();
  _lastName = s.lastName();
}
 
//a constructor which initializes a student
Student::Student(int studentId,
		 const std::string &studentFirstName,
		 const std::string &studentLastName)
{
  //set up our variables
  id(studentId);
  firstName(studentFirstName);
  lastName(studentLastName);
}
 
 
//methods for accessing student data
std::string
Student::firstName() const
{
  return _firstName;
}
 
void
Student::firstName(std::string studentFirstName)
{
  this->_firstName = studentFirstName;
}
 
std::string
Student::lastName() const
{
  return _lastName;
}
 
 
void
Student::lastName(std::string studentLastName)
{
  this->_lastName = studentLastName;
}
 
std::string
Student::fullName() const
{
  return _lastName + ", " + _firstName;
}
 
int
Student::id() const
{
  return _id;
}
 
 
void
Student::id(int studentId)
{
  this->_id = studentId;
}
 
//Student + Student = Section
Section 
Student::operator+(const Student &rhs) const
{
  Section result;
 
  //add these two students to the section
  result += *this;
  result += rhs;
 
  return result;
}
 
//Compare by lastName, then firstName, then id
bool
Student::operator<(const Student &rhs) const
{
  //these are the possibilities for true
  if(_lastName < rhs._lastName) {
    return true;
  } else if(_lastName == rhs._lastName and _firstName < rhs._firstName) {
    return true;
  } else if(_firstName == rhs._firstName and _id < rhs._id) {
    return true;
  }
 
  //otherwise it is false
  return false;
}
 
 
//Print out the student in the following format:
//  10 character wide field containing the id
//  *space*
//  24 character wide field containing fristName, lastName
std::ostream &
operator<<(std::ostream &os, const Student &rhs)
{
  os << std::left << std::setw(10) << rhs.id()
     << ' '
     << std::setw(24) << rhs.fullName() << std::right;
 
  return os;
}


The assignment is this:
"
Note that student constructors can be initialized with existing values! So with this in mind, create two sections. Each one will work a little bit differently.
1. For the first section, create three student pointer variables and initialize them. Create a new section and then add each of the three students to the section.
2. For the second section, create a new section and then add three students to it without using any student variables. You will not be allocating new students here. Instead, you'll be using the constructors to initialize the students and add them together in one go.

After you have created your two sections, print them both out. What I am looking for in this program is that you make correct use of your overloaded operators. If you do this correctly, you are actually not going to write very much code at all in the main function!
"

I'm very lost in the sauce and if anyone could help me out I'd greatly appreciate it.

Thanks in advance!
Create a new section and then add each of the three students to the section.

I see nothing having to do with sections.

I'm assuming you need to write a class to represent a section and then add students to a section. You need to think about how to represent a section. std::vector is an obvious choice, although other containers (list, map) would also work.

Last edited on
Topic archived. No new replies allowed.