I need a major help on this!!! => here is instruction of assignment Copy the sol
ID: 663189 • Letter: I
Question
I need a major help on this!!!
=> here is instruction of assignment
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>
Use the same tests as in problem F1.
Here is my code for Assignment F
#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()
{
}
Car(Car &obj)
{
obj.loaded, obj.destination);
}
Car(const string &repMark1, const int &carNumber1, const string
&kind1, const bool &loaded1, const string &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;
setUp("", 0, "other", false, "NONE");
setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind],
setUp(repMark1, carNumber1, kind1, loaded1, dest1);
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];
}
StringOfCars(const StringOfCars &oldObj)
{
Node *currentNodeptr=new Node;
currentNodeptr->next=oldObj.head;
size = 0;
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(){
}
FreightCar(const FreightCar &obj) {
obj.loaded, obj.destination);
}
FreightCar(const string &rM, const int &cN, const string &kind,
const bool &load, const string &dest) {
}
void setKind(const string &x);
};
class PassengerCar :public Car
{
public:
PassengerCar(){
}
PassengerCar(const PassengerCar &obj){
obj.loaded, obj.destination);
}
PassengerCar(const string &rM, const int &cN, const string &kind,
const bool &load, const string &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;
}
setUp("", 0, "other", false, "NONE");
setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind],
setKind(KIND_ARRAY[obj.kind]);
setUp(rM, cN, kind, load, dest);
setUp("", 0, "other", false, "NONE");
setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind],
setUp(rM, cN, kind, load, dest);
// **********************************************************
// 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)
else
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")
else if (kind1 == "business")
else if (kind1 == "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)
{
cout << "loaded: " << "true" << endl;
cout << "loaded: " << "false" << endl;
kind = other;
kind = business;
kind = maintenance;
bool x;
if ((car1.reportingMark == car2.reportingMark) && (car1.carNumber
== car2.carNumber))
else
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;
x = true;
x = false;
}
/* ***********************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")
else if (kind1 == "box")
else if (kind1 == "tank")
else if (kind1 == "flat")
}
// **********************************************************
// PassengerCar
// **********************************************************
/* *********************PassengerCar::setKind******************
One parameters by const reference. Sets the correct kind.
*/
void PassengerCar::setKind(const string &kind1)
{
if (kind1 != "chair" && kind1 != "sleeper")
else if (kind1 == "chair")
else if (kind1 == "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)
{
kind = otherFreight;
kind = box;
kind = tank;
kind = flat;
kind = otherPassenger;
kind = chair;
kind = sleeper;
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)
{
destination);
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,
//obj.push(temp);
counter++;
//cout << "car" << counter << " (" << carNumber << ")" <<
if (type == "Car")
buildCar(obj, reportingMark, carNumber, kind, loaded,
else if (type == "FreightCar")
buildFreightCar(obj, reportingMark, carNumber, kind,
else if (type == "PassengerCar")
buildPassengerCar(obj, reportingMark, carNumber, kind,
endl;
destination);
loaded, destination);
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);
}
/*execution result:
reportingMark: CN
carNumber: 819481
kind: maintenance
loaded: false
destination: NONE
reportingMark: SLSF
carNumber: 46871
kind: business
loaded: true
destination: Memphis
reportingMark: AOK
carNumber: 156
kind: other
loaded: true
destination: McAlester
reportingMark: MKT
carNumber: 123456
kind: tank
loaded: false
destination: Fort Worth
reportingMark: MP
carNumber: 98765
kind: box
loaded: true
destination: Saint Louis
reportingMark: SP
carNumber: 567890
kind: flat
loaded: true
destination: Chicago
reportingMark: GMO
carNumber: 7878
kind: otherFreight
loaded: true
destination: Mobile
reportingMark: KCS
carNumber: 7893
kind: chair
loaded: true
destination: Kansas City
reportingMark: PAPX
carNumber: 145
kind: sleeper
loaded: true
destination: Tucson
reportingMark: GN
carNumber: 744
kind: otherPassenger
loaded: false
destination: NONE
*/
Explanation / Answer
ok...changed Node to template Node<car>
here are files
template <class Car> class Node
{
Car data; //information of object
Node* next;
setUp("", 0, "other", false, "NONE");
setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind],
setUp(repMark1, carNumber1, kind1, loaded1, dest1);
Car *data;
Node<Car>()
{
}
public:
friend class StringOfCars;
};
-----------------------------------------
#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()
{
}
Car(Car &obj)
{
obj.loaded, obj.destination);
}
Car(const string &repMark1, const int &carNumber1, const string
&kind1, const bool &loaded1, const string &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 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];
}
StringOfCars(const StringOfCars &oldObj)
{
Node<Car> *currentNodeptr=new Node<Car>;
currentNodeptr->next=oldObj.head;
size = 0;
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(){
}
FreightCar(const FreightCar &obj) {
obj.loaded, obj.destination);
}
FreightCar(const string &rM, const int &cN, const string &kind,
const bool &load, const string &dest) {
}
void setKind(const string &x);
};
----------------------------------------------------------------------------------------------
class PassengerCar :public Car
{
public:
PassengerCar(){
}
PassengerCar(const PassengerCar &obj){
obj.loaded, obj.destination);
}
PassengerCar(const string &rM, const int &cN, const string &kind,
const bool &load, const string &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;
}
setUp("", 0, "other", false, "NONE");
setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind],
setKind(KIND_ARRAY[obj.kind]);
setUp(rM, cN, kind, load, dest);
setUp("", 0, "other", false, "NONE");
setUp(obj.reportingMark, obj.carNumber, KIND_ARRAY[obj.kind],
setUp(rM, cN, kind, load, dest);
// **********************************************************
// 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)
else
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")
else if (kind1 == "business")
else if (kind1 == "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)
{
cout << "loaded: " << "true" << endl;
cout << "loaded: " << "false" << endl;
kind = other;
kind = business;
kind = maintenance;
bool x;
if ((car1.reportingMark == car2.reportingMark) && (car1.carNumber
== car2.carNumber))
else
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;
x = true;
x = false;
}
/* ***********************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")
else if (kind1 == "box")
else if (kind1 == "tank")
else if (kind1 == "flat")
}
// **********************************************************
// PassengerCar
// **********************************************************
/* *********************PassengerCar::setKind******************
One parameters by const reference. Sets the correct kind.
*/
void PassengerCar::setKind(const string &kind1)
{
if (kind1 != "chair" && kind1 != "sleeper")
else if (kind1 == "chair")
else if (kind1 == "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)
{
kind = otherFreight;
kind = box;
kind = tank;
kind = flat;
kind = otherPassenger;
kind = chair;
kind = sleeper;
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)
{
destination);
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,
//obj.push(temp);
counter++;
//cout << "car" << counter << " (" << carNumber << ")" <<
if (type == "Car")
buildCar(obj, reportingMark, carNumber, kind, loaded,
else if (type == "FreightCar")
buildFreightCar(obj, reportingMark, carNumber, kind,
else if (type == "PassengerCar")
buildPassengerCar(obj, reportingMark, carNumber, kind,
endl;
destination);
loaded, destination);
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);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.