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

I need a help for my coding question... Here is the guideline: Copy the solution

ID: 663673 • Letter: I

Question

I need a help for my coding question...

Here is the guideline:

Copy the solution from problem F1.
Change the Node class to be a template class.
In the Node class change the Car * pointer to a T * pointer where T is the template generated type.

Everywhere that Node is used in the program, change it to Node<Car>

The Code that I used for F1:

/*

Assignment F

Problem F1

*/

#include <iostream>

#include <string>

#include <iomanip>

#include <fstream>

#include <cstdlib>

#include<bits/stdc++.h>

#define nullptr NULL

using namespace std;

enum Kind { business, maintenance, other, box, tank, flat, otherFreight, chair, sleeper, otherPassenger };

const string KIND_ARRAY[10] = { "business", "maintenance", "other", "box", "tank", "flat", "otherFreight",

"chair", "sleeper", "otherPassenger" };

class Car

{

public:

     string reportingMark;

     int carNumber;

     Kind kind;

     bool loaded;

     string destination;

public:

     Car()

     {

         setUp("", 0, "other", false, "NONE");

     }

     Car(Car &obj)

     {

         setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind], obj.loaded, obj.destination);

     }

     Car(const string &repMark1, const int &carNumber1, const string &kind1, const bool &loaded1, const string &dest1)

     {

         setUp(repMark1, carNumber1, kind1, loaded1, dest1);

     }

     virtual ~Car(){}

     void setUp(string reportingMark1, int carNumber1, string kind1, bool loaded1, string destination1);

     void output();

     friend bool operator==(const Car &car1, const Car &car2);

     Car & operator=(const Car & carB);

     virtual void setKind(const string &x);

};

class Node

{

Node* next;

Car *data;

Node()

{

   

}

public:

friend class StringOfCars;

};

class StringOfCars

{

private:

Node *head;

Node *tail;

     Car **ptr;

     static const int ARRAY_MAX_SIZE = 10;

     int size;

public:

     StringOfCars()

     {

     head=NULL;

     tail=NULL;

     ptr = new Car*[ARRAY_MAX_SIZE];

         size = 0;

     }

     StringOfCars(const StringOfCars &oldObj)

     {

     Node *currentNodeptr=new Node;

     currentNodeptr->next=oldObj.head;

         head=NULL;

         tail=NULL;

         if(oldObj.head!=NULL)

    {

      while(currentNodeptr!=NULL)

      {

        Car *temp=currentNodeptr->data;

        push(*temp);

        currentNodeptr=currentNodeptr->next;

      }

    }

     }

     ~StringOfCars()

     {

         for (int x = 0; x < size;x++)

         {

              delete ptr[x];

              ptr[x] = nullptr;

         }

         delete[]ptr;

         ptr = nullptr;

     }

     void push(Car &x);

     void pop(Car &x);

     void output();

};

class FreightCar :public Car

{

public:

     FreightCar(){

         setUp("", 0, "other", false, "NONE");

     }

     FreightCar(const FreightCar &obj) {

         setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind], obj.loaded, obj.destination);

         setKind(KIND_ARRAY[obj.kind]);

     }

     FreightCar(const string &rM, const int &cN, const string &kind, const bool &load, const string &dest) {

         setUp(rM, cN, kind, load, dest);

     }

     void setKind(const string &x);

};

class PassengerCar :public Car

{

public:

     PassengerCar(){

         setUp("", 0, "other", false, "NONE");

     }

     PassengerCar(const PassengerCar &obj){

         setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind], obj.loaded, obj.destination);

     }

     PassengerCar(const string &rM, const int &cN, const string &kind, const bool &load, const string &dest){

         setUp(rM, cN, kind, load, dest);

     }

     void setKind(const string &x);

};

void input(StringOfCars &obj);

bool operator==(const Car &car1, const Car &car2);

void buildCar(StringOfCars &obj,string rMark, int carNumber, string kind, bool loaded, string destination);

void buildFreightCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination);

void buildPassengerCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination);

int main()

{

     StringOfCars stringOfCars1;

     //FreightCar x("GN", 744, "combine", false, "Salt Lake");

     //stringOfCars1.push(x);

     //stringOfCars1.output();

     //stringOfCars1.pop(x);

     //stringOfCars1.output();

// return 0;

     input(stringOfCars1);

     //return 0;

     //cout<<" yes done "<<endl;

     stringOfCars1.output();

     //cout<<" back "<<endl;

     return 0;

}

