Overload the output stream operator << and the assignment operator =.
Any time cout << task; is used the code should output a task, any time task1 = task2 is copied there must be a deep copy if there are pointers. Use the operators in the following code as suggested above. DO NOT simply overload them!
Do so on the following code:
//Project 4 Main function
#include "functions.h"
#include <stdlib.h>
#include <stdio.h>
//main
int main(){
TaskList column("tasks.txt");
char ch;
while (ch != '4'){
menuDisplay();
ch = menuSelection();
menuSwitch(ch, column);
}
return 0;
}
*************************************************************************************
//This is the implementation file for tools.h
#include "functions.h"
#include <iomanip>
//MENU FUNCTIONS
void menuDisplay(){
cout << "\n1. Add Assigment \n"
<< "2. View Task Library \n"
<< "3. Search by Course \n"
<< "4. Quit \n\n"
<< "Your selection: \n" << endl;
}
char menuSelection(){
char ch;
cin >> ch;
cin.ignore(100, '\n');
return ch;
}
void menuSwitch(char ch, TaskList &column){
Task aTask;
switch (ch){
case '1':
add(aTask);
column.add(aTask);
break;
case '2':
cout << left
<< setw(15) << "\nCourse"
<< setw(15) << "Assignment"
<< setw(15) << "Due Date" << endl;
column.taskLibrary();
break;
case '3':
column.findTask();
break;
case '4':
column.outFile();
break;
default:
cout << "\nDigits 1-4 please
\n\n\n\n Try again" << endl;
}
}
////////ADD TASK FUNCTION: new tasks from user.
void add(Task &aTask)
{
char xCourse[MAX];
char xAssignment[MAX];
char xDueDate[MAX];
cout << "\n Course: ";
cin.get(xCourse, MAX, '\n');
cin.ignore(100, '\n');
cout << "\n Assignment: ";
cin.get(xAssignment, MAX, '\n');
cin.ignore(100, '\n');
cout << "\n Due Date: ";
cin.get(xDueDate, MAX, '\n');
cin.ignore(100, '\n');
aTask.setCourse(xCourse);
aTask.setAssignment(xAssignment);
aTask.setDueDate(xDueDate);
}
///////ALL CAPS: toupper, for none case sensitive
searching.
void allCaps(char xStr[])
{
for (int i = 0; i < strlen(xStr); i++)
{
xStr[i] = toupper(xStr[i]);
}
}*************************************************************************************
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include "TaskList.h"
//function prototypes
void menuDisplay();
char menuSelection();
void menuSwitch(char ch, TaskList &column);
void add(Task &aTask);
void allCaps(char xStr[]);
#endif
*************************************************************************************
//Task.cpp
#include "Task.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iomanip>
//CONSTRUCTOR
//DEFAULT CONSTRUCTOR
Task::Task(){
course = new char[MAX];
strcpy(course, "No Course");
assignment = new char[MAX];
strcpy(assignment, "No Assignment");
duedate = new char[MAX];
strcpy(duedate, "No Due Date");
}
//CONSTRUCTOR WITH PERAMETERS
Task::Task(char newCourse[], char newAssignment[], char
newDueDate[]){
course = new char[strlen(newCourse) + 1];
strcpy(course, newCourse);
assignment = new char [strlen(newAssignment)
+1];
strcpy(assignment, newAssignment);
duedate = new char[strlen(newDueDate) + 1];
strcpy(duedate, newDueDate);
}
//COPY CONSTRUCTOR
Task::Task(const Task &aTask){
if (course){
delete[] course;
course = NULL;
}
course = new char[strlen(aTask.course) + 1];
if (assignment){
delete[] assignment;
assignment = NULL;
}
assignment = new char[strlen(aTask.assignment) + 1];
if (duedate){
delete[] duedate;
duedate = NULL;
}
duedate = new char[strlen(aTask.duedate) + 1];
*this = aTask;
}
//DESTRUCTOR
Task::~Task(){
//deallocate mem for course and Due Date
if (course){
delete[] course;
course = NULL;
}
if (assignment){
delete[] assignment;
assignment = NULL;
}
if (duedate){
delete[] duedate;
duedate = NULL;
}
}
//A&M FUNCTIONS
//ACCESSORS
const void Task::getCourse(char returnCourse[]){
strcpy(returnCourse, course);
}
const void Task::getAssignment(char returnAssignment[]){
strcpy(returnAssignment, assignment);
}
const void Task::getDueDate(char returnDueDate[]){
strcpy(returnDueDate, duedate);
}
//MUTATORS
void Task::setCourse(char newCourse[]){
if (course){
delete[] course;
course = NULL;
}
course = new char[strlen(newCourse) + 1];
strcpy(course, newCourse);
}
void Task::setAssignment(char newAssignment[]){
if (assignment){
delete[] assignment;
assignment = NULL;
}
assignment = new char[strlen(newAssignment) + 1];
strcpy(assignment, newAssignment);
}
void Task::setDueDate(char newDueDate[]){
if (duedate){
delete[] duedate;
duedate = NULL;
}
duedate = new char[strlen(newDueDate) + 1];
strcpy(duedate, newDueDate);
}
//SAVE TO FILE
const void Task::printFile(ofstream &outFile){
outFile << course << ';'
<< assignment << ';'
<< duedate << endl;
}
//OVERLOADED
const Task& Task::operator= (const Task& aTask){
// CHECK FOR DUPLICATE
if (this == &aTask)
return *this;
else{
strcpy(this->course,
aTask.course);
strcpy(this->assignment, aTask.assignment);
strcpy(this->duedate,
aTask.duedate);
return *this;
}
}
ostream& operator<< (ostream& out, const Task&
aTask){
out << left
<< setw(15) << aTask.course
<< setw(15) << aTask.assignment
<< setw(15) << aTask.duedate << endl;
return out;
}
*************************************************************************************
//This is a header file for the Task class
#ifndef TASK_H
#define TASK_H
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
const int MAX = 101;
//Class data type for the Task object
class Task
{
friend ostream& operator<< (ostream&
out, const Task &);
private:
char *course;
char *assignment;
char *duedate;
public:
//constructors
Task();
Task(char initCourse[], char initAssignment[], char
initDueDate[]);
Task(const Task &aTask);
//destructor
~Task();
//accessor and mutator functions
const void getCourse(char returnCourse[]);
const void getAssignment(char
returnAssignment[]);
const void getDueDate(char returnDueDate[]);
void setCourse(char newCourse[]);
void setAssignment(char newAssignment[]);
void setDueDate(char newDueDate[]);
//print function
const void printFile(ofstream &outFile);
const Task& operator= (const Task&
aTask);
};
#endif
*************************************************************************************
//This is the implementation file for TaskList class
#include "TaskList.h"
#include "functions.h"
#include <iomanip>
//TaskList implementation functions
//default constructor
TaskList::TaskList(){
head = NULL;
tail = NULL;
}
//INFILE CONSTRUCTOR
TaskList::TaskList(char file[]){
ifstream in;
Task aTask;
char xCourse[MAX], xDueDate[MAX], xAssignment[MAX];
head = NULL;
tail = NULL;
in.open(file);
if (!in){
cout << "\nERROR: FILE NOT IN
SCOPE \n\n\n\n\n" << endl;
exit(0);
}
in.get(xCourse, MAX, ';');
while (!in.eof()){
in.ignore(5, ';');
in.get(xAssignment, MAX,
';');
in.ignore(5, ';');
in.get(xDueDate, MAX, '\n');
in.ignore(5, '\n');
//populate aTask with all the
information
aTask.setCourse(xCourse);
aTask.setAssignment(xAssignment);
aTask.setDueDate(xDueDate);
this->add(aTask);
in.get(xCourse, MAX, ';');
}
in.close();
}
//destructor
TaskList::~TaskList(){
Node *i = head;
while (head){
head = i->next;
delete i;
i = head;
}
head = NULL;
i = NULL;
}
//adds a Task to the list
void TaskList::add(Task &aTask){
Node *newNode, *i, *prev;
char xCourse1[MAX], xCourse2[MAX];
newNode = new Node;
newNode->data = aTask;
newNode->next = NULL;
if (!head){
head = newNode;
tail = newNode;
}
else{
i = head;
prev = NULL;
i->data.getCourse(xCourse1);
newNode->data.getCourse(xCourse2);
while (i &&
strcmp(xCourse1, xCourse2) < 0){//push prev and i right
prev = i;
i =
i->next;
if (i)
i->data.getCourse(xCourse1);
}
if (!i){ //add to tail
tail->next =
newNode;
tail =
newNode;
}
else{ //insert between 2
nodes
if (prev){
prev->next = newNode;
newNode->next = i;
}
else{
newNode->next = head;
head = newNode;
}
}
}
}
//prints the list
const void TaskList::taskLibrary(){
Node *i;
for (i = head; i; i = i->next){
cout << i->data;
}
cout << endl;
}
//finds a movie in the list
const void TaskList::findTask()
{
Node *i;
char searchTask[MAX], xCourse[MAX];
cout << "\nSearch Task by Course: \n";
cin.get(searchTask, MAX, '\n');
allCaps(searchTask);
for (i = head; i; i = i->next){
i->data.getCourse(xCourse);
allCaps(xCourse);
if (strstr(xCourse,
searchTask)){
cout <<
i->data;
}
}
cout << endl;
}
// WRITE TO FILE
void TaskList::outFile(){
ofstream outFile;
Node *i;
outFile.open("tasks.txt");
if (!outFile){ // TEST CASE FOR NO FILE
cout << "ERROR FILE NOT IN
SCOPE. " << endl;
exit(0);
}
for (i = head; i; i = i->next){
i->data.printFile(outFile);
}
outFile.close();
}
*************************************************************************************
#ifndef TASKLIST_H
#define TASKLIST_H
#include "Task.h"
class TaskList
{
private:
struct Node
{
Task data;
Node *next;
};
Node *head, *tail;
public:
//CONSTRUCTOR
TaskList();
TaskList(char file[]);
//DESTRUCTOR
~TaskList();
//FUNCTIONS
void add(Task &aTask);
const void taskLibrary();
const void findTask();
void outFile();
};
#endif
*************************************************************************************
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
Overload the output stream operator << and the assignment operator =. Any time cout << task;...
Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...
For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...
Hi there, I am working on a binary search tree code in c++. The program must store and update students' academic records, each node includes the student name, credits attempted, credits earned and GPA. I have made some progress with the code and written most of the functions in the .cpp file (already did the .h file) but i am struggling with what to put in the main and how to put an update part in the insert function. I...
#include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...
can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...
This program uses C++. This program reads in a line from the user and prints it out in a certain format. An example would be Input: 1 2 3 4 5 would result Output: [{1}, {2}, {3}, {4}, {5}]. When quotations marks are added into the input the format becomes different. For instance, Input 1 2 "3 4 5" would result in [{1}, {2}, {3 4 5}]. When I ad multiple quotation marks into the input, it will only use...
What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...
Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...
Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...
This is c++ programming and here is my code. I am getting an exception thrown on the destructor for permanentworker. Can you tell me why I am getting this exception? #include <iostream> #pragma warning(disable:4996) class PermanentWorker { private: char *name; int salary; public: PermanentWorker(const char* nam, int money) : salary(money) { name = new char[strlen(nam) + 1]; strcpy(name, nam); } int getPay() const { return salary; ...