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

Copy the solution from problem Keep the same order of the functions as in proble

ID: 3849509 • Letter: C

Question

Copy the solution from problem

Keep the same order of the functions as in problem.
We will keep the public interface to the StringOfCars.
We will change the implementation of the StringOfCars.
So keep all the public function prototypes in the StringOfCars.
Discard all the private data and the implementation of the member functions from the StringOfCars class; we will rebuild all of these.

Do not change anything outside the StringOfCars class.

In the StringOfCars class implementation:

Replace the private data with an STL vector of Car objects, and nothing else.

Change the default constructor so it does nothing.

Change the copy constructor so it uses the assignment operator to assign the old vector object to the vector object being constructed.

The destructor should do nothing.

Change the output function so it uses the vector size function and prints the Car objects from the vector.

Change the push function so it uses the vector push_back function to add a Car to the vector.

Change the pop function.
Use the vector back function to make a copy of the last Car object.
Use the vector pop_back function to remove the last Car object.
Put the Car object removed into the reference parameter.

Use the same tests as in problem.

#include <iostream>

#include <string>

#include <iomanip>

#include <fstream>

#include <cstdlib>

using namespace std;

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

global functions

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

void input();

class Car

{

string reportingMark;

int carNumber;

string kind;

bool loaded;

string destination;

public:

/*****************Default Constructor ************/

Car()

{

reportingMark = "";

carNumber = 0;

kind = "other";

loaded = false;

destination = "NONE";

}

/*****************Copy Constructor ************/

Car (const Car &sourceObject)

{

setup(sourceObject.reportingMark,

sourceObject.carNumber,

sourceObject.kind,

sourceObject.loaded,

sourceObject.destination);

}

Car (const string reportingMark,

   const int carNumber,

   const string kind,

   const bool loaded,

   const string destination)

{

setup(reportingMark, carNumber, kind, loaded, destination);

}

/***************** Destructor ********************/

~Car() { }

friend bool operator==(Car Car1,Car Car2);

void output();

void setup (string reportingMark,

int carNumber,

string kind,

bool loaded,

string destination);

Car & operator=(const Car & carB);

};

class StringOfCars

{

static const int ARRAY_SIZE = 10;

Car * ptr;

int carCount;

public:

StringOfCars();

StringOfCars(const StringOfCars & obj);

~StringOfCars();

void push(Car & car1);

void pop(Car & car2);

void output() const;

};

void input(StringOfCars &string1);

/***************** Main function ********************/

int main()

{

cout<<"TEST 1"<<endl;

Car car1("SP",34567,"business",1,"Salt Lake City");

Car car2 = car1;

car2.output();

cout << endl;

cout<<"TEST 2"<<endl;

StringOfCars string1;

input(string1);

cout<<"STRING 1"<<endl<<endl;

string1.output();

cout<<"TEST 3"<<endl;

Car car3;

string1.pop(car3);

cout<<"CAR 3"<<endl<<endl;

car3.output();

cout<<endl<<endl<<"STRING 1"<<endl;

string1.output();

return 0;

}

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

Car class functions

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

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

Prints the member data in a neat format

*/

void Car :: output()

{

cout <<left<< setw(20)<< "reportingMark"<<reportingMark<<endl;

cout <<left<< setw(20)<< "carNumber"<<carNumber<<endl;

cout <<left<< setw(20)<< "kind"<<kind<<endl;

cout <<left<< setw(20)<< "loaded"<< boolalpha << loaded<<endl;

cout <<left<< setw(20)<< "destination"<<destination<<endl;

}

/********************* Car::setup ********************

Takes the five parameters by value: reportingMark, carNumber, kind, loaded, and destination

Puts the data in the object

*/

void Car :: setup(string tempReportingMark, int tempCarNumber, string tempKind, bool tempLoaded, string tempDestination)

{

reportingMark = tempReportingMark;

carNumber = tempCarNumber;

kind = tempKind;

loaded = tempLoaded;

destination = tempDestination;

}

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

sets the values in the left hand object from the right hand object

*/

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

{

setup(carB.reportingMark, carB.carNumber, carB.kind, carB.loaded, carB.destination);

return * this;

}

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

StringOfCars class functions

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

/********************* Constructor ********************/

StringOfCars::StringOfCars()

{

ptr = new Car[ARRAY_SIZE];

carCount = 0;

}

