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

This is Problem D2 #include <iostream> #include <cctype> #include <string> #incl

ID: 3671876 • Letter: T

Question

This is Problem D2

#include <iostream>
#include <cctype>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;

class Car
{
private:
    string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public :
Car();//default constructor
Car(const Car &oldcar);//copy constructor
//constructor that takes five constant reference parameters
Car(const string &Mark,const int &Number,const string &strkind,const bool &load,const string &dest);
Car & Car::operator=(const Car & carB);// Car operator=
~Car(){}//destructor that does nothing
void output() const;
void setUp(const string &Mark,const int &Number,const string &strkind,const bool &load,const string &dest);
friend bool operator== (const Car &leftcar,const Car &rightcar);
};
class StringOfCars
{
private:
Car * carptr;
static const int ARRAY_MAX_SIZE = 10;
int size;
public :
StringOfCars();//default constructor
StringOfCars(const StringOfCars & oldobj);//copy constructor
~StringOfCars();//destructor
void push(Car & newcar);
void pop(Car & remcar);
void output() const;
};
void input(StringOfCars & refSOC);
/* ******************** main ********************
* Call the input function
*/
int main()
{
cout<<"TEST 1"<<endl;
Car car1("SP",34567,"box",true,"Salt Lake City");
Car car2 = car1;
car2.output();
cout<<endl;
cout<<"TEST 2"<<endl;
StringOfCars string1;
input(string1);
cout<<"STRING 1"<<endl;
string1.output();
cout<<"TEST 3"<<endl;
Car car3;
string1.pop(car3);
cout<<"CAR 3"<<endl;
car3.output();
cout<<"STRING 1"<<endl;
string1.output();
system("pause");
return 0;
}
/* ******************** Car() ********************
* constructor which create default data
*/
Car::Car()
{
reportingMark = "";
carNumber = 0;
kind = "other";
loaded = false;
destination = "NONE";
}
/* ************ Car(const Car &oldcar) ************
* copy constructor
*/
Car::Car(const Car &oldcar)
{
reportingMark = oldcar.reportingMark;
carNumber = oldcar.carNumber;
kind = oldcar.kind;
loaded = oldcar.loaded;
destination = oldcar.destination;
}
/* ************ Car(five constant referance parameters) ************
* constructor with only one line of code that calls the setUp member function
*/
Car::Car(const string &Mark,const int &Number,const string &strkind,const bool &load,const string &dest)
{
setUp(Mark,Number,strkind,load,dest);
}
/* ******************** output ********************
* Print the data in a neat format
*/
void Car::output() const
{
cout<<"reportingMark "<<reportingMark<<endl;
cout<<"carNumber     "<<carNumber<<endl;
cout<<"kind          "<<kind<<endl;
if (loaded)
cout<<"loaded        "<<"True"<<endl;
else
cout<<"loaded        "<<"False"<<endl;
cout<<"destination   "<<destination<<endl;
}
/* ******************** setUpCar ********************
* setUp function will pass the data to the private
*/
void Car::setUp(const string &Mark,const int &Number,const string &strkind,const bool &load,const string &dest)
{
reportingMark = Mark;
carNumber = Number;
kind = strkind;
loaded = load;
destination = dest;
}
//* **************** 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;
}
/* ***************** StringOfCars() *****************
* constructor which create default data
*/
StringOfCars::StringOfCars()
{
carptr = new Car[ARRAY_MAX_SIZE];
size = 0;
}
/* ************ StringOfCars(const StringOfCars &oldob) ************
* copy constructor
*/
StringOfCars::StringOfCars(const StringOfCars &oldobj)
{
delete [] carptr;
carptr = new Car[ARRAY_MAX_SIZE];
size = oldobj.size;
for (int i = 0; i < size; i ++)
  {
   carptr[i] = oldobj.carptr[i];
  }
}
/* ***************** ~StringOfCars() *****************
* destructor which returns the space to the heap
*/
StringOfCars::~StringOfCars()
{
delete [] carptr;
carptr = 0;
size = 0;
}
/* ******************** output ********************
* prints a heading for each car
*/
void StringOfCars::output() const
{
if (size == 0)
{
  cout <<"NO cars!"<<endl;
}
else
{
  for (int i = 0; i < size; i ++)
  {
   cout<<"The information of No."<<i + 1<<" car is :"<<endl;
   carptr[i].output();
   cout<<endl;
  }
}
}
/* ******************** push ********************
* adds a car to the string of cars
*/
void StringOfCars::push(Car & newcar)
{
if (size == ARRAY_MAX_SIZE)
{
  cout<<"Not valid! It has reached the Max size.";
}
else
{
  carptr[size] = newcar;
  size ++;
}
}
/* ******************** pop ********************
* removes a car from the string of cars
*/
void StringOfCars::pop(Car & remcar)
{
if (size == 0)
{
  cout<<"There is No car to remove.";
}
else
{
  size --;
  remcar = carptr[size];
}
}
/* ************ operator== overloading ************
* tests to see if two objects are equivalent
* return true if they have same reportingMark and number
*/
bool operator== (const Car &leftcar,const Car &rightcar)
{
if (leftcar.reportingMark == rightcar.reportingMark && leftcar.carNumber == rightcar.carNumber)
  return true;
else
  return false;
}
/* ******************** input ********************
* Read all the data from the file
*/
void input(StringOfCars & refSOC)
{
string Mark,strkind,dest,type,strload;
int Number;
bool load;
ifstream infile;
infile.open("cars.txt");
if (!infile)
{
   cerr << "File open failure!";
   exit(EXIT_FAILURE);
}
do
{
  infile>>type;
  infile>>Mark;
  infile>>Number;
     infile>>strkind;
  infile>>strload;
  if(strload == "true")
   load = true;
  else
   load = false;
  while(infile.peek() == ' ')
   infile.get();
  getline(infile,dest);
     Car temp(Mark,Number,strkind,load,dest); //set the Car
     refSOC.push(temp);
}while(infile.peek() != EOF);
infile.close(); //close the file
}
/*
Output:
TEST 1
reportingMark SP
carNumber     34567
kind          box
loaded        True
destination   Salt Lake City
TEST 2
STRING 1
The information of No.1 car is :
reportingMark CN
carNumber     819481
kind          maintenance
loaded        False
destination   NONE

The information of No.2 car is :
reportingMark SLSF
carNumber     46871
kind          business
loaded        True
destination   Memphis

The information of No.3 car is :
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
The information of No.1 car is :
reportingMark CN
carNumber     819481
kind          maintenance
loaded        False
destination   NONE
The information of No.2 car is :
reportingMark SLSF
carNumber     46871
kind          business
loaded        True
destination   Memphis
Press any key to continue.

*/

In Problem, we will use an STL vector to contain the Car objects within the StringOfCar object, rather than using an array.

Copy the solution from problem D2 and make the name OEC1.
Keep the same order of the functions as in problem D2.
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 D2.

Explanation / Answer

#include #include void input(std::string &reportingMark, int &carNumber, std::string &kind, bool &loaded, std::string &destination); class Car { private: const int FIELD_WIDTH = 22; std::string reportingMark; int carNumber; std::string kind; bool loaded; std::string destination; public: void setUp(std::string reportingMark, int carNumber, std:: string kind, bool loaded, std::string destination) { this->reportingMark = reportingMark; this->carNumber = carNumber; this->kind = kind; this->loaded = loaded; this->destination = destination; } void output() { std::cout
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