// **********************************************************

// Car

// **********************************************************

/* ***********************Car::setUp*************************

Five parameters passed by value: reportingMark, carNumber,

kind, loaded, and destination. Puts data from arguments to

class member variables.

*/

void Car::setUp(string reportingMark1, int carNumber1, string kind1, bool loaded1, string destination1)

{

     reportingMark = reportingMark1;

     carNumber = carNumber1;

     setKind(kind1);

     loaded = loaded1;

     destination = destination1;

}

/* ***********************Car::output*************************

No parameters. Prints the class member data in a neat format.

*/

void Car::output()

{

     cout << "reportingMark: " << reportingMark << endl;

     //cout<<" yoman "<<endl;

     cout << "carNumber:     " << carNumber << endl;

     cout << "kind:          " << KIND_ARRAY[kind] << endl;

     if (loaded == true)

         cout << "loaded:        " << "true" << endl;

     else

         cout << "loaded:        " << "false" << endl;

     cout << "destination:   " << destination << endl;

}

// Car operator= **************************************************

Car & Car::operator=(const Car & carB)

{

     setUp(carB.reportingMark, carB.carNumber, KIND_ARRAY[carB.kind], carB.loaded, carB.destination);

     return *this;

}

/* ***********************Car::setKind*************************

One parameter by const reference. Sets the correct kind.

*/

void Car::setKind(const string &kind1)

{

     if (kind1 != "business" && kind1 != "maintenance")

         kind = other;

     else if (kind1 == "business")

         kind = business;

     else if (kind1 == "maintenance")

         kind = maintenance;

}

/* ***********************operator==****************************

Two parameters passed by const reference: car1 and car2

Function defines what == returns when comparing two Car objects.

*/

bool operator==(const Car &car1, const Car &car2)

{

     bool x;

     if ((car1.reportingMark == car2.reportingMark) && (car1.carNumber == car2.carNumber))

         x = true;

     else

         x = false;

     return x;

}

// **********************************************************

// StringOfCars

// **********************************************************

/* ***********************StringOfCars::output*****************

No parameters. Prints a heading for each car and calls car class ouput function;

*/

void StringOfCars::output()

{

Node * currentNodePtr=head;

if(head==NULL)

{

    cout << "NO cars";

}

else

{

    //cout<<" printing "<<endl;

    currentNodePtr=head;

    while(currentNodePtr!=NULL)

    {

      //cout<<" inside "<<endl;

      currentNodePtr->data->output();

      currentNodePtr=currentNodePtr->next;

      //cout<<" inside 2"<<endl;

    }

}

}

/* ***********************StringOfCars::push*****************

One parameter by reference. Adds a car to the string of cars.

*/

void StringOfCars::push(Car &x)

{

Node *currentNodePtr=new Node;

Car *temp2(&x);

currentNodePtr->data=temp2;

currentNodePtr->next=NULL;

if(head==NULL)

{

    //cout<<" pushing first element "<<endl;

    head=currentNodePtr;

    tail=currentNodePtr;

    //head->data->output();

}

   else

   {

     tail->next=currentNodePtr;

   }

   tail=currentNodePtr;

   tail->next=NULL;

}

/* ***********************StringOfCars::pop*****************

One parameter by reference. Returns nothing. Removes a car from the string.

*/

void StringOfCars::pop(Car &x)

{

     if (size > 0)

     {

         size--;

         x = (*ptr[size]);

         delete ptr[size];

         ptr[size] = nullptr;

     }

}

// **********************************************************

// FreightCar

// **********************************************************

/* *********************FreightCar::setKind******************

One parameters by const reference. Sets the correct kind.

*/

void FreightCar::setKind(const string &kind1)

{

     if (kind1 != "box" && kind1 != "tank" && kind1 != "flat")

         kind = otherFreight;

     else if (kind1 == "box")

         kind = box;

     else if (kind1 == "tank")

         kind = tank;

     else if (kind1 == "flat")

         kind = flat;

}

// **********************************************************

// PassengerCar

// **********************************************************

/* *********************PassengerCar::setKind******************

One parameters by const reference. Sets the correct kind.

*/

void PassengerCar::setKind(const string &kind1)

{

     if (kind1 != "chair" && kind1 != "sleeper")

         kind = otherPassenger;

     else if (kind1 == "chair")

         kind = chair;

     else if (kind1 == "sleeper")

         kind = sleeper;

}

/* *************************input****************************

No parameters. Reads data from file and calls appropriate build function.

*/

