Question: C++ Problem Code Isnt working, I assume its a problem with the template set up. Can you please show the code working when you answer,

C++ Problem

Code Isnt working, I assume its a problem with the template set up. Can you please show the code working when you answer, THANK YOU!

//Interface For ADT List

//File ListInterface.h

#ifndef LIST_INTERFACE_H

#define LIST_INTERFACE_H

template

class ListInterface

{

public:

virtual bool isEmpty() const = 0;

virtual int getLength() const = 0;

virtual bool insert(int newPosition, const T& newEntry) = 0;

virtual T getEntry(int position) const = 0;

virtual T replace(int position, const T& newEntry) = 0;

virtual ~ListInterface() {}

};

#endif

//Check.h

#ifndef CHECK_H

#define CHECK_H

#include

#include"ListInterface.h"

#include

#include

using namespace std;

//Use a Linked List to impliment the ADT checkbook

template

class Check

{

//Check should have at least a checknumber, amount and payTo field

int checknumber;

T currentBalance;

string payTo;

Check *next;

public:

//constructor

Check();

//get/set methods.

void setNext(Check* newNext);

Check* getNext() {

return next;

}

double getCurrentBalance();

void setCurrentBalance(T bal);

double deposit(double amt);

double withdraw(double amt);

double getCheckNumber();

double setCheckNumber(int num);

string getPayTo();

void setPayTo(string payto);

};

#include"Check.cpp"

#endif

//Checkbook.h

#ifndef CHECKBOOK_H

#define CHECKBOOK_H

#include

#include"ListInterface.h"

#include"Check.h"

#include

using namespace std;

template

class CheckBook : public ListInterface

{

public:

Check *start = NULL;

bool isEmpty() const;

int getLength() const;

//Inserting a new check at new position

bool insert(int newPosition, const T& newEntry);

T getEntry(int position) const;

T replace(int position, const T& newEntry);

};

#include"Checkbook.h"

#endif

//Check.cpp

#include "Check.h"

#include

template

Check::Check()

{

checknumber = 0;

currentBalance = 0;

payTo = "";

next = NULL;

}

//get/set methods.

template

void Check::setNext(Check* newNext)

{

next = newNext;

}

template

double Check::getCurrentBalance()

{

return currentBalance;

}

template

void Check::setCurrentBalance(T bal)

{

currentBalance = bal;

}

template

double Check::deposit(double amt)

{

currentBalance += amt;

cout << endl << "Check deposit successful";

}

template

double Check::withdraw(double amt)

{

//The balance should not be allowed to go under 0.

if (currentBalance >= amt)

{

currentBalance -= amt;

cout << endl << "Your Check is passed successfully";

}

else

{

cout << endl << "Cannot pass this check. Your balance is too low";

}

}

template

double Check::getCheckNumber()

{

return checknumber;

}

template

double Check::setCheckNumber(int num)

{

checknumber = num;

}

template

string Check::getPayTo()

{

return payTo;

}

template

void Check::setPayTo(string payto)

{

payTo = payto;

}

//Checkbook.cpp

#include "Checkbook.h"

#include

#include"ListInterface.h"

#include"Check.h"

#include

using namespace std;

template

bool Checkbook::isEmpty() const

{

return (start == NULL);

}

template

int Checkbook::getLength() const

{

Check *temp = start;

int count = 0;

while (temp != NULL)

{

count++;

temp = temp->getNext();

}

return count;

}

//Inserting a new check at new position

template

bool Checkbook::insert(int newPosition, const T& newEntry)

{

Check *temp = start, *prev;

int p = 0;

while (temp != NULL)

{

if (p == newPosition) break;

p++;

prev = temp;

temp = temp->getNext();

}

prev->next = newEntry;

newEntry.setNext(temp);

return true;

}

template

T Checkbook::getEntry(int position) const

{

Check *temp = start;

int p = 0;

while (temp != NULL)

{

if (p == position) break;

p++;

temp = temp->getNext();

}

return temp->getCurrentBalance();

}

template

T Checkbook::replace(int position, const T& newEntry)

{

Check *temp = start, *t;

int p = 0;

while (temp != NULL)

{

if (p == position) break;

p++;

temp = temp->getNext();

}

temp->setCurrentBalance(newEntry);

return temp->getCurrentBalance();

}

//Main.cpp

#include

#include"Checkbook.h"

#include"Check.h"

#include

using namespace std;

int main()

{

CheckBookCT;

if (CT.isEmpty())

{

cout << endl << "Empty list created";

}

Check*one;

one->setCheckNumber(90);

one->deposit(5000);

one->setPayTo("self");

CT.start = one;

Check*two;

two->setCheckNumber(91);

two->deposit(3000);

two->setPayTo("self");

CT.start->setNext(two);

Check*three;

three->setCheckNumber(92);

three->deposit(1000);

three->setPayTo("self");

two->setNext(three);

Check*four;

four->setCheckNumber(94);

four->withdraw(2000);

four->setPayTo("Jhon");

three->setNext(four);

Check*five;

five->setCheckNumber(95);

five->withdraw(10000);

five->setPayTo("Mary");

four->setNext(five);

five->setNext(NULL);

cout << endl << "Length of the list: " << CT.getLength();

cout << endl << " Balance at position 3: " << CT.getEntry(3);

//cout<getCheckNumber()<<" "<

cout << endl << "After replacing 5th entry with new amount ";

cout << endl << "Balance at position 5: " << CT.replace(5, 2000);

//cout<

return 0;

}

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!