Question
Problem 4.2 Copy the solution for problem 4.1 Posting Code Below Questions Please Provide Your 4.2 Code So I can see mine and compare. Copy
Problem 4.2
Copy the solution for problem 4.1
Posting Code Below Questions
Please Provide Your 4.2 Code So I can see mine and compare.
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 cardata.txt file as in problem 4.1
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
4.1 Code Below:
#include #include #include #include #include using namespace std;
// function prototype void input();
//**************************************** //*** Car class *** //**************************************** class Car { private: static const int FIELD_WIDTH = 22; string reportingMark; int carNumber; string kind; bool loaded; string destination; public: Car(); Car(const Car& userCar); Car(string reportingMark, int carNumber, string kind, bool loaded, string destination); ~Car() {}; friend bool operator==(const Car& car1, const Car& car2); void setUpCar(string reportingMark, int carNumber, string kind, bool loaded, string destination); void output(); };
//***************************************** //*** main function to run program *** //***************************************** int main() { input(); return 0; }
//********************************************** //*** default constructor for Car class *** //********************************************** Car::Car() { setUpCar(reportingMark = "", carNumber = 0, kind = "other", loaded = false, destination = "NONE"); }
//********************************************** //*** copy constructor for Car class *** //********************************************** Car::Car(const Car& userCar) { setUpCar(userCar.reportingMark, userCar.carNumber, userCar.kind, userCar.loaded, userCar.destination); }
//********************************************** //*** other constructor for Car class *** //********************************************** Car::Car(string reportingMark, int carNumber, string kind, bool loaded, string destination) { setUpCar(reportingMark, carNumber, kind, loaded, destination); }
//********************************************** //*** output function for Car class *** //********************************************** void Car::output() { cout << left << setw(FIELD_WIDTH) << "Reporting Mark: " << setw(FIELD_WIDTH) << reportingMark << endl; cout << setw(FIELD_WIDTH) << "Car Number: " << setw(FIELD_WIDTH) << carNumber << endl; cout << setw(FIELD_WIDTH) << "Kind: " << setw(FIELD_WIDTH) << kind << endl; cout << setw(FIELD_WIDTH) << "Loaded : " << setw(FIELD_WIDTH); if (loaded) cout << "Loaded" << endl; else cout << "Not Loaded" << endl; cout << setw(FIELD_WIDTH) << "Destination: " << setw(FIELD_WIDTH) << destination << endl; }
//********************************************** //*** setUpCar function for Car class *** //********************************************** void Car::setUpCar(string reportingMark, int carNumber, string kind, bool loaded, string destination) { this->reportingMark = reportingMark; this->carNumber = carNumber; this->kind = kind; this->loaded = loaded; this->destination = destination; }
//*************************************************** //*** overloading operator friend in Car class *** //*************************************************** bool operator==(const Car& car1, const Car& car2) { if ((car2.reportingMark.compare(car1.reportingMark) == 0) && car2.carNumber == car1.carNumber) return true; else return false; }
//******************************************* //*** defination of input function *** //******************************************* void input() { string reportingMark; int carNumber; string kind; bool loaded; string destination; string isLoaded; string type;
ifstream inputFile("cardata.txt"); if (!inputFile) { cerr << "Can not open File!" << endl; return; }
while (inputFile.peek() != EOF) { inputFile >> type; inputFile >> reportingMark; inputFile >> carNumber; inputFile >> kind; inputFile >> isLoaded; if (isLoaded == "true") loaded = true; else loaded = false; while (inputFile.peek() == ' ') { inputFile.get(); } getline(inputFile, destination);
Car temp(reportingMark, carNumber, kind, loaded, destination); temp.output(); cout << endl; // print empty line } }
====================================
cardata.txt
====================================
Car CN 819481 maintenance false NONE Car SLSF 46871 tank true Memphis Car AOK 156 tender true McAlester
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started