void input(StringOfCars &obj)

{

     ifstream inputFile;

     inputFile.open("Cars.txt");

     if (!inputFile)

     {

         cerr << "Error: File did not open. Program will terminate";

         exit(1);

     }

     string reportingMark; int carNumber; string kind; bool loaded; string destination;

     string type;

     string order;

     int counter = 0;

     while (inputFile.peek() != EOF)

     {

         string trueFalse;

         inputFile >> type;

         inputFile >> order;

         inputFile >> reportingMark;

         inputFile >> carNumber;

         inputFile >> kind;

         inputFile >> trueFalse;

         if (trueFalse == "true")

              loaded = true;

         else

              loaded = false;

         while (inputFile.peek() == ' ')

              inputFile.get();

         getline(inputFile, destination);

         //Car temp(reportingMark, carNumber, kind, loaded, destination);

         //obj.push(temp);

         counter++;

         //cout << "car" << counter << " (" << carNumber << ")" << endl;

         if (type == "Car")

              buildCar(obj, reportingMark, carNumber, kind, loaded, destination);

         else if (type == "FreightCar")

              buildFreightCar(obj, reportingMark, carNumber, kind, loaded, destination);

         else if (type == "PassengerCar")

              buildPassengerCar(obj, reportingMark, carNumber, kind, loaded, destination);

     }

}

/* ***********************buildCar****************************

Five parameters passed by value. Parameters needed to build car.

Creates car object and uses car ouput function.

*/

void buildCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination)

{

     Car *x = new Car(rMark, carNumber, kind, loaded, destination);

     obj.push(*x);

}

/* ***********************buildFreightCar****************************

Five parameters passed by value. Parameters needed to build Freightcar.

Creates FreightCar object and uses car ouput function.

*/

void buildFreightCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination)

{

     Car *x = new FreightCar(rMark, carNumber, kind, loaded, destination);

     obj.push(*x);

}

/* ***********************buildPassengerCar***************************

Five parameters passed by value. Parameters needed to build PassengerCar.

Creates PassengerCar object and uses car ouput function.

*/

void buildPassengerCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination)

{

     Car *x = new PassengerCar(rMark, carNumber, kind, loaded, destination);

     obj.push(*x);

}

Explanation / Answer

CODE is below. I have made all the necessary changes as per the instruction and the program is working fine. As I am not having actual input format so I am not able to provide the output. Please run the program and verify the result. I have tested it otherwise.

CODE :

/*

Assignment F

Problem F1

*/

#include <iostream>

#include <string>

#include <iomanip>

#include <fstream>

#include <cstdlib>

#include<bits/stdc++.h>

#define nullptr NULL

using namespace std;

enum Kind { business, maintenance, other, box, tank, flat, otherFreight, chair, sleeper, otherPassenger };

const string KIND_ARRAY[10] = { "business", "maintenance", "other", "box", "tank", "flat", "otherFreight",

"chair", "sleeper", "otherPassenger" };

class Car

{

public:

     string reportingMark;

     int carNumber;

     Kind kind;

     bool loaded;

     string destination;

public:

     Car()

     {

         setUp("", 0, "other", false, "NONE");

     }

     Car(Car &obj)

     {

         setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind], obj.loaded, obj.destination);

     }

     Car(const string &repMark1, const int &carNumber1, const string &kind1, const bool &loaded1, const string &dest1)

     {

         setUp(repMark1, carNumber1, kind1, loaded1, dest1);

     }

     virtual ~Car(){}

     void setUp(string reportingMark1, int carNumber1, string kind1, bool loaded1, string destination1);

     void output();

     friend bool operator==(const Car &car1, const Car &car2);

     Car & operator=(const Car & carB);

     virtual void setKind(const string &x);

};

template <class T>

class Node

{

Node* next;

T* data;

Node()

{

  

  }

public:

friend class StringOfCars;

};

class StringOfCars

{

private:

Node<Car> *head;

Node<Car> *tail;

     Car **ptr;

     static const int ARRAY_MAX_SIZE = 10;

     int size;

public:

     StringOfCars()

     {

       head=NULL;

       tail=NULL;

       ptr = new Car*[ARRAY_MAX_SIZE];

         size = 0;

     }

     StringOfCars(const StringOfCars &oldObj)

     {

       Node<Car> *currentNodeptr=new Node<Car>;

       currentNodeptr->next=oldObj.head;

         head=NULL;

         tail=NULL;

         if(oldObj.head!=NULL)

    {

      while(currentNodeptr!=NULL)

      {

        Car *temp=currentNodeptr->data;

        push(*temp);

        currentNodeptr=currentNodeptr->next;

      }

    }

     }