StringOfCars::StringOfCars(const StringOfCars &obj)

{

delete [] ptr;

ptr = new Car[ARRAY_SIZE];

carCount = obj.carCount;

for (int i = 0; i < carCount; i ++)

{

ptr[i] = obj.ptr[i];

}

}

/********************* Destructor ********************/

StringOfCars::~StringOfCars()

{

delete [] ptr;

ptr = 0;

carCount = 0;

}

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

void StringOfCars::output() const

{

if (carCount <= 0)

{

cout <<"NO cars!"<<endl;

}

else

{

for (int i = 0; i < carCount; i ++)

{

cout<<"Car Number "<<i + 1<<endl;

ptr[i].output();

cout<<endl;

}

}

}

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

void StringOfCars::push(Car & car1)

{

if (carCount == ARRAY_SIZE)

{

cout<<"Not valid! It has reached the Max carCount.";

}

else

{

ptr[carCount] = car1;

carCount ++;

}

}

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

void StringOfCars::pop(Car & car2)

{

carCount --;

car2 = ptr[carCount];

}

/********************* operator *********************

Reads the reportingMark, carNumber, kind, loaded, and destination from the user

*/

bool operator==(Car Car1, Car Car2)

{

return (Car1.reportingMark == Car2.reportingMark && Car1.carNumber == Car2.carNumber);

}

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

Create a friend function for the function operator== which tests to see if two objects are equivalent.

The two objects are equivalent if they have the same reportingMark and carNumber

*/

void input(StringOfCars & string1)

{

   string reportingMark;

   int carNumber;

   string kind;

   string load;

   bool loaded;

   string destination;

   string type;

ifstream inputFile;

inputFile.open("file.txt");

if(!inputFile)

{

cout<<"Error! Can not find the file!";

       exit(100);

}

   while(inputFile.peek()!= EOF)

{

inputFile >> type >> reportingMark>> carNumber>> kind >> load;

if (load == "true")

loaded = true;

else

loaded = false;

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

inputFile.get();

inputFile.clear();

getline(inputFile,destination);

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

string1.push(temp);

}

inputFile.close();

}

/** Execution results

TEST 1

reportingMark SP

carNumber 34567

kind business

loaded true

destination Salt Lake City

TEST 2

STRING 1

Car Number 1

reportingMark CN

carNumber 819481

kind maintenance

loaded false

destination NONE

Car Number 2

reportingMark SLSF

carNumber 46871

kind business

loaded true

destination Memphis

Car Number 3

reportingMark AOK

carNumber 156

kind tender

loaded true

destination McAlester

TEST 3

CAR 3

reportingMark AOK

carNumber 156

kind tender

loaded true

destination McAlester

STRING 1

Car Number 1

reportingMark CN

carNumber 819481

kind maintenance

loaded false

destination NONE

Car Number 2

reportingMark SLSF

carNumber 46871

kind business

loaded true

destination Memphis

Process returned 0 (0x0) execution time : 0.094 s

Press any key to continue.

*/

Explanation / Answer

Here is the modified code according to the question. Output shown below. Please don't forget to rate teh answer if it helped. Thank you very much.

#include <iostream>

#include <string>

#include <iomanip>

#include <fstream>

#include <cstdlib>

#include <vector>

using namespace std;

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

global functions

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

void input();

class Car

{

string reportingMark;

int carNumber;

string kind;

bool loaded;

string destination;

  

public:

/*****************Default Constructor ************/

Car()

{

reportingMark = "";

carNumber = 0;

kind = "other";

loaded = false;

destination = "NONE";

}

/*****************Copy Constructor ************/

Car (const Car &sourceObject)

{

setup(sourceObject.reportingMark,

sourceObject.carNumber,

sourceObject.kind,

sourceObject.loaded,

sourceObject.destination);

}

Car (const string reportingMark,

   const int carNumber,

   const string kind,

   const bool loaded,

   const string destination)

{

setup(reportingMark, carNumber, kind, loaded, destination);

}

/***************** Destructor ********************/

~Car() { }

friend bool operator==(Car Car1,Car Car2);

void output();

void setup (string reportingMark,

int carNumber,

string kind,

bool loaded,

string destination);

Car & operator=(const Car & carB);

  

};

class StringOfCars

{

vector<Car> cars;

public:

StringOfCars();

StringOfCars(const StringOfCars & obj);

~StringOfCars();

void push(Car & car1);

void pop(Car & car2);

void output() ;

};

void input(StringOfCars &string1);

/***************** Main function ********************/

int main()

{

cout<<"TEST 1"<<endl;

Car car1("SP",34567,"business",1,"Salt Lake City");

Car car2 = car1;

car2.output();

cout << endl;

  

cout<<"TEST 2"<<endl;

StringOfCars string1;

input(string1);

cout<<"STRING 1"<<endl<<endl;

string1.output();

  

cout<<"TEST 3"<<endl;

Car car3;

string1.pop(car3);

cout<<"CAR 3"<<endl<<endl;

car3.output();

cout<<endl<<endl<<"STRING 1"<<endl;

string1.output();

  

return 0;

}

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

Car class functions

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

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

Prints the member data in a neat format

*/

void Car :: output()

{

cout <<left<< setw(20)<< "reportingMark"<<reportingMark<<endl;

cout <<left<< setw(20)<< "carNumber"<<carNumber<<endl;

cout <<left<< setw(20)<< "kind"<<kind<<endl;

cout <<left<< setw(20)<< "loaded"<< boolalpha << loaded<<endl;

cout <<left<< setw(20)<< "destination"<<destination<<endl;

}

/********************* Car::setup ********************

Takes the five parameters by value: reportingMark, carNumber, kind, loaded, and destination

Puts the data in the object

*/

void Car :: setup(string tempReportingMark, int tempCarNumber, string tempKind, bool tempLoaded, string tempDestination)

{

reportingMark = tempReportingMark;

carNumber = tempCarNumber;

kind = tempKind;

loaded = tempLoaded;

destination = tempDestination;

}

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

sets the values in the left hand object from the right hand object

*/

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

{

setup(carB.reportingMark, carB.carNumber, carB.kind, carB.loaded, carB.destination);

  

return * this;

}

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

StringOfCars class functions

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

/********************* Constructor ********************/

StringOfCars::StringOfCars()

{

  

}

StringOfCars::StringOfCars(const StringOfCars &obj)

{

  

cars = obj.cars;

  

}

/********************* Destructor ********************/

StringOfCars::~StringOfCars()

{

  

}

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

void StringOfCars::output()

{

  

for (int i = 0; i < cars.size(); i ++)

{

cout<<"Car Number "<<i + 1<<endl;

cars[i].output();

cout<<endl;

}

}

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

void StringOfCars::push(Car & car1)

{

cars.push_back(car1);

}

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

void StringOfCars::pop(Car & car2)

{

car2 = cars.back();

cars.pop_back();

}

/********************* operator *********************

Reads the reportingMark, carNumber, kind, loaded, and destination from the user

*/

bool operator==(Car Car1, Car Car2)

{

return (Car1.reportingMark == Car2.reportingMark && Car1.carNumber == Car2.carNumber);

}

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

Create a friend function for the function operator== which tests to see if two objects are equivalent.

The two objects are equivalent if they have the same reportingMark and carNumber

*/

void input(StringOfCars & string1)

{

string reportingMark;

int carNumber;

string kind;

string load;

bool loaded;

string destination;

string type;

  

ifstream inputFile;

inputFile.open("file.txt");

if(!inputFile)

{

cout<<"Error! Can not find the file!";

exit(100);

}

while(inputFile.peek()!= EOF)

{

inputFile >> type >> reportingMark>> carNumber>> kind >> load;

if (load == "true")

loaded = true;

else

loaded = false;

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

inputFile.get();

inputFile.clear();

getline(inputFile,destination);

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

string1.push(temp);

}

inputFile.close();

}

output

TEST 1
reportingMark SP
carNumber 34567
kind business
loaded true
destination Salt Lake City

TEST 2
STRING 1

Car Number 1
reportingMark CN
carNumber 819481
kind maintenance
loaded false
destination NONE

Car Number 2
reportingMark SLSF
carNumber 46871
kind business
loaded true
destination Memphis

Car Number 3
reportingMark AOK
carNumber 156
kind tender
loaded true
destination McAlester

TEST 3
CAR 3

reportingMark AOK
carNumber 156
kind tender
loaded true
destination McAlester


STRING 1
Car Number 1
reportingMark CN
carNumber 819481
kind maintenance
loaded false
destination NONE

Car Number 2
reportingMark SLSF
carNumber 46871
kind business
loaded true
destination Memphis

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote