I need someone to do lab F1 for me, I have attached E4 to this. #include #includ
ID: 666420 • Letter: I
Question
I need someone to do lab F1 for me, I have attached E4 to this.
#include
#include
#include
#include
using namespace std;
enum Kind {business, maintenance, other, box, tank, flat, otherFreight, chair, sleeper, otherPassenger};
string KIND_ARRAY[]={"business", "maintenance", "other", "box", "tank", "flat", "otherFreight", "chair", "sleeper",
"otherPassenger"};
class Car
{
public:
string reportingMark;
int carNumber;
Kind kind;
bool loaded;
string destination;
public:
// #1 DEFAULT constructor
Car()
{
setUp("", 0, other,false, "NONE");
}
// #2 COPY constructor
Car(const Car &obj)
{
setUp(obj.reportingMark, obj.carNumber ,obj.kind, obj.loaded,obj.destination);
}
// #3 accept five constant referance parameters constructor
Car(string reportMark, int cNumber, Kind knd, bool load, string destition)
{
setUp(reportMark, cNumber, knd, load, destition);
}
// #4 DESTRUCTOR
virtual ~Car()
{
}
// Setup
virtual void setUp(string reportMark, int cNumber, Kind knd, bool load, string destition);
// output member function
void output();
// setKind
virtual void setKind(const Kind k)
{
if (k != business && k != maintenance)
kind = other;
}
//Operator Overloading
Car & operator = (const Car & carB);
//operator function
friend bool operator ==(Car A, Car b);
};
/**************************************
* FreightCar Class *
**************************************/
class FreightCar : public Car
{
public:
FreightCar():Car()
{
}
FreightCar(const FreightCar & old) : Car(old)
{
}
FreightCar(string a, int b, Kind c, bool d, string e):Car(a, b, c, d, e)
{
}
//This could be made Virtual but it doesn't have to since its a derived class.
virtual void setKind(const Kind k)
{
if (k != box && k != tank && k != flat)
kind = otherFreight
}
};
/*****************************************
* PassengerCar Class *
*****************************************/
class PassengerCar : public Car
{
public:
PassengerCar():Car()
{
}
PassengerCar(const PassengerCar &old) : Car(old)
{
}
PassengerCar(string a, int b, Kind c, bool d, string e) : Car(a, b, c, d, e)
{
}
//This could be made Virtual but it doesn't have to since its a derived class.
virtual void setKind(const Kind k)
{
if (k != chair && k != sleeper)
kind = otherPassenger;
}
};
/*****************************************
* StringOfCars Class *
*****************************************/
class StringOfCars
{
Car **ptr;
static const int ARRAY_MAX_SIZE = 10;
int size;
public:
// DEFAULT constructor
StringOfCars()
{
ptr = new Car*[ARRAY_MAX_SIZE];
size = 0;
}
//COPY constructor
StringOfCars(const StringOfCars &old)
{
ptr = new Car*[ARRAY_MAX_SIZE];
for (int i = 0; i < ARRAY_MAX_SIZE; i++)
{
Car *p = new Car;
p->reportingMark = old.ptr[i]->reportingMark;
p->carNumber = old.ptr[i]->carNumber;
p->kind = old.ptr[i]->kind;
p->loaded = old.ptr[i]->loaded;
p->destination = old.ptr[i]->destination;
ptr[i]=p;
}
size = old.size;
}
//DESTRUCTOR
virtual ~StringOfCars()
{
delete [] ptr;
}
//Push Function
void push(Car & add);
//Pop Function
void pop(Car & a);
//Output Function
void output();
};
void input(StringOfCars &a);
/********************************
* main function *
********************************/
int main()
{
StringOfCars StringOfcar1;
input(StringOfcar1);
StringOfCars StringOfcar2 = StringOfcar1;
StringOfcar2.output();
return 0;
}
void buildCar(string reportMark, int cNumber, Kind knd, bool load, string destition, StringOfCars & a)
{
Car Cartest(reportMark, cNumber, knd, load, destition);
Cartest.setKind(knd);
a.push(Cartest);
}
void buildPassengerCar(string reportMark, int cNumber, Kind knd, bool load, string destition, StringOfCars & a)
{
PassengerCar Cartest(reportMark, cNumber, knd, load, destition);
Cartest.setKind(knd);
a.push(Cartest);
}
void buildFreightCar(string reportMark, int cNumber, Kind knd, bool load, string destition, StringOfCars & a)
{
FreightCar Cartest(reportMark, cNumber, knd, load, destition);
Cartest.setKind(knd);
a.push(Cartest);
}
void Car::output()
{
cout << left;
cout << setw(20) << "reportingMark" << " " << reportingMark << endl;
cout << setw(20) << "carNumber" << " " << carNumber << endl;
cout << setw(20) << "kind" << " " << KIND_ARRAY[kind] << endl;
if (loaded)
cout << setw(20) << "loaded" << " " << "true" << endl;
else
cout << setw(20) << "loaded" << " " << "false" << endl;
cout << setw(20) << "destination" << " " << destination << endl;
cout << endl;
}
void Car::setUp(string reportMark, int cNumber, Kind knd, bool load, string destition)
{
reportingMark=reportMark;
carNumber=cNumber;
kind =knd;
loaded=load;
destination=destition;
}
/********************************
* operator function *
********************************/
bool operator == (Car A, Car B)
{
if (A.reportingMark == B.reportingMark && A.carNumber == B.carNumber)
return true;
else
return false;
}
/***********************************
* Operator Overloading *
***********************************/
Car & Car::operator=(const Car & carB)
{
setUp(carB.reportingMark, carB.carNumber, carB.kind, carB.loaded, carB.destination);
return * this;
}
/********************************************
* Output function for StringOfCar *
********************************************/
void StringOfCars::output()
{
cout << "car number is:" << size << endl;
for (int i = 0; i < size; i++)
{
ptr[i]->output();
cout << endl;
}
}
/****************************
* push function *
****************************/
void StringOfCars::push(Car &add)
{
Car *pointer = new Car;
*pointer = add;
ptr[size] = pointer;
size++;
}
/***************************
* pop function *
***************************/
void StringOfCars::pop(Car &a)
{
a = *ptr[size-1];
delete ptr[size-1];
size--;
}
/********************************
* input function *
********************************/
void input(StringOfCars & a)
{
ifstream inputFile;
inputFile.open("data.txt");
if (!inputFile)
{
cout << "File open failure";
}
else
{
string type;
string reportingMark;
string order;
int carNumber;
Kind kind = otherPassenger;
string kindinput;
bool loaded = false;
string loadedinput;
string destination;
while (inputFile.peek() != EOF)
{
inputFile >> type;
inputFile >> order;
inputFile >> reportingMark;
inputFile >> carNumber;
inputFile >> kindinput;
kind = otherFreight;
for (int i = 0; i < 9; i ++)
{
if (kindinput == KIND_ARRAY[i])
{
kind = static_cast(i);
break;
}
}
inputFile >> loadedinput;
if (loadedinput == "true")
loaded = true;
else if (loadedinput == "false")
loaded = false;
else
cout << "wrong loaded input"<
while (inputFile.peek() == ' ')
inputFile.get();
getline(inputFile, destination);
if (type == "Car")
buildCar(reportingMark,carNumber,kind,loaded,destination, a);
else if (type == "FreightCar")
buildFreightCar(reportingMark, carNumber, kind, loaded, destination, a);
else if (type == "PassengerCar")
buildPassengerCar(reportingMark, carNumber, kind, loaded, destination, a);
}
}
inputFile.close();
}
Assignment F
Use the style guide, including Additonal styles beginning with
Assignment D.
Problem F1
Copy the solution from problem E4.
Again we will change the implementation of the StringOfCars class, but
keep the public interface. This implementation will use a linked list,
rather than an array or vector to keep the cars in.
So keep all the public function prototypes in the StringOfCars.
Discard all the private data and the implementation of the functions; we
will rebuild all of these.
Do not change anything outside the StringOfCars class.
Build a new class called Node:
We will build our link list using Node objects, linked together to make
a list.
In the private data of the Node class put two pointers:
One with type Node * and name next, which will point to the next node in
the linked list.
The other with type Car* and name data, which will point to the Car data
associated with this node.
Also in the private area create a default constructor that sets the next
and data pointers to zero. Because the constructor is private, only
friends can use this class.
In the public area of the Node class, make StringOfCars a friend class.
The order of the following three things is important:
1. Declare the StringOfCars class with: class StringOfCars;
2. The Node class
3. The StringOfCars class
This is needed because the Node class uses the StringOfCars and the
StringOfCars class uses the Node class.
In the StringOfCars class implementation:
Replace the private data with two pointers of type Node *, and nothing
else. Name these two pointers head and tail.
Change the StringOfCars default constructor to set the head and tail
pointers to zero.
Rebuild the push function, with the same function heading.
Declare a local pointer variable of type Car * named currentCarPtr.
Declare a local pointer variable of type Node * named currentNodePtr.
Use new to get space in the heap for a Node and save the address of the
space in the heap in currentNodePtr
Use new get space in the heap for a new car that is a copy of the car
prameter of the push function as you did in problem E4 and save the
address of the space in the heap in currentCarPtr
Set the data pointer in this new Node object to point to the new car
object.
If the head pointer is zero
set the head and the tail pointer to the value of currentNodePtr
else
set the next pointer of the Node object pointed to by the tail pointer
to the value of currentNodePtr
set the last pointer to the value of the currentNodePtr
Do not write a pop function.
Rebuild the output function, with the same function heading.
Declare a local pointer variable of type Node * named currentNodePtr -
it will point to the Node we are currently working on.
if the head pointer is zero
print: NO cars
else
set the currentNodePointer to the value of the head pointer
while the currentNodePointer is not zero
print the Car pointed to by the currentNodePointer
set the currentNodePtr to the next pointer in the Node pointed to
by currentNodePtr, which now makes the next Node the current Node
Rebuild the copy construtor.
Declare a local pointer variable of type Node * named currentNodePtr -
it will point to the Node in the oldStringOfCars that we are currently
working on.
Set the head and tail pointers in the StringOfCars being constructed to
zero.
If the oldStringOfCars head pointer is not zero:
loop while the currentNodePointer is not zero,
push the Car pointed to by the data pointer in the current Node,
which is pointed to by the currentNodePointer.
set the currentNodePtr to the next pointer in the currentNodePtr
so we now make the next Node the current Node
Use the same tests as in problem E4.
Explanation / Answer
#include <new>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <cstdlib>
using namespace std;
enum Kind {business, maintenance, other, box, tank, flat, otherFreight, chair,
sleeper, otherPassenger};
string KIND_ARRAY={"business", "maintenance", "other", "box", "tank", "flat",
"otherFreight", "chair", "sleeper", "otherPassenger"};
class Car
{
protected:
string reportingMark;
int carNumber;
Kind kind;;
bool loaded;
string destination;
public:
Car(const Car & t )
{
*this = t;
}
Car( string r,int c,string K, bool l,string d);
Car ();
virtual ~Car();
void setUp( string reportingMark, int carNumber, string Kind, bool loaded,
string destination);
void output();
virtual void setKind(string const_string);
friend bool operator==(Car carA, Car carB);
Car &operator = (const Car &);
};
class FreightCar:public Car
{
public:
FreightCar()
{
reportingMark = "";
carNumber
= 0;
kind
= other;
loaded
= false;
destination
= "NONE";
}
FreightCar(const FreightCar &obj)
{
reportingMark = obj.reportingMark;
carNumber
= obj.carNumber;
kind
= obj.kind;
loaded
= obj.loaded;
destination
= obj.destination;
}
FreightCar(string report_mark, int car_num, string kind_car, bool
loaded_of_car, string d_place)
{
setUp(report_mark,car_num,kind_car,loaded_of_car,d_place)
}
void setKind(string const_string);
};
void FreightCar::setKind(string const_string)
{
if (const_string == "box")
= "NONE";
}
FreightCar(const FreightCar &obj)
{
reportingMark = obj.reportingMark;
carNumber
= obj.carNumber;
kind
= obj.kind;
loaded
= obj.loaded;
destination
= obj.destination;
}
FreightCar(string report_mark, int car_num, string kind_car, bool
loaded_of_car, string d_place)
{
setUp(report_mark,car_num,kind_car,loaded_of_car,d_place)
}
void setKind(string const_string);
};
void FreightCar::setKind(string const_string)
{
if (const_string == "box")
reportingMark = obj.reportingMark;
carNumber
= obj.carNumber;
kind
= obj.kind;
loaded
= obj.loaded;
destination
= obj.destination;
}
PassengerCar(string report_mark, int car_num, string kind_car, bool
loaded_of_car, string d_place)
{
setUp(report_mark,car_num,kind_car,loaded_of_car,d_place);
}
};
void setKind(string const_string);
void PassengerCar::setKind(string const_string)
{
if (const_string == "chair")
kind = chair;
else if (const_string == "sleeper")
kind = sleeper;
else
kind = otherPassenger;
}
class Node
{
private:
Node* next;
Car* data;
Node ()
{
next = 0;
data = 0;
}
public:
friend class StringOfCar;
};
class StringOfCar
{
private:
Node* head;
Node* tail;
public:
StringOfCar()
{
head = 0;
tail = 0;
}
StringOfCar(const StringOfCar &oldStringOfCar)
{
Node *currentNodePtr;
head = 0;
tail = 0;
head = oldStringOfCar.head;
tail = oldStringOfCar.tail;
currentNodePtr = oldStringOfCar.head;
if (oldStringOfCar.head != 0)
{
while (currentNodePtr != 0)
{
push(*(currentNodePtr->data));
currentNodePtr->next = 0;
currentNodePtr = (currentNodePtr->next);
}
}
}
~StringOfCar()
{
}
void push(Car &new_car);
void output();
};
void StringOfCar::output()
{
Node * currentNodePtr;
if (head == 0)
cout << "NO cars ";
else
{
currentNodePtr = head;
}
}
while (currentNodePtr != 0)
{
currentNodePtr->data->output();
currentNodePtr = (currentNodePtr->next);
}
void StringOfCar::push(Car &new_car)
{
Car *currentCarPtr;
Node *currentNodePtr;
currentNodePtr = new
currentCarPtr = new
currentNodePtr->next
void push(Car &new_car);
void output();
};
void StringOfCar::output()
{
Node * currentNodePtr;
if (head == 0)
cout << "NO cars ";
else
{
currentNodePtr = head;
}
}
while (currentNodePtr != 0)
{
currentNodePtr->data->output();
currentNodePtr = (currentNodePtr->next);
}
void StringOfCar::push(Car &new_car)
{
Car *currentCarPtr;
Node *currentNodePtr;
currentNodePtr = new
currentCarPtr = new
currentNodePtr->next
currentNodePtr->data
Node;
Car(new_car);
= 0;
= &new_car;
if (head == 0)
{
head = currentNodePtr;
tail = currentNodePtr;
}
else
{
tail->next = currentNodePtr;
tail
= currentNodePtr;
}
}
void input(StringOfCar&);
int main()
{
StringOfCar car1;
input(car1);
car1.output();
}
void input(StringOfCar&string1)
{
string rep;
string type;
string dest;
string loadString;
int carNum;
bool load;
ifstream inputFile;
inputFile.open("input2.txt");
if (inputFile.fail())
{
cerr<< "Error, File not found ";
exit(EXIT_FAILURE);
}
string carType;
while (inputFile.peek()!=EOF)
{
while (inputFile.peek() == ' ')
{
inputFile.get();
}
inputFile>>carType;
inputFile>>rep;
inputFile>>carNum;
inputFile>>type;
inputFile>>loadString;
getline(inputFile, dest);
Car temp (rep, carNum, type, load, dest);
//temp.output();
Car*carptr = &temp;
string1.push(*carptr);
/*if (carType == "Car")
{Car car1;
car1.setKind(type);
string1.push(car1);
}*/
if (type == "FreightCar")
{
FreightCar car2;
car2.setKind(type);
string1.push(car2);
}
if (type == "PassengerCar")
{
PassengerCar car3;
car3.setKind(type);
string1.push(car3);
}
}
inputFile.close();
}
Car & Car::operator=(const Car & carB)
{
reportingMark = carB.reportingMark;
carNumber
= carB.carNumber;
kind
= carB.kind;
loaded
= carB.loaded;
destination
= carB.destination;
return * this;
}
/*
setUp
put data in object
*/
void Car::setUp( string r, int c, string KIND, bool l, string d)
{
reportingMark = r;
carNumber =c;
setKind(KIND);
loaded = l;
destination = d;
F1.cpp
/*/*
Joseph Huang
CIS 22B
Spring 2015
Assignment F
Problem F1
Description of problem:
Introduction to classes (continued)
Overloading function names
Abstract data types
Friend functions
Overloading operators
*/
#include <new>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <cstdlib>
using namespace std;
enum Kind {business, maintenance, other, box, tank, flat, otherFreight, chair,
sleeper, otherPassenger};
string KIND_ARRAY={"business", "maintenance", "other", "box", "tank", "flat",
"otherFreight", "chair", "sleeper", "otherPassenger"};
class Car
{
protected:
string reportingMark;
int carNumber;
Kind kind;;
bool loaded;
string destination;
public:
Car(const Car & t )
{
*this = t;
}
Car( string r,int c,string K, bool l,string d);
Car ();
virtual ~Car();
void setUp( string reportingMark, int carNumber, string Kind, bool loaded,
string destination);
void output();
virtual void setKind(string const_string);
friend bool operator==(Car carA, Car carB);
Car &operator = (const Car &);
};
class FreightCar:public Car
{
public:
FreightCar()
{
reportingMark = "";
carNumber
= 0;
kind
= other;
loaded
= false;
destination
= "NONE";
}
FreightCar(const FreightCar &obj)
{
reportingMark = obj.reportingMark;
carNumber
= obj.carNumber;
kind
= obj.kind;
loaded
= obj.loaded;
destination
= obj.destination;
}
FreightCar(string report_mark, int car_num, string kind_car, bool
loaded_of_car, string d_place)
{
setUp(report_mark,car_num,kind_car,loaded_of_car,d_place)
}
void setKind(string const_string);
};
void FreightCar::setKind(string const_string)
{
if (const_string == "box")
kind = box;
else if (const_string == "tank")
kind = tank;
else if (const_string == "flat")
kind = flat;
else
kind = otherFreight;
}
;
class PassengerCar:public Car
{
public:
PassengerCar()
{
reportingMark = "";
carNumber
= 0;
kind
= other;
loaded
= false;
destination
= "NONE";
}
PassengerCar(const PassengerCar &obj)
{
reportingMark = obj.reportingMark;
carNumber
= obj.carNumber;
kind
= obj.kind;
loaded
= obj.loaded;
destination
= obj.destination;
}
PassengerCar(string report_mark, int car_num, string kind_car, bool
loaded_of_car, string d_place)
{
setUp(report_mark,car_num,kind_car,loaded_of_car,d_place);
}
};
void setKind(string const_string);
void PassengerCar::setKind(string const_string)
{
if (const_string == "chair")
kind = chair;
else if (const_string == "sleeper")
kind = sleeper;
else
kind = otherPassenger;
}
class Node
{
private:
Node* next;
Car* data;
Node ()
{
next = 0;
data = 0;
}
public:
friend class StringOfCar;
};
class StringOfCar
{
private:
Node* head;
Node* tail;
public:
StringOfCar()
{
head = 0;
tail = 0;
}
StringOfCar(const StringOfCar &oldStringOfCar)
{
Node *currentNodePtr;
head = 0;
tail = 0;
head = oldStringOfCar.head;
tail = oldStringOfCar.tail;
currentNodePtr = oldStringOfCar.head;
if (oldStringOfCar.head != 0)
{
while (currentNodePtr != 0)
{
push(*(currentNodePtr->data));
currentNodePtr->next = 0;
currentNodePtr = (currentNodePtr->next);
}
}
}
~StringOfCar()
{
}
void push(Car &new_car);
void output();
};
void StringOfCar::output()
{
Node * currentNodePtr;
if (head == 0)
cout << "NO cars ";
else
{
currentNodePtr = head;
}
}
while (currentNodePtr != 0)
{
currentNodePtr->data->output();
currentNodePtr = (currentNodePtr->next);
}
void StringOfCar::push(Car &new_car)
{
Car *currentCarPtr;
Node *currentNodePtr;
currentNodePtr = new
currentCarPtr = new
currentNodePtr->next
currentNodePtr->data
Node;
Car(new_car);
= 0;
= &new_car;
if (head == 0)
{
head = currentNodePtr;
tail = currentNodePtr;
}
else
{
tail->next = currentNodePtr;
tail
= currentNodePtr;
}
}
void input(StringOfCar&);
int main()
{
StringOfCar car1;
input(car1);
car1.output();
}
/*
input
get the data from the user
*/
void input(StringOfCar&string1)
{
string rep;
string type;
string dest;
string loadString;
int carNum;
bool load;
ifstream inputFile;
inputFile.open("input2.txt");
if (inputFile.fail())
{
cerr<< "Error, File not found ";
exit(EXIT_FAILURE);
}
string carType;
while (inputFile.peek()!=EOF)
{
while (inputFile.peek() == ' ')
{
inputFile.get();
}
inputFile>>carType;
inputFile>>rep;
inputFile>>carNum;
inputFile>>type;
inputFile>>loadString;
getline(inputFile, dest);
Car temp (rep, carNum, type, load, dest);
//temp.output();
Car*carptr = &temp;
string1.push(*carptr);
/*if (carType == "Car")
{Car car1;
car1.setKind(type);
string1.push(car1);
}*/
if (type == "FreightCar")
{
FreightCar car2;
car2.setKind(type);
string1.push(car2);
}
if (type == "PassengerCar")
{
PassengerCar car3;
car3.setKind(type);
string1.push(car3);
}
}
inputFile.close();
}
Car & Car::operator=(const Car & carB)
{
reportingMark = carB.reportingMark;
carNumber
= carB.carNumber;
kind
= carB.kind;
loaded
= carB.loaded;
destination
= carB.destination;
return * this;
}
/*
setUp
put data in object
*/
void Car::setUp( string r, int c, string KIND, bool l, string d)
{
reportingMark = r;
carNumber =c;
setKind(KIND);
loaded = l;
destination = d;
}
/*
output
print the data to the screen
*/
void Car::output()
{
cout<<" reportingMark "<<setw(3)<<reportingMark;
cout<<" carNumber "<< setw(8)<< carNumber;
cout<<" kind " << setw(13)<<KIND_ARRAY[kind];
if (loaded==true)
{
cout<<" loaded
true";
}
else if (loaded == false)
{
cout<< " loaded
false";
}
cout<< " destination "<<setw(7)<<destination<<endl;
}
Car ::Car()
{
}
reportingMark = " ";
carNumber =0;
kind = other;
loaded = false;
destination = "NONE";
Car::Car( string r,
{
setUp(r,c,K,l,d);
int c, string K, bool l,
string d)
}
/*Car::Car(const Car & x )
{
setUp(reportingMark,carNumber,Kind,loaded,destination);
}
*/
Car ::~Car()
{
}
/*
operator friend function
tests to see if two objects are equivalent. The two objects are equivalent if
they have the same reportingMark and number (do not look at the kind, loaded,
and destination fields).
*/
void Car::setKind(string const_string)
{
if (const_string == "business")
kind = business;
else if (const_string == "maintenance")
kind = maintenance;
else
if (const_string == "chair")
kind = chair;
else if (const_string == "sleeper")
kind = sleeper;
else if (const_string == "box")
kind = box;
else if (const_string == "tank")
kind = tank;
else if (const_string == "flat")
kind = flat;
else
kind = other;
}
bool operator==(Car carA, Car carB)
{
if(carA.reportingMark==carB.reportingMark&&carA.carNumber==carB.carNumber)
return true;
else
return false;
}
/*