SlideShare a Scribd company logo
1 of 28
#include <iostream>
#include <string>
#include <fstream> // std::ifstream
#include <iomanip>
using namespace std;
/* ********* Class Car *************
********************************* */
class Car
{
private:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public:
Car()
{
reportingMark = "";
carNumber = 0;
kind = "Others";
loaded = 0;
destination = "NONE";
}
//copy constructor
Car(Car &car)
{
reportingMark = car.reportingMark;
carNumber = car.carNumber;
kind = car.kind;
loaded = car.loaded;
destination = car.destination;
}
~Car()
{
}
/* ************* Function output ************
show all results of the program
******************************************* */
void output()
{
string strLoaded;
if (loaded==true)
{
strLoaded = "TRUE";
}
else
{
strLoaded = "FALSE";
}
cout << fixed << left << setw(18) << "Reporting Mark" <<
reportingMark << endl;
cout << fixed << left << setw(18) << "Car Number" <<
carNumber << endl;
cout << fixed << left << setw(18) << "Kind" << kind << endl;
cout << fixed << left << setw(18) << "Loaded" << strLoaded
<< endl;
cout << fixed << left << setw(18) << "Destination" <<
destination << endl;
cout << endl;
}
void setUp(string &r, int &c , string &k, bool &l, string &d)
{
reportingMark =r;
carNumber = c;
kind = k;
loaded = l;
destination = d;
}
friend bool operator== (const Car &c1, const Car &c2);
};
bool operator== (const Car &c1, const Car &c2)
{
return (c1.reportingMark== c2.reportingMark &&
c1.carNumber == c2.carNumber );
}
void input();
//*********** Main **************
int main()
{
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
input ();
Car car1;
car1.setUp(reportingMark,carNumber, kind, loaded,
destination);
cout<<"Contents of car1:"<<endl;
car1.output();
//copy constructor
Car car2=car1;
//call output funciton
cout<<"Contents of car1:"<<endl;
car2.output();
//default constructor
Car car3;
cout<<"Contents of car1:"<<endl;
//call output funciton
car3.output();
if (car1 == car2)
cout<< "car1 is the same car as car2 ";
else
cout<< "car1 is not the same car as car2 ";
if (car2 == car3)
cout<< "car2 is the same car as car3 ";
else
cout<< "car2 is not the same car as car3 ";
return 0;
}
/* ************ Function Input **************
Collect the data
****************************************** */
void input()
{
string type;
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
std::ifstream inputFile ("input.txt");
if (inputFile.is_open())
{
while(inputFile.peek() != EOF)
{
inputFile >> type >> reportingMark >> carNumber >> kind >>
loaded >> destination;
Car temp(string reportingMar k, int carNumber, string kind,
bool loaded);
}
}
else
{
// show message:
std::cout << "Error opening file";
return ;
}
inputFile.close();
/*
int len;
string choice;
cout<< "Enter reportingMark with 5 or less upper case
characters: ";
getline(cin, reportingMark);
len= reportingMark.length();
while (len > 5)
{
cout<< "invalid! Enter reportingMark with 5 or less upper case
characters: ";
getline(cin, reportingMark);
len= reportingMark.length();
}
cout<< "Enter Car Number: ";
cin>> carNumber;
cout << "Enter kind of car: ";
cin>> kind;
do
{
cout<<"Enter the loaded only true or false:";
cin>>choice;
if (choice == "true")
{
loaded = true;
}
else if (choice == "false")
{
loaded = false;
}
else
{
cout<<"error!"<<endl;
}
}
while (choice != "true" && choice != "false");
if(loaded==true)
{
cout<<"Entre the destination: ";
cin.ignore();
getline(cin,destination);
}
if(loaded ==false)
{
destination = "NONE";
}
Car temp(string reportingMark, int carNumber, string kind, bool
loaded);
*/
}
problem 4.2
Copy the following operator= overload member function that
returns the
left hand operator by reference:
// Car operator=
**************************************************
Car & Car::operator=(const Car & carB)
{
reportingMark = carB.reportingMark;
carNumber = carB.carNumber;
kind = carB.kind;
loaded = carB.loaded;
destination = carB.destination;
return * this;
}
Several cars coupled together are referred to as a string of cars.
Create another class called StringOfCars, containing:
* a pointer to an array of Car objects in the heap.
* a static const int ARRAY_MAX_SIZE set to 10.
* an int size containing the current number of Cars in the array.
* a default constructor which gets space for the array in the
heap,
and sets the the size to zero.
* a copy constructor which gets new space in the heap for the
array
and copies all the Car objects.
* a destructor which returns the space to the heap.
* a push function which adds a car to the string of cars.
* a pop function which removes a car from the string of cars,
Last In
First Out (LIFO).
* an output function which prints a heading for each car:
car number n where n is the position in the array starting
from 1
for the first car and then uses the Car output function to print
the
data for each car Or, if the array is empty prints: NO cars
Order of functions in the code:
1. main
2. Car member functions
1. Car constructors in the order
1. default constructor
2. copy constructor
3. other constructors
2. output
3. setUp
4. operator=
3. StringOfCars member functions
1. Car constructors in the order
1. default constructor
2. copy constructor
2. destructor
3. output
4. push
5. pop
4. operator== friend of Car
5. input
Put an eye catcher before the beginning of each function, class,
and the
global area:
// class name function name comment(if any)
*************************************************
Modify the main function to do the following tests:
1. Test the Car operator= function.
Before the call to the input function:
Print: TEST 1
Creat a Car object named car1 with initial values:
reportingMark: SP
carNumber: 34567
kind: box
loaded: true
destination: Salt Lake City
Create a Car object named car2 which is a copy of car1
Print car2
2. Test the StringOfCar push function.
Change the input function to have a parameter that is a
reference to
a StringOfCars.
Add to main:
Create a default StringOfCars object named string1.
Print: TEST 2
Pass string1 to the input function.
Use the same file as in problem D1.
In the input function, just after creating the car, push it on to
string1.
Remove the print of the car in the input function.
Print: STRING 1
In the main function, after using the input function, print
string1.
3. Test the StringOfCars pop function.
Add to main:
Print: TEST 3
Create a car named car3.
Pop one car from string1 into car3.
Print: CAR 3
Print car3.
Print: STRING 1
Then print the contents of string1 again
Solution
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <stdbool.h>
#include <fstream>
#include <iomanip>
using namespace std;
enum Kind {business, maintenance, other, box, tank, flat,
otherFreight, chair, sleeper, otherPassenger};
class Car
{
protected:
//all private variables
string reportingMark;
int carNumber;
Kind kind;
string kindInit;
bool loaded;
string destination;
public:
//default constructor
Car()
{
setUp("",0,"other",false,"NONE");
}
//copy constructor
Car(const Car &obj)
{
setUp(obj.reportingMark,obj.carNumber,obj.kindInit,obj.loaded,
obj.destination);
}
//third constructor
Car(const string reportingMarkX, const int carNumberX,
const string kindX, const bool loadedX, const string
destinationX)
{
setUp(reportingMarkX, carNumberX, kindX, loadedX,
destinationX);
}
//deconstructor
~Car()
{
}
/* **********setUp*********
* takes in string, int, string, bool, and string
* sets teh private values accoridng to the data input
*
*/
void setUp(string reportingMarkOut, int carNumberOut,
string kindInit, bool loadedOut, string destinationOut);
/* **********output**********
* simply outputs the variables in a formatted fashion
*
*/
void output();
virtual void setKind(const string &kindS);
/* **********operator=**********
* overloads the = operator for car objects
*
*/
Car& operator=(const Car & carB);
//friend function
friend bool operator==(const Car&, const Car&);
};
class cs112 : public Car
{
public:
cs112()
{
setUp("",0,"other",false,"NONE");
}
//copy constructor
cs112(const cs112 &obj)
{
setUp(obj.reportingMark,obj.carNumber,obj.kindInit,obj.loaded,
obj.destination);
}
cs112(const string reportingMarkX, const int carNumberX,
const string kindX, const bool loadedX, const string
destinationX)
{
setUp(reportingMarkX, carNumberX, kindX, loadedX,
destinationX);
}
void setKind(const string &kindS);
};
class PassinjirKarr : public Car
{
public:
PassinjirKarr()
{
setUp("",0,"other",false,"NONE");
}
//copy constructor
PassinjirKarr(const PassinjirKarr &obj)
{
setUp(obj.reportingMark,obj.carNumber,obj.kindInit,obj.loaded,
obj.destination);
}
PassinjirKarr(const string reportingMar kX, const int
carNumberX, const string kindX, const bool loadedX, const
string destinationX)
{
setUp(reportingMarkX, carNumberX, kindX, loadedX,
destinationX);
}
void setKind(const string &kindS);
};
class StringOfCars
{
private:
const static int ARRAY_MAX_SIZE = 10;
int size;
Car *AOCPointer[ARRAY_MAX_SIZE];
public:
//default constructor
StringOfCars()
{
size = 0;
for(int i = 0; i < ARRAY_MAX_SIZE; i++)
{
//AOCPointer[i] = new Car[ARRAY_MAX_SIZE];
Car *myPointer = new Car;
AOCPointer[i] = myPointer;
}
}
//copy constructor
StringOfCars(const StringOfCars &thing)
{
int counter = thing.size;
Car *myPointer = new Car;
for(counter = 0; counter >= (thing.size - 1); counter++)
{
AOCPointer[counter] = new
Car[ARRAY_MAX_SIZE];
push(*thing.AOCPointer[counter]);
}
}
//destructor
~StringOfCars()
{
for(int i = 0; i < size; i++)
{
delete AOCPointer[i];
}
}
void push(const Car &carA);
void pop(Car *carx);
void output();
};
//empty constructors
void input(StringOfCars *myCar);
void buildCar(string reportingMarkOut, int carNumberOut,
string kindOut, bool loadedOut, string destinationOut);
void buildcs112(string reportingMarkOut, int carNumberOut,
string kindOut, bool loadedOut, string destinationOut);
void buildPassinjirKarr(string reportingMarkOut, int
carNumberOut, string kindOut, bool loadedOut, string
destinationOut);
const string KIND_ARRAY[10] = {"business",
"maintenance", "other", "box", "tank", "flat",
"otherFreight", "chair", "sleeper", "otherPassenger"};
//bool isEqual(const Car carA, const Car carB);
/* ****************main******************
*
*/
int main()
{
StringOfCars string1;
input(&string1);
return 0;
}
void input(StringOfCars *myCar)
{
ifstream inputFile;
string type;
string order;
string reportingMark;
string carNumberX;
int carNumber;
string kind;
//Kind kind;
string loadedX;
bool loaded;
string destination = "problem";
inputFile.open("inputE.txt");
if(inputFile.fail())
{
fprintf(stderr, "Error in reading file ");
exit(0);
}
while(inputFile.peek() != EOF)
{
string templine;
inputFile >> type;
inputFile >> order;
cout << order << endl;
inputFile >> reportingMark;
inputFile >> carNumber;
inputFile >> kind;
inputFile >> loadedX;
if(loadedX == "true")
{
loaded = 1;
}
else
{
loaded = 0;
}
while(inputFile.peek() == ' ')
inputFile.get();
getline(inputFile,destination);
if(type == "Car")
{
buildCar(reportingMark, carNumber, kind, loaded,
destination);
}
else if(type == "FreightCar")
{
buildcs112(reportingMark, carNumber, kind, loaded,
destination);
}
else if(type == "PassengerCar")
{
buildPassinjirKarr(reportingMark, carNumber, kind,
loaded, destination);
}
}
inputFile.close();
}
//***************************************************
*********************
void StringOfCars::push(const Car &carA)
{
Car *point = new Car(carA);
AOCPointer[size] = point;
size++;
}
void StringOfCars::pop(Car *carx)
{
size--;
Car newCar(*AOCPointer[size]);
*carx = newCar;
}
void StringOfCars::output()
{
int printCheck;
for(printCheck = 0; printCheck < size; printCheck++)
{
cout << "This is car number " << (printCheck+1) <<
endl;
AOCPointer[printCheck]->output();
}
}
//declaired as part of the Car function - see above
void Car::setUp(string reportingMarkOut, int carNumberOut,
string kindOut, bool loadedOut, string destinationOut)
{
reportingMark = reportingMarkOut;
carNumber = carNumberOut;
setKind(kindOut);
loaded = loadedOut;
destination = destinationOut;
}
//declaired as part of the Car function - see above
void Car::output()
{
cout << "The Reporting Mark is: " << reportingMark <<
endl;
cout << "The Car Number is: " << carNumber << endl;
cout << "The Kind is: " << KIND_ARRAY[kind] <<
endl;
cout << "The Loaded status is: ";
if(loaded == true)
{
cout << "true" << endl;
}
else
{
cout << "false" << endl;
}
cout << "The destination is: " << destination << endl <<
endl;
}
void Car::setKind(const string &kindS)
{
if(kindS == "business")
{
kind = business;
}
else if(kindS == "maintenance")
{
kind = maintenance;
}
else
{
kind = other;
}
}
void cs112::setKind(const string &kindS)
{
if(kindS == "box")
{
kind = box;
}
else if(kindS == "tank")
{
kind = tank;
}
else if(kindS == "flat")
{
kind = flat;
}
else
{
kind = otherFreight;
}
}
void PassinjirKarr::setKind(const string &kindS)
{
if(kindS == "chair")
{
kind = chair;
}
else if(kindS == "sleeper")
{
kind = sleeper;
}
else
{
kind = otherPassenger;
}
}
/* **********operator==**********
*
* friend function of Car which overloads == to check if two
cars are equal only using their reportingMark and their
carNumber
*
*/
bool operator==(const Car &a, const Car &b)
{
if((a.reportingMark == b.reportingMark) && (a.carNumber
== b.carNumber))
{
return true;
}
else
{
return false;
}
}
/* ********** Car operator= **********
*/
Car & Car::operator=(const Car & carB)
{
setUp(carB.reportingMark, carB.carNumber, carB.kindInit,
carB.loaded, carB.destination);
return * this;
}
void buildCar(string reportingMarkOut, int carNumberOut,
string kindOut, bool loadedOut, string destinationOut)
{
Car CarThing(reportingMarkOut,carNumberOut, kindOut,
loadedOut,destinationOut);
CarThing.output();
}
void buildPassinjirKarr(string reportingMarkOut, int
carNumberOut, string kindOut, bool loadedOut, string
destinationOut)
{
PassinjirKarr
passengerCarThing(reportingMarkOut,carNumberOut, kindOut,
loadedOut,destinationOut);
passengerCarThing.output();
}
void buildcs112(string reportingMarkOut, int carNumberOut,
string kindOut, bool loadedOut, string destinationOut)
{
cs112 FreightCarThing(reportingMarkOut,carNumberOut,
kindOut, loadedOut,destinationOut);
FreightCarThing.output();
}
inputE.txt
Car car1 CN 819481 maintenance false NONE
Car car2 SLSF 46871 business true Memphis
Car car3 AOK 156 tender true McAlester
FreightCar car4 MKT 123456 tank false Fort Worth
FreightCar car5 MP 98765 box true Saint Louis
FreightCar car6 SP 567890 flat true Chicago
FreightCar car7 GMO 7878 hopper true Mobile
PassengerCar car8 KCS 7893 chair true Kansas City
PassengerCar car9 PAPX 145 sleeper true Tucson
PassengerCar car10 GN 744 combine false NONE
#include iostream #include string #include fstream  std.docx