     ~StringOfCars()

     {

         for (int x = 0; x < size;x++)

         {

              delete ptr[x];

              ptr[x] = nullptr;

         }

         delete[]ptr;

         ptr = nullptr;

     }

     void push(Car &x);

     void pop(Car &x);

     void output();

};

class FreightCar :public Car

{

public:

     FreightCar(){

         setUp("", 0, "other", false, "NONE");

     }

     FreightCar(const FreightCar &obj) {

         setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind], obj.loaded, obj.destination);

         setKind(KIND_ARRAY[obj.kind]);

     }

     FreightCar(const string &rM, const int &cN, const string &kind, const bool &load, const string &dest) {

         setUp(rM, cN, kind, load, dest);

     }

     void setKind(const string &x);

};

class PassengerCar :public Car

{

public:

     PassengerCar(){

         setUp("", 0, "other", false, "NONE");

     }

     PassengerCar(const PassengerCar &obj){

         setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind], obj.loaded, obj.destination);

     }

     PassengerCar(const string &rM, const int &cN, const string &kind, const bool &load, const string &dest){

         setUp(rM, cN, kind, load, dest);

     }

     void setKind(const string &x);

};

void input(StringOfCars &obj);

bool operator==(const Car &car1, const Car &car2);

void buildCar(StringOfCars &obj,string rMark, int carNumber, string kind, bool loaded, string destination);

void buildFreightCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination);

void buildPassengerCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination);

int main()

{

     StringOfCars stringOfCars1;

     //FreightCar x("GN", 744, "combine", false, "Salt Lake");

     //stringOfCars1.push(x);

     //stringOfCars1.output();

     //stringOfCars1.pop(x);

     //stringOfCars1.output();

// return 0;

     input(stringOfCars1);

     //return 0;

     //cout<<" yes done "<<endl;

     stringOfCars1.output();

     //cout<<" back "<<endl;

     return 0;

}

// **********************************************************

// Car

// **********************************************************

/* ***********************Car::setUp*************************

Five parameters passed by value: reportingMark, carNumber,

kind, loaded, and destination. Puts data from arguments to

class member variables.

*/

void Car::setUp(string reportingMark1, int carNumber1, string kind1, bool loaded1, string destination1)

{

     reportingMark = reportingMark1;

     carNumber = carNumber1;

     setKind(kind1);

     loaded = loaded1;

     destination = destination1;

}

/* ***********************Car::output*************************

No parameters. Prints the class member data in a neat format.

*/

void Car::output()

{

     cout << "reportingMark: " << reportingMark << endl;

     //cout<<" yoman "<<endl;

     cout << "carNumber:     " << carNumber << endl;

     cout << "kind:          " << KIND_ARRAY[kind] << endl;

     if (loaded == true)

         cout << "loaded:        " << "true" << endl;

     else

         cout << "loaded:        " << "false" << endl;

     cout << "destination:   " << destination << endl;

}

// Car operator= **************************************************

Car & Car::operator=(const Car & carB)

{

     setUp(carB.reportingMark, carB.carNumber, KIND_ARRAY[carB.kind], carB.loaded, carB.destination);

     return *this;

}

/* ***********************Car::setKind*************************

One parameter by const reference. Sets the correct kind.

*/

void Car::setKind(const string &kind1)

{

     if (kind1 != "business" && kind1 != "maintenance")

         kind = other;

     else if (kind1 == "business")

         kind = business;

     else if (kind1 == "maintenance")

         kind = maintenance;

}

/* ***********************operator==****************************

Two parameters passed by const reference: car1 and car2

Function defines what == returns when comparing two Car objects.

*/

bool operator==(const Car &car1, const Car &car2)

{

     bool x;

     if ((car1.reportingMark == car2.reportingMark) && (car1.carNumber == car2.carNumber))

         x = true;

     else

         x = false;

     return x;

}

// **********************************************************

// StringOfCars

// **********************************************************

/* ***********************StringOfCars::output*****************

No parameters. Prints a heading for each car and calls car class ouput function;

*/

void StringOfCars::output()

{

Node<Car> * currentNodePtr=head;

if(head==NULL)

{

    cout << "NO cars";

}

else

{

    //cout<<" printing "<<endl;

    currentNodePtr=head;

    while(currentNodePtr!=NULL)

    {

      //cout<<" inside "<<endl;

      currentNodePtr->data->output();

      currentNodePtr=currentNodePtr->next;

      //cout<<" inside 2"<<endl;

    }

}

}

