Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ language Overloading operators 1. Implement the equality (==) and inequality

ID: 3727519 • Letter: C

Question

c++ language

Overloading operators

1. Implement the equality (==) and inequality (!=) relational operators as member functions of the List class. These test whether all the data pointers in two lists are the same and in the same order.

2. Implement the addition assignment (+=) operator as a member function of the List class that takes a data pointer as a parameter and adds it to the List object as a new element. Note: You must reuse existing code as much as possible. You must enable cascading where applicable!

3. Implement the addition (+) operator as a member function of the List class. This function takes a data pointer as a parameter and creates a new list comprised of all the elements of the List object as well as the new data element. You must reuse an existing overloaded operator here. Be careful what your member function returns! What does integer addition return?

----------------------------list.cc--------------------------------------------

#include <iostream>

using namespace std;

#include "List.h"

List::List() : head(0), tail(0) { }

List::~List()

{

cleanup();

}

// Adds a student to the front of the list

void List::addFront(Student* newStu)

{

Node* tmpNode = new Node;

tmpNode->data = newStu;

tmpNode->prev = 0;

tmpNode->next = 0;

if (head == 0) {

head = tmpNode;

tail = tmpNode;

return;

}

tmpNode->next = head;

head->prev = tmpNode;

head = tmpNode;

}

// Adds a student in alphabetical order

void List::addAlpha(Student* newStu)

{

Node* tmpNode = new Node;

tmpNode->data = newStu;

tmpNode->next = 0;

tmpNode->prev = 0;

if (head == 0) {

head = tmpNode;

tail = tmpNode;

return;

}

Node *currNode, *prevNode;

prevNode = 0;

currNode = head;

while (currNode != 0) {

if (currNode->data->getName() > tmpNode->data->getName())

break;

prevNode = currNode;

currNode = currNode->next;

}

if (currNode == head) { // add to first position

currNode->prev = tmpNode;

tmpNode->next = currNode;

head = tmpNode;

}

else if (currNode == 0) { // add to last position

prevNode->next = tmpNode;

tmpNode->prev = prevNode;

tail = tmpNode;

}

else { // add in the middle

tmpNode->next = currNode;

tmpNode->prev = prevNode;

prevNode->next = tmpNode;

currNode->prev = tmpNode;

}

}

void List::print() const

{

Node* currNode = head;

if (currNode == 0) return;

do {

currNode->data->print();

currNode = currNode->next;

} while (currNode != 0);

}

void List::printBack() const

{

Node* currNode = tail;

if (currNode == 0) return;

do {

currNode->data->print();

currNode = currNode->prev;

} while (currNode != 0);

}

void List::cleanupData()

{

Node *currNode = head;

while (currNode != 0) {

delete currNode->data;

currNode = currNode->next;

}

}

void List::cleanup()

{

Node *currNode, *nextNode;

currNode = head;

while (currNode != 0) {

nextNode = currNode->next;

delete currNode;

currNode = nextNode;

}

head = tail = 0;

}

----------------------------list.h--------------------------------------------

#ifndef LIST_H
#define LIST_H

#include "Student.h"

class List
{
class Node
{
friend class List;
private:
Student* data;
Node* prev;
Node* next;
};

public:
List();
~List();
void addFront(Student*);
void addAlpha(Student*);
void cleanupData();
void print() const;
void printBack() const;

private:
Node* head;
Node* tail;
void cleanup();
};

#endif

----------------------------main.cc--------------------------------------------

#include <iostream>
using namespace std;
#include <string>
#include <fstream>
#include <cstdlib>

#include "Student.h"
#include "List.h"


int main()
{
List list1, list2;
Student* stuPtr;
string name, number;

ifstream infile("stu.txt", ios::in);
if (!infile) {
cout<<"could not open file"<<endl;
exit(1);
}

while (infile >> number >> name) {
stuPtr = new Student(number, name);
list1.addFront(stuPtr);
list2.addAlpha(stuPtr);
}

cout << "ADD FRONT FORWARD" << endl;
list1.print();
cout << "ADD FRONT BACKWARD" << endl;
list1.printBack();
cout << "ADD ALPHA FORWARD" << endl;
list2.print();;
cout << "ADD ALPHA BACKWARD" << endl;
list2.printBack();;

list1.cleanupData();

}

----------------------------student.cc--------------------------------------------

#include <iostream>
using namespace std;
#include <string>

#include "Student.h"


Student::Student(string nu, string na)
: number(nu), name(na) { }

Student::~Student() { }

string Student::getName() const { return name; }

void Student::setName(string n) { name = n; }

void Student::print() const
{
cout<<"Student: "<<number<<" "<<name<<endl;
}

----------------------------student.h--------------------------------------------

#ifndef STUDENT_H
#define STUDENT_H


class Student
{
public:
Student(string="000000000", string="");
~Student();
string getName() const;
void setName(string);
void print() const;

private:
const string number;
string name;
};

#endif

-----------------stu.txt--------------------------------

100567899 Matilda
100234555 Joe
100456777 Timmy
100333222 Clayton
100999888 Harold

Explanation / Answer

---------------- Student.h

#ifndef STUDENT_H

#define STUDENT_H

class Student

{

public:

Student(string = "000000000", string = "");

~Student();

string getName() const;

void setName(string);

void print() const;

private:

const string number;

string name;

};

#endif

----------------- Student.cc

#include <iostream>

using namespace std;

#include <string>

#include "Student.h"

Student::Student(string nu, string na): number(nu), name(na)

{

}

Student::~Student() {}

string Student::getName() const { return name; }

void Student::setName(string n) { name = n; }

void Student::print() const

{

cout << "Student: " << number << " " << name << endl;

}

--------------List.h

#ifndef LIST_H

#define LIST_H

#include "Student.h"

class List

{

class Node

{

friend class List;

private:

Student *data;

Node *prev;

Node *next;

};

public:

List();

~List();

void addFront(Student *);

void addAlpha(Student *);

void cleanupData();

void print() const;

void printBack() const;

bool operator==(List &list2);

bool operator!=(List &list2);

void operator+=(List &list2);

List& operator+(List &list2);

private:

Node *head;

Node *tail;

void cleanup();

};

#endif

--------------- List.cc

#include <iostream>

using namespace std;

#include "List.h"

List::List() : head(0), tail(0)

{

}

List::~List()

{

cleanup();

}

// Adds a student to the front of the list

void List::addFront(Student *newStu)

{

Node *tmpNode = new Node;

tmpNode->data = newStu;

tmpNode->prev = 0;

tmpNode->next = 0;

if (head == 0)

{

head = tmpNode;

tail = tmpNode;

return;

}

tmpNode->next = head;

head->prev = tmpNode;

head = tmpNode;

}

// Adds a student in alphabetical order

void List::addAlpha(Student *newStu)

{

Node *tmpNode = new Node;

tmpNode->data = newStu;

tmpNode->next = 0;

tmpNode->prev = 0;

if (head == 0)

{

head = tmpNode;

tail = tmpNode;

return;

}

Node *currNode, *prevNode;

prevNode = 0;

currNode = head;

while (currNode != 0)

{

if (currNode->data->getName() > tmpNode->data->getName())

break;

prevNode = currNode;

currNode = currNode->next;

}

if (currNode == head)

{ // add to first position

currNode->prev = tmpNode;

tmpNode->next = currNode;

head = tmpNode;

}

else if (currNode == 0)

{ // add to last position

prevNode->next = tmpNode;

tmpNode->prev = prevNode;

tail = tmpNode;

}

else

{ // add in the middle

tmpNode->next = currNode;

tmpNode->prev = prevNode;

prevNode->next = tmpNode;

currNode->prev = tmpNode;

}

}

void List::print() const

{

Node *currNode = head;

if (currNode == 0)

return;

do

{

currNode->data->print();

currNode = currNode->next;

} while (currNode != 0);

}

void List::printBack() const

{

Node *currNode = tail;

if (currNode == 0)

return;

do

{

currNode->data->print();

currNode = currNode->prev;

} while (currNode != 0);

}

void List::cleanupData()

{

Node *currNode = head;

while (currNode != 0)

{

//delete currNode->data;

currNode = currNode->next;

}

}

void List::cleanup()

{

Node *currNode, *nextNode;

currNode = head;

while (currNode != 0)

{

nextNode = currNode->next;

delete currNode;

currNode = nextNode;

}

head = tail = 0;

}

bool List::operator==(List &list2)

{

Node *temp = head, *temp1 = list2.head;

while (temp != NULL && temp1 != NULL)

{

if (temp->data != temp1->data)

{

break;

}

temp = temp->next;

temp1 = temp1->next;

}

if (temp == NULL && temp1 == NULL)

{

return true;

}

else

return false;

}

bool List::operator!=(List &list2)

{

Node *temp = head, *temp1 = list2.head;

while (temp != NULL && temp1 != NULL && temp->data == temp1->data)

{

temp = temp->next;

temp1 = temp1->next;

}

if (temp == NULL && temp1 == NULL)

return false;

else

return true;

}

void List::operator+=(List &list2){

Node *temp = list2.head;

while(temp!=NULL){

Node *tmp = new Node;

tmp->data = temp->data;

tmp->next = NULL;

tmp->prev = tail;

tail->next = tmp;

tail = tmp;

temp = temp->next;

}

tail->next = NULL;

}

List& List::operator+(List &list2){

List *newNode = new List(*this);

*newNode += list2;

return *newNode;

}

--------------- Main.cc

#include <iostream>

#include <string>

#include <fstream>

#include <cstdlib>

using namespace std;

#include "Student.cc"

#include "List.cc"

int main()

{

List list1, list2;

Student *stuPtr;

string name, number;

ifstream infile("stu.txt", ios::in);

if (!infile)

{

cout << "could not open file" << endl;

exit(1);

}

while (infile >> number >> name)

{

stuPtr = new Student(number, name);

list1.addFront(stuPtr);

list2.addAlpha(stuPtr);

}

cout<<"IS LIST1==LIST1: "<<((list1 == list1)?"True":"False")<<endl;

cout<<"IS LIST1==LIST2: "<<((list1 == list2)?"True":"False")<<endl;

cout<<"IS LIST1!=LIST1: "<<((list1 != list1)?"True":"False")<<endl;

cout<<"IS LIST1!=LIST2: "<<((list1 != list2)?"True":"False")<<endl;

cout << "ADD FRONT FORWARD" << endl;

list1.print();

cout << "ADD FRONT BACKWARD" << endl;

list1.printBack();

cout << "ADD ALPHA FORWARD" << endl;

list2.print();

cout << "ADD ALPHA BACKWARD" << endl;

list2.printBack();

list1+=list2;

cout << "ADDING TWO LISTS INTO LIST1" << endl;

list1.print();

List mynew = list1+list2;

cout << "ADDING TWO LISTS INTO NEW LIST: " << endl;

mynew.print();

list1.cleanupData();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote