Jacob Jacob - 1 month ago 13
C++ Question

Difficulty with using an overloaded addition operator

So here is my program. I have to make an object of type Student, then have the Student "check out" an item. And I am using an overloaded addition operator to make the user check out that item.

main.cpp:

#include <iostream>
#include "Student.h"

using namespace std;

int main() {

Student s(54000, "JOHN", "DOE");

cout << "main:" << endl << (s + "Frisbee") << endl << endl;

system("pause");

return 0;

}


I defined all my class defintions in the header file to try and keep this program minimal and simplified.

Student.h:

#ifndef STUDENT_H
#define STUDENT_H

#include <fstream>
#include <string>
#include <iostream>

using namespace std;

class Student {

public:

string firstName;
string lastName;
int id;
int itemsCheckedOut;
int size;
string *array;

Student(int id = 0, string firstName = "", string lastName = "") {
Student::firstName = firstName;
Student::lastName = lastName;
Student::id = id;
itemsCheckedOut = 0;
size = 10;
array = new string[size];
}

Student(const Student &other) {
itemsCheckedOut = other.itemsCheckedOut;
array = new string[itemsCheckedOut];
for (int i = 0; i < itemsCheckedOut; i++) {
array[i] = other.array[i];
}
}

~Student() {
delete[] array;
array = NULL;
}

Student &operator=(const Student &rhs) {
if (this != &rhs) {
firstName = rhs.firstName;
lastName = rhs.lastName;
id = rhs.id;
itemsCheckedOut = rhs.itemsCheckedOut;
delete[] array;
array = new string[size];
for (int i = 0; i < itemsCheckedOut; i++) {
array[i] = rhs.array[i];
}
}
return *this;
}

void CheckOut(const string &item) {
array[itemsCheckedOut] = item;
itemsCheckedOut++;
}

friend ostream &operator<<(ostream &output, const Student &student) {
output << student.id << " " << student.firstName << " " << student.lastName << endl;
if (student.itemsCheckedOut != 0) {
output << student.itemsCheckedOut;
for (int i = 0; i < student.itemsCheckedOut; i++) {
output << " " << student.array[i] << endl;
}
}
else {
output << 0;
}
return output;
}

const Student operator+(const string &item) {
Student s;
s = *this;
s.CheckOut(item);
cout << "class:" << endl << s << endl << endl;
return s;
}

};

#endif


output:

class:
54000 JOHN DOE
1 Frisbee

main:
-858993460
1 Frisbee


As you can see, from the main, its outputting the wrong thing. Instead of outputting the id followed by two spaces then the first name and last name, it outputs the number: -858993460. This has gotta be some sort of memory leak issue or something, but I'm pretty sure my copy constructor, overloaded assignment operator, and deconstructor are all defined correctly, but you can take a look at them.

I would appreciate any help at all as I am getting pretty desperate here. Thanks.

Answer

It calls copy constructor:

    Student(const Student &other) {
    itemsCheckedOut = other.itemsCheckedOut;
    array = new string[itemsCheckedOut];
    for (int i = 0; i < itemsCheckedOut; i++) {
        array[i] = other.array[i];
    }
}

but you forget to copy all Student's fields in its body. You override default copy constructor, so you should manually execute all data copying, as in assignment operator.