Reputation: 461
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.
Upvotes: 0
Views: 42
Reputation: 16
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.
Upvotes: 0
Reputation: 141638
Your actual operator+
looks correct. But there are bugs in your copy-constructor and assignment-operator that would cause it to malfunction:
size
, id
, or the names. [size]
items, not [itemsCheckedOut]
.size
.size
. It needs to detect this case and either reject the checkout, or allocate more space. (I mentioned this last time you posted a question about this project)Upvotes: 1
Reputation: 1
You should replace your string* array with a std::vector. It will handle the memory management for you, make your code far easier and less error prone than the manual memory management you are currently using. You can reserve an initial size of 10 if you are worried about it doing allocations when adding items (although with such small data sizes that shouldn't ever be a problem).
Upvotes: 0