More Related Content

Similar to #include iostream #include string #include fstream std.docx

Goal The goal of this assignment is to help students understand the.pdf
Goal The goal of this assignment is to help students understand the.pdfGoal The goal of this assignment is to help students understand the.pdf
Goal The goal of this assignment is to help students understand the.pdfarsmobiles
 
FedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdf
FedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdfFedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdf
FedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdfalukkasprince
 
C#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfC#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfajantha11
 
#includeiostream #includecmath #includestringusing na.pdf
 #includeiostream #includecmath #includestringusing na.pdf #includeiostream #includecmath #includestringusing na.pdf
#includeiostream #includecmath #includestringusing na.pdfanuradhaartjwellery
 
publicclass VehicleParser {publicstatic Vehicle parseStringToVehic.pdf
publicclass VehicleParser {publicstatic Vehicle parseStringToVehic.pdfpublicclass VehicleParser {publicstatic Vehicle parseStringToVehic.pdf
publicclass VehicleParser {publicstatic Vehicle parseStringToVehic.pdfANGELMARKETINGJAIPUR
 
C programming session 01
C programming session 01C programming session 01
C programming session 01Dushmanta Nath
 
public class Passenger {    public static enum Section {        .pdf
public class Passenger {    public static enum Section {        .pdfpublic class Passenger {    public static enum Section {        .pdf
public class Passenger {    public static enum Section {        .pdfannesmkt
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfjyothimuppasani1
 
COMP360 Assembler Write an assembler that reads the source code of an.pdf
COMP360 Assembler Write an assembler that reads the source code of an.pdfCOMP360 Assembler Write an assembler that reads the source code of an.pdf
COMP360 Assembler Write an assembler that reads the source code of an.pdffazalenterprises
 
ONLY EDIT CapacityOptimizer.java, Simulator.java, and TriangularD.pdf
ONLY EDIT  CapacityOptimizer.java, Simulator.java, and TriangularD.pdfONLY EDIT  CapacityOptimizer.java, Simulator.java, and TriangularD.pdf
ONLY EDIT CapacityOptimizer.java, Simulator.java, and TriangularD.pdfvinodagrawal6699
 
Parking Ticket SimulatorFor this assignment you will design a se.docx
Parking Ticket SimulatorFor this assignment you will design a se.docxParking Ticket SimulatorFor this assignment you will design a se.docx
Parking Ticket SimulatorFor this assignment you will design a se.docxdanhaley45372
 
The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212Mahmoud Samir Fayed
 
public static void main(String[], args) throws FileNotFoundExcepti.pdf
public static void main(String[], args) throws FileNotFoundExcepti.pdfpublic static void main(String[], args) throws FileNotFoundExcepti.pdf
public static void main(String[], args) throws FileNotFoundExcepti.pdffms12345
 
In this assignment you will practice creating classes and enumeratio.pdf
In this assignment you will practice creating classes and enumeratio.pdfIn this assignment you will practice creating classes and enumeratio.pdf
In this assignment you will practice creating classes and enumeratio.pdfivylinvaydak64229
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introductionraghukatagall2
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaonyash production
 
C++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+OperatorsC++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+OperatorsMohammad Shaker
 
Automobile.javapublic class Automobile {    Declaring instan.pdf
Automobile.javapublic class Automobile {    Declaring instan.pdfAutomobile.javapublic class Automobile {    Declaring instan.pdf
Automobile.javapublic class Automobile {    Declaring instan.pdfanitasahani11
 

Similar to #include iostream #include string #include fstream std.docx (20)

Goal The goal of this assignment is to help students understand the.pdf
Goal The goal of this assignment is to help students understand the.pdfGoal The goal of this assignment is to help students understand the.pdf
Goal The goal of this assignment is to help students understand the.pdf
 
FedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdf
FedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdfFedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdf
FedExPlanes7.txt1 medical 111 Boeing767 120000 London 3 packages.pdf
 
C#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdfC#i need help creating the instance of stream reader to read from .pdf
C#i need help creating the instance of stream reader to read from .pdf
 
#includeiostream #includecmath #includestringusing na.pdf
 #includeiostream #includecmath #includestringusing na.pdf #includeiostream #includecmath #includestringusing na.pdf
#includeiostream #includecmath #includestringusing na.pdf
 
publicclass VehicleParser {publicstatic Vehicle parseStringToVehic.pdf
publicclass VehicleParser {publicstatic Vehicle parseStringToVehic.pdfpublicclass VehicleParser {publicstatic Vehicle parseStringToVehic.pdf
publicclass VehicleParser {publicstatic Vehicle parseStringToVehic.pdf
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
public class Passenger {    public static enum Section {        .pdf
public class Passenger {    public static enum Section {        .pdfpublic class Passenger {    public static enum Section {        .pdf
public class Passenger {    public static enum Section {        .pdf
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
 
COMP360 Assembler Write an assembler that reads the source code of an.pdf
COMP360 Assembler Write an assembler that reads the source code of an.pdfCOMP360 Assembler Write an assembler that reads the source code of an.pdf
COMP360 Assembler Write an assembler that reads the source code of an.pdf
 
ONLY EDIT CapacityOptimizer.java, Simulator.java, and TriangularD.pdf
ONLY EDIT  CapacityOptimizer.java, Simulator.java, and TriangularD.pdfONLY EDIT  CapacityOptimizer.java, Simulator.java, and TriangularD.pdf
ONLY EDIT CapacityOptimizer.java, Simulator.java, and TriangularD.pdf
 
Parking Ticket SimulatorFor this assignment you will design a se.docx
Parking Ticket SimulatorFor this assignment you will design a se.docxParking Ticket SimulatorFor this assignment you will design a se.docx
Parking Ticket SimulatorFor this assignment you will design a se.docx
 
The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212The Ring programming language version 1.10 book - Part 97 of 212
The Ring programming language version 1.10 book - Part 97 of 212
 
public static void main(String[], args) throws FileNotFoundExcepti.pdf
public static void main(String[], args) throws FileNotFoundExcepti.pdfpublic static void main(String[], args) throws FileNotFoundExcepti.pdf
public static void main(String[], args) throws FileNotFoundExcepti.pdf
 
In this assignment you will practice creating classes and enumeratio.pdf
In this assignment you will practice creating classes and enumeratio.pdfIn this assignment you will practice creating classes and enumeratio.pdf
In this assignment you will practice creating classes and enumeratio.pdf
 
JavaScript Core
JavaScript CoreJavaScript Core
JavaScript Core
 
C Programming ppt for beginners . Introduction
C Programming ppt for beginners . IntroductionC Programming ppt for beginners . Introduction
C Programming ppt for beginners . Introduction
 
Oop in java script
Oop in java scriptOop in java script
Oop in java script
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
C++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+OperatorsC++ L02-Conversion+enum+Operators
C++ L02-Conversion+enum+Operators
 
Automobile.javapublic class Automobile {    Declaring instan.pdf
Automobile.javapublic class Automobile {    Declaring instan.pdfAutomobile.javapublic class Automobile {    Declaring instan.pdf
Automobile.javapublic class Automobile {    Declaring instan.pdf
 

More from ajoy21

Please complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docxPlease complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docxajoy21
 
Please cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docxPlease cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docxajoy21
 
Please choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docxPlease choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docxajoy21
 
Please check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docxPlease check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docxajoy21
 
Please answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docxPlease answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docxajoy21
 
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docxPlease attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docxajoy21
 
Please answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docxPlease answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docxajoy21
 
Please answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docxPlease answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docxajoy21
 
Please answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docxPlease answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docxajoy21
 
Please answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docxPlease answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docxajoy21
 
Please answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docxPlease answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docxajoy21
 
Please answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docxPlease answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docxajoy21
 
Please answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docxPlease answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docxajoy21
 
Please answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docxPlease answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docxajoy21
 
Please answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docxPlease answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docxajoy21
 
Please answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docxPlease answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docxajoy21
 
Please answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docxPlease answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docxajoy21
 
Please answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docxPlease answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docxajoy21
 
Please answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docxPlease answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docxajoy21
 
Please answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxPlease answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxajoy21
 

More from ajoy21 (20)

Please complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docxPlease complete the assignment listed below.Define and explain, us.docx
Please complete the assignment listed below.Define and explain, us.docx
 
Please cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docxPlease cite sources for each question. Do not use the same sources f.docx
Please cite sources for each question. Do not use the same sources f.docx
 
Please choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docxPlease choose one of the following questions to answer for this week.docx
Please choose one of the following questions to answer for this week.docx
 
Please check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docxPlease check the attachment for my paper.Please add citations to a.docx
Please check the attachment for my paper.Please add citations to a.docx
 
Please answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docxPlease answer to this discussion post. No less than 150 words. Refer.docx
Please answer to this discussion post. No less than 150 words. Refer.docx
 
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docxPlease attach Non-nursing theorist summaries.JigsawExecutive .docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
 
Please answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docxPlease answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the question .There is no work count. PLEASE NUMBER .docx
 
Please answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docxPlease answer the following questions. Please cite your references..docx
Please answer the following questions. Please cite your references..docx
 
Please answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docxPlease answer the following questions.1.      1.  Are you or.docx
Please answer the following questions.1.      1.  Are you or.docx
 
Please answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docxPlease answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question with 200-300 words.Q. Discu.docx
 
Please answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docxPlease answer the following question Why do you think the US ha.docx
Please answer the following question Why do you think the US ha.docx
 
Please answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docxPlease answer the following questions. Define tunneling in the V.docx
Please answer the following questions. Define tunneling in the V.docx
 
Please answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docxPlease answer the following questions1. How can you stimulate the.docx
Please answer the following questions1. How can you stimulate the.docx
 
Please answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docxPlease answer the following questions very deeply and presicely .docx
Please answer the following questions very deeply and presicely .docx
 
Please answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docxPlease answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in an informal 1 ½ - 2-page es.docx
 
Please answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docxPlease answer the following questions in a response of 150 to 200 wo.docx
Please answer the following questions in a response of 150 to 200 wo.docx
 
Please answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docxPlease answer these questions regarding the (TILA) Truth in Lending .docx
Please answer these questions regarding the (TILA) Truth in Lending .docx
 
Please answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docxPlease answer the following question pertaining to psychology. Inc.docx
Please answer the following question pertaining to psychology. Inc.docx
 
Please answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docxPlease answer the following questions in a response of 250 to 300 .docx
Please answer the following questions in a response of 250 to 300 .docx
 
Please answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docxPlease answer the three questions completly. I have attached the que.docx
Please answer the three questions completly. I have attached the que.docx
 

Recently uploaded

How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 

Recently uploaded (20)

How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 

#include iostream #include string #include fstream std.docx

  • 1. #include <iostream> #include <string> #include <fstream> // std::ifstream #include <iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } //copy constructor Car(Car &car) { reportingMark = car.reportingMark; carNumber = car.carNumber; kind = car.kind; loaded = car.loaded; destination = car.destination; } ~Car() {
  • 2. } /* ************* Function output ************ show all results of the program ******************************************* */ void output() { string strLoaded; if (loaded==true) { strLoaded = "TRUE"; } else { strLoaded = "FALSE"; } cout << fixed << left << setw(18) << "Reporting Mark" << reportingMark << endl; cout << fixed << left << setw(18) << "Car Number" << carNumber << endl; cout << fixed << left << setw(18) << "Kind" << kind << endl; cout << fixed << left << setw(18) << "Loaded" << strLoaded << endl; cout << fixed << left << setw(18) << "Destination" << destination << endl; cout << endl; } void setUp(string &r, int &c , string &k, bool &l, string &d) { reportingMark =r; carNumber = c; kind = k; loaded = l; destination = d; }
  • 3. friend bool operator== (const Car &c1, const Car &c2); }; bool operator== (const Car &c1, const Car &c2) { return (c1.reportingMark== c2.reportingMark && c1.carNumber == c2.carNumber ); } void input(); //*********** Main ************** int main() { string reportingMark; int carNumber; string kind; bool loaded; string destination; input (); Car car1; car1.setUp(reportingMark,carNumber, kind, loaded, destination); cout<<"Contents of car1:"<<endl; car1.output(); //copy constructor Car car2=car1; //call output funciton cout<<"Contents of car1:"<<endl; car2.output(); //default constructor Car car3; cout<<"Contents of car1:"<<endl; //call output funciton car3.output(); if (car1 == car2) cout<< "car1 is the same car as car2 "; else cout<< "car1 is not the same car as car2 ";
  • 4. if (car2 == car3) cout<< "car2 is the same car as car3 "; else cout<< "car2 is not the same car as car3 "; return 0; } /* ************ Function Input ************** Collect the data ****************************************** */ void input() { string type; string reportingMark; int carNumber; string kind; bool loaded; string destination; std::ifstream inputFile ("input.txt"); if (inputFile.is_open()) { while(inputFile.peek() != EOF) { inputFile >> type >> reportingMark >> carNumber >> kind >> loaded >> destination; Car temp(string reportingMar k, int carNumber, string kind, bool loaded); } } else { // show message: std::cout << "Error opening file"; return ;
  • 5. } inputFile.close(); /* int len; string choice; cout<< "Enter reportingMark with 5 or less upper case characters: "; getline(cin, reportingMark); len= reportingMark.length(); while (len > 5) { cout<< "invalid! Enter reportingMark with 5 or less upper case characters: "; getline(cin, reportingMark); len= reportingMark.length(); } cout<< "Enter Car Number: "; cin>> carNumber; cout << "Enter kind of car: "; cin>> kind; do { cout<<"Enter the loaded only true or false:"; cin>>choice; if (choice == "true") { loaded = true; } else if (choice == "false") { loaded = false; } else { cout<<"error!"<<endl;
  • 6. } } while (choice != "true" && choice != "false"); if(loaded==true) { cout<<"Entre the destination: "; cin.ignore(); getline(cin,destination); } if(loaded ==false) { destination = "NONE"; } Car temp(string reportingMark, int carNumber, string kind, bool loaded); */ } problem 4.2 Copy the following operator= overload member function that returns the left hand operator by reference: // Car operator= ************************************************** Car & Car::operator=(const Car & carB) { reportingMark = carB.reportingMark; carNumber = carB.carNumber; kind = carB.kind; loaded = carB.loaded; destination = carB.destination; return * this; } Several cars coupled together are referred to as a string of cars. Create another class called StringOfCars, containing: * a pointer to an array of Car objects in the heap. * a static const int ARRAY_MAX_SIZE set to 10.
  • 7. * an int size containing the current number of Cars in the array. * a default constructor which gets space for the array in the heap, and sets the the size to zero. * a copy constructor which gets new space in the heap for the array and copies all the Car objects. * a destructor which returns the space to the heap. * a push function which adds a car to the string of cars. * a pop function which removes a car from the string of cars, Last In First Out (LIFO). * an output function which prints a heading for each car: car number n where n is the position in the array starting from 1 for the first car and then uses the Car output function to print the data for each car Or, if the array is empty prints: NO cars Order of functions in the code: 1. main 2. Car member functions 1. Car constructors in the order 1. default constructor 2. copy constructor 3. other constructors 2. output 3. setUp 4. operator= 3. StringOfCars member functions 1. Car constructors in the order 1. default constructor 2. copy constructor 2. destructor 3. output 4. push 5. pop
  • 8. 4. operator== friend of Car 5. input Put an eye catcher before the beginning of each function, class, and the global area: // class name function name comment(if any) ************************************************* Modify the main function to do the following tests: 1. Test the Car operator= function. Before the call to the input function: Print: TEST 1 Creat a Car object named car1 with initial values: reportingMark: SP carNumber: 34567 kind: box loaded: true destination: Salt Lake City Create a Car object named car2 which is a copy of car1 Print car2 2. Test the StringOfCar push function. Change the input function to have a parameter that is a reference to a StringOfCars. Add to main: Create a default StringOfCars object named string1. Print: TEST 2 Pass string1 to the input function. Use the same file as in problem D1. In the input function, just after creating the car, push it on to string1. Remove the print of the car in the input function. Print: STRING 1 In the main function, after using the input function, print string1. 3. Test the StringOfCars pop function. Add to main:
  • 9. Print: TEST 3 Create a car named car3. Pop one car from string1 into car3. Print: CAR 3 Print car3. Print: STRING 1 Then print the contents of string1 again Solution #include <iostream> #include <cstring> #include <stdlib.h> #include <string> #include <cstring> #include <stdbool.h> #include <fstream> #include <iomanip> using namespace std; enum Kind {business, maintenance, other, box, tank, flat, otherFreight, chair, sleeper, otherPassenger}; class Car
  • 10. { protected: //all private variables string reportingMark; int carNumber; Kind kind; string kindInit; bool loaded; string destination; public: //default constructor Car() { setUp("",0,"other",false,"NONE"); } //copy constructor Car(const Car &obj) { setUp(obj.reportingMark,obj.carNumber,obj.kindInit,obj.loaded, obj.destination); } //third constructor Car(const string reportingMarkX, const int carNumberX,
  • 11. const string kindX, const bool loadedX, const string destinationX) { setUp(reportingMarkX, carNumberX, kindX, loadedX, destinationX); } //deconstructor ~Car() { } /* **********setUp********* * takes in string, int, string, bool, and string * sets teh private values accoridng to the data input * */ void setUp(string reportingMarkOut, int carNumberOut, string kindInit, bool loadedOut, string destinationOut); /* **********output********** * simply outputs the variables in a formatted fashion * */ void output();
  • 12. virtual void setKind(const string &kindS); /* **********operator=********** * overloads the = operator for car objects * */ Car& operator=(const Car & carB); //friend function friend bool operator==(const Car&, const Car&); }; class cs112 : public Car { public: cs112() { setUp("",0,"other",false,"NONE"); } //copy constructor
  • 13. cs112(const cs112 &obj) { setUp(obj.reportingMark,obj.carNumber,obj.kindInit,obj.loaded, obj.destination); } cs112(const string reportingMarkX, const int carNumberX, const string kindX, const bool loadedX, const string destinationX) { setUp(reportingMarkX, carNumberX, kindX, loadedX, destinationX); } void setKind(const string &kindS); }; class PassinjirKarr : public Car { public: PassinjirKarr() {
  • 14. setUp("",0,"other",false,"NONE"); } //copy constructor PassinjirKarr(const PassinjirKarr &obj) { setUp(obj.reportingMark,obj.carNumber,obj.kindInit,obj.loaded, obj.destination); } PassinjirKarr(const string reportingMar kX, const int carNumberX, const string kindX, const bool loadedX, const string destinationX) { setUp(reportingMarkX, carNumberX, kindX, loadedX, destinationX); } void setKind(const string &kindS); }; class StringOfCars { private: const static int ARRAY_MAX_SIZE = 10;
  • 15. int size; Car *AOCPointer[ARRAY_MAX_SIZE]; public: //default constructor StringOfCars() { size = 0; for(int i = 0; i < ARRAY_MAX_SIZE; i++) { //AOCPointer[i] = new Car[ARRAY_MAX_SIZE]; Car *myPointer = new Car; AOCPointer[i] = myPointer; } } //copy constructor StringOfCars(const StringOfCars &thing) {
  • 16. int counter = thing.size; Car *myPointer = new Car; for(counter = 0; counter >= (thing.size - 1); counter++) { AOCPointer[counter] = new Car[ARRAY_MAX_SIZE]; push(*thing.AOCPointer[counter]); } } //destructor ~StringOfCars() { for(int i = 0; i < size; i++) { delete AOCPointer[i]; } }
  • 17. void push(const Car &carA); void pop(Car *carx); void output(); }; //empty constructors void input(StringOfCars *myCar); void buildCar(string reportingMarkOut, int carNumberOut, string kindOut, bool loadedOut, string destinationOut); void buildcs112(string reportingMarkOut, int carNumberOut, string kindOut, bool loadedOut, string destinationOut); void buildPassinjirKarr(string reportingMarkOut, int carNumberOut, string kindOut, bool loadedOut, string destinationOut); const string KIND_ARRAY[10] = {"business", "maintenance", "other", "box", "tank", "flat", "otherFreight", "chair", "sleeper", "otherPassenger"}; //bool isEqual(const Car carA, const Car carB); /* ****************main****************** * */ int main() { StringOfCars string1; input(&string1); return 0;
  • 18. } void input(StringOfCars *myCar) { ifstream inputFile; string type; string order; string reportingMark; string carNumberX; int carNumber; string kind; //Kind kind; string loadedX; bool loaded; string destination = "problem"; inputFile.open("inputE.txt"); if(inputFile.fail()) { fprintf(stderr, "Error in reading file "); exit(0); } while(inputFile.peek() != EOF) {
  • 19. string templine; inputFile >> type; inputFile >> order; cout << order << endl; inputFile >> reportingMark; inputFile >> carNumber; inputFile >> kind; inputFile >> loadedX; if(loadedX == "true") { loaded = 1; } else { loaded = 0; } while(inputFile.peek() == ' ') inputFile.get(); getline(inputFile,destination); if(type == "Car")
  • 20. { buildCar(reportingMark, carNumber, kind, loaded, destination); } else if(type == "FreightCar") { buildcs112(reportingMark, carNumber, kind, loaded, destination); } else if(type == "PassengerCar") { buildPassinjirKarr(reportingMark, carNumber, kind, loaded, destination); } } inputFile.close(); } //*************************************************** ********************* void StringOfCars::push(const Car &carA) { Car *point = new Car(carA);
  • 21. AOCPointer[size] = point; size++; } void StringOfCars::pop(Car *carx) { size--; Car newCar(*AOCPointer[size]); *carx = newCar; } void StringOfCars::output() { int printCheck; for(printCheck = 0; printCheck < size; printCheck++) { cout << "This is car number " << (printCheck+1) << endl; AOCPointer[printCheck]->output(); } } //declaired as part of the Car function - see above void Car::setUp(string reportingMarkOut, int carNumberOut,
  • 22. string kindOut, bool loadedOut, string destinationOut) { reportingMark = reportingMarkOut; carNumber = carNumberOut; setKind(kindOut); loaded = loadedOut; destination = destinationOut; } //declaired as part of the Car function - see above void Car::output() { cout << "The Reporting Mark is: " << reportingMark << endl; cout << "The Car Number is: " << carNumber << endl; cout << "The Kind is: " << KIND_ARRAY[kind] << endl; cout << "The Loaded status is: "; if(loaded == true) { cout << "true" << endl; } else { cout << "false" << endl; }
  • 23. cout << "The destination is: " << destination << endl << endl; } void Car::setKind(const string &kindS) { if(kindS == "business") { kind = business; } else if(kindS == "maintenance") { kind = maintenance; } else { kind = other; } } void cs112::setKind(const string &kindS) { if(kindS == "box") { kind = box; }
  • 24. else if(kindS == "tank") { kind = tank; } else if(kindS == "flat") { kind = flat; } else { kind = otherFreight; } } void PassinjirKarr::setKind(const string &kindS) { if(kindS == "chair") { kind = chair; } else if(kindS == "sleeper") { kind = sleeper; } else
  • 25. { kind = otherPassenger; } } /* **********operator==********** * * friend function of Car which overloads == to check if two cars are equal only using their reportingMark and their carNumber * */ bool operator==(const Car &a, const Car &b) { if((a.reportingMark == b.reportingMark) && (a.carNumber == b.carNumber)) { return true; } else { return false; } } /* ********** Car operator= **********
  • 26. */ Car & Car::operator=(const Car & carB) { setUp(carB.reportingMark, carB.carNumber, carB.kindInit, carB.loaded, carB.destination); return * this; } void buildCar(string reportingMarkOut, int carNumberOut, string kindOut, bool loadedOut, string destinationOut) { Car CarThing(reportingMarkOut,carNumberOut, kindOut, loadedOut,destinationOut); CarThing.output(); } void buildPassinjirKarr(string reportingMarkOut, int carNumberOut, string kindOut, bool loadedOut, string destinationOut) { PassinjirKarr passengerCarThing(reportingMarkOut,carNumberOut, kindOut, loadedOut,destinationOut);
  • 27. passengerCarThing.output(); } void buildcs112(string reportingMarkOut, int carNumberOut, string kindOut, bool loadedOut, string destinationOut) { cs112 FreightCarThing(reportingMarkOut,carNumberOut, kindOut, loadedOut,destinationOut); FreightCarThing.output(); } inputE.txt Car car1 CN 819481 maintenance false NONE Car car2 SLSF 46871 business true Memphis Car car3 AOK 156 tender true McAlester FreightCar car4 MKT 123456 tank false Fort Worth FreightCar car5 MP 98765 box true Saint Louis FreightCar car6 SP 567890 flat true Chicago FreightCar car7 GMO 7878 hopper true Mobile PassengerCar car8 KCS 7893 chair true Kansas City PassengerCar car9 PAPX 145 sleeper true Tucson PassengerCar car10 GN 744 combine false NONE