/* ***********************StringOfCars::push*****************

One parameter by reference. Adds a car to the string of cars.

*/

void StringOfCars::push(Car &x)

{

Node<Car> *currentNodePtr=new Node<Car>;

Car *temp2(&x);

currentNodePtr->data=temp2;

currentNodePtr->next=NULL;

if(head==NULL)

{

    //cout<<" pushing first element "<<endl;

    head=currentNodePtr;

    tail=currentNodePtr;

    //head->data->output();

}

   else

   {

     tail->next=currentNodePtr;

   }

   tail=currentNodePtr;

   tail->next=NULL;

}

/* ***********************StringOfCars::pop*****************

One parameter by reference. Returns nothing. Removes a car from the string.

*/

void StringOfCars::pop(Car &x)

{

     if (size > 0)

     {

         size--;

         x = (*ptr[size]);

         delete ptr[size];

         ptr[size] = nullptr;

     }

}

// **********************************************************

// FreightCar

// **********************************************************

/* *********************FreightCar::setKind******************

One parameters by const reference. Sets the correct kind.

*/

void FreightCar::setKind(const string &kind1)

{

     if (kind1 != "box" && kind1 != "tank" && kind1 != "flat")

         kind = otherFreight;

     else if (kind1 == "box")

         kind = box;

     else if (kind1 == "tank")

         kind = tank;

     else if (kind1 == "flat")

         kind = flat;

}

// **********************************************************

// PassengerCar

// **********************************************************

/* *********************PassengerCar::setKind******************

One parameters by const reference. Sets the correct kind.

*/

void PassengerCar::setKind(const string &kind1)

{

     if (kind1 != "chair" && kind1 != "sleeper")

         kind = otherPassenger;

     else if (kind1 == "chair")

         kind = chair;

     else if (kind1 == "sleeper")

         kind = sleeper;

}

/* *************************input****************************

No parameters. Reads data from file and calls appropriate build function.

*/

void input(StringOfCars &obj)

{

     ifstream inputFile;

     inputFile.open("C:\Users\sandeep.o.kuma\Desktop\car.txt");

     if (!inputFile)

     {

         cerr << "Error: File did not open. Program will terminate";

         exit(1);

     }

     string reportingMark; int carNumber; string kind; bool loaded; string destination;

     string type;

     string order;

     int counter = 0;

     while (inputFile.peek() != EOF)

     {

         string trueFalse;

         inputFile >> type;

         inputFile >> order;

         inputFile >> reportingMark;

         inputFile >> carNumber;

         inputFile >> kind;

         inputFile >> trueFalse;

         if (trueFalse == "true")

              loaded = true;

         else

              loaded = false;

         while (inputFile.peek() == ' ')

              inputFile.get();

         getline(inputFile, destination);

         //Car temp(reportingMark, carNumber, kind, loaded, destination);

         //obj.push(temp);

         counter++;

         //cout << "car" << counter << " (" << carNumber << ")" << endl;

         if (type == "Car")

              buildCar(obj, reportingMark, carNumber, kind, loaded, destination);

         else if (type == "FreightCar")

              buildFreightCar(obj, reportingMark, carNumber, kind, loaded, destination);

         else if (type == "PassengerCar")

              buildPassengerCar(obj, reportingMark, carNumber, kind, loaded, destination);

     }

}

/* ***********************buildCar****************************

Five parameters passed by value. Parameters needed to build car.

Creates car object and uses car ouput function.

*/

void buildCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination)

{

     Car *x = new Car(rMark, carNumber, kind, loaded, destination);

     obj.push(*x);

}

/* ***********************buildFreightCar****************************

Five parameters passed by value. Parameters needed to build Freightcar.

Creates FreightCar object and uses car ouput function.

*/

void buildFreightCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination)

{

     Car *x = new FreightCar(rMark, carNumber, kind, loaded, destination);

     obj.push(*x);

}

/* ***********************buildPassengerCar***************************

Five parameters passed by value. Parameters needed to build PassengerCar.

Creates PassengerCar object and uses car ouput function.

*/

void buildPassengerCar(StringOfCars &obj, string rMark, int carNumber, string kind, bool loaded, string destination)

{

     Car *x = new PassengerCar(rMark, carNumber, kind, loaded, destination);

     obj.push(*x);

}

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