We were given Student class and Student Roll class (header files included below) The only things I cant figure out so far are how to overload the = operator for StudentRoll, and the copy constructor for studentRoll. Heres what I have, any suggestions are appreciated.
//Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
class Student {
public:
Student(const char * const name, int perm);
int getPerm() const;
const char * const getName() const;
void setPerm(const int perm);
void setName(const char * const name);
Student(const Student &orig);
~Student();
Student & operator=(const Student &right);
std::string toString() const;
private:
int perm;
char *name; // allocated on heap
};
#endif
//studentRoll.h
#ifndef STUDENTROLL_H
#define STUDENTROLL_H
#include <string>
#include "student.h"
class StudentRoll {
public:
StudentRoll();
void insertAtTail(const Student &s);
std::string toString() const;
StudentRoll(const StudentRoll &orig);
~StudentRoll();
StudentRoll & operator=(const StudentRoll &right);
private:
struct Node {
Student *s;
Node *next;
};
Node *head;
Node *tail;
};
#endif
//studentRoll.cpp
#include <string>
#include "studentRoll.h"
#include <sstream>
#include <iostream>
StudentRoll::StudentRoll() {
head = tail = NULL;
}
void StudentRoll::insertAtTail(const Student &s) {
/* tail -> next = new Node;
tail = tail -> next;
tail -> s = s;
tail -> next = null;
*/
Node* n = new Node;
n -> s = new Student(s);
n -> next = NULL;
if (head == NULL){
head = n;
}
if(tail != NULL){
tail -> next = n;
}
n -> next = NULL;
tail = n;
}
std::string StudentRoll::toString() const {
std::ostringstream oss;
oss << "[";
Node *p = head;
while (p){
oss << p -> s -> toString();
if(p != tail) oss << ",";
p = p -> next;
}
oss << "]";
delete p;
return oss.str();
}
StudentRoll::StudentRoll(const StudentRoll &orig) {
Node *p1 = 0;
Node *p2 = 0;
if (orig.head == NULL){
head = NULL;
tail = NULL;
}
else{
head = new Node;
head -> s = new Student(*(orig.head -> s));
std::cout << "got here";
p1 = head;
p2 = orig.head -> next;
}
while(p2){
p1 -> next = new Node;
p1 = p1 -> next;
p1 -> s = new Student(*(orig.head -> s));
p2 = p2 -> next;
}
p1 -> next = NULL;
tail = p1;
delete p1;
delete p2;
}
StudentRoll::~StudentRoll() {
Node *current = head;
while ( current != 0 ){
Node* next = current->next;
delete current;
current = next;
}
}
StudentRoll & StudentRoll::operator =(const StudentRoll &right ) {
// The next two lines are standard, and you should keep them.
// They avoid problems with self-assignment where you might free up
// memory before you copy from it. (e.g. x = x)
if (&right == this)
return (*this);
// TODO... Here is where there is code missing that you need to
// fill in...
Node *p1 = 0;
Node *p2 = 0;
if (right.head == NULL){
head = NULL;
tail = NULL;
}
else{
head = new Node;
head -> s = new Student(*(right.head -> s));
p1 = head;
p2 = right.head -> next;
}
while(p2){
p1 -> next = new Node;
p1 = p1 -> next;
p1 -> s = new Student(*(p2 -> s));
p2 = p2 -> next;
}
p1 -> next = NULL;
tail = p1;
delete p1;
delete p2;
// KEEP THE CODE BELOW THIS LINE
// Overloaded = should end with this line, despite what the textbook says.
return (*this);
}