0

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); 

}
zspar
  • 23
  • 1
  • 7
  • 1
    While you should stick with the instructions for your homework. **Do not** do this in production code! - I mean: You have non-trivial duplication of code in your *Copy Constructor* and *Copy Assignment operator*. You should consider lifting the duplicate code to a single function, or the [*Copy and Swap*](http://stackoverflow.com/questions/1734628/copy-constructor-and-operator-overload-in-c-is-a-common-function-possible/1734640#1734640) idiom. For further indepths, see [What is the copy and swap idiom](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) – WhiZTiM May 20 '17 at 07:00
  • Here I was, ready to tag this as a copy and swap dupe, but that's a much more reasonable comment. – user4581301 May 20 '17 at 07:03
  • Thanks for the tip I wont do this in the future. I have to for this assignment though because the assignment has very specific tests it must pass. – zspar May 20 '17 at 07:05

0 Answers0