Question: Please solve in C++ Overloading useful operators 1. Implement the subscript ([]) operator as a member function of the List class. This takes an integer

Please solve in C++

Overloading useful operators

1. Implement the subscript ([]) operator as a member function of the List class. This takes an integer subscript as a parameter and returns the data pointer of the element at that position in the List object.

2. Implement the stream insertion (<<) operator of the List class. To make your code more consistent, implement the same operator in the Student class so that you can use it from the List operator. Both operators can replace the existing print functions.

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

#include

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 using namespace std; #include #include #include

#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"<

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 using namespace std; #include

#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: "<

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!