Need help doing this program. The followng in bold is the solution. reportingMar
ID: 665442 • Letter: N
Question
Need help doing this program.
The followng in bold is the solution.
reportingMark:CN
carNumber: 819481
kind: maintenance
loaded: false
destination: NONE
reportingMark:SLSF
carNumber: 46871
kind: business
loaded: true
destination: Memphis
reportingMark:AOK
carNumber: 156
kind: tender
loaded: true
destination: McAlester
Copy the solution for problem D1 and change the problem number to D2.
Order of functions in the code:
1. Car constructors and destructor within the Car class definition
1. default constructor
2. copy constructor
3. other constructors
4. destructor
2. StringOfCars constructors and destructor within the StringOfCar
class definition
1. default constructor
2. copy constructor
3. other constructors
4. destructor
3. main
4. Car member functions declared within the Car class but defined later
1. setUp
2. output
3. operator=
5. operator== friend of Car
6. StringOfCars member functions declared within the StringOfCars class
but defined later
1. output
2. push
3. pop
7. input
Copy the following operator= overload member function that returns the
left hand operator by reference. Also code a prototype for this function
within the Car class.
/* ******************** 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;
}
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, named ptr.
* a static const int set to 10, named ARRAY_SIZE.
* an int containing the current number of Cars in the array, named
carCount.
* a default constructor which gets space in the heap for the array of
Car elements, and sets the the carCount to zero.
* a copy constructor which gets new space in the heap for an array of
Car elements with size ARRAY_CARS. It copies the each Car and also
copies the carCount.
* a destructor which returns the space to the heap.
* a push function which adds a car to the string of cars. Exit with an
error message if the string of cars is full.
* a pop function which removes a car from the string of cars, Last In
First Out (LIFO). The return type is void. There is one prarameter
which is a reference to a Car object. Put the Car object that was
taken out from the array into the parameter Car object. Exit with an
error message if the string is empty.
* 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
Make the following changes in the input function:
* Change the input function to have a parameter that is a reference to
a StringOfCars.
* After creating a Car, do not print it.
* Instead, push the Car onto the StringOfCars.
Modify the main function to do the following tests:
1. Test the Car operator= function.
Print: TEST 1
Creat a Car object named car1 with the following constants as
initial values:
reportingMark: SP
carNumber: 34567
kind: business
loaded: true
destination: Salt Lake City
Create a Car object named car2 using the default constructor.
Use the = operator to copy the data from car1 to car 2.
Print car2
2. Test the StringOfCar push function.
Add to main:
Print: TEST 2
Create a default StringOfCars object named string1.
Pass string1 to the input function.
Use the same file as in problem D1.
Print: STRING 1
In the main 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
Explanation / Answer
Answer:
NOTE: No solution for problem D1 is provided to copy and modify.
#include <iostream>
#include <string>
using namespace std;
class Car
{
friend bool operator==(const Car &carA, const Car &carB);
public:
Car();
Car(const Car &otherCar);
Car(string aReportingMark, int aCarNumber, string aKind, bool aLoaded, string aDestination);
~Car();
void setup(string aReportingMark, int aCarNumber, string aKind, bool aLoaded, string aDestination);
void output();
Car & Car::operator=(const Car & carB);
private:
string reportingMark;
int carNumber;
bool loaded;
string destination;
};
class StringOfCars
{
public:
StringOfCars();
StringOfCars(const StringOfCars &otherStringOfCars);
~StringOfCars();
void output();
void push(const Car &car);
void pop(Car &car);
private:
Car *ptr;
static const int ARRAY_SIZE = 10;
int carCount;
};
StringOfCars::StringOfCars()
{
ptr = new Car[ARRAY_SIZE];
carCount = 0;
}
StringOfCars::StringOfCars(const StringOfCars &otherStringOfCars)
{
ptr = new Car[otherStringOfCars.ARRAY_SIZE];
for(int i = 0; i < otherStringOfCars.carCount; i++)
ptr[i] = otherStringOfCars.ptr[i];
carCount = otherStringOfCars.carCount;
}
StringOfCars::~StringOfCars()
{
delete [] ptr;
}
void StringOfCars::output()
{
if(carCount == 0)
{
cout << "NO cars" << endl;
}
else
{
for(int n = 0; n < carCount; n++)
{
ptr[n].output();
}
}
}
void StringOfCars::push(const Car &car)
{
if(carCount == ARRAY_SIZE)
{
cout << "The string of cars is full." << endl;
}
else
{
ptr[carCount] = car;
carCount++;
}
}
void StringOfCars::pop(Car &car)
{
if(carCount == 0)
{
cout << "The string of cars is empty." << endl;
}
else
{
carCount--;
car = ptr[carCount];
}
}
void input(StringOfCars &soc)
{
}
int main()
{
Car car1("SP", 34567, "business", true, "Salt Lake City");
Car car2;
car2 = car1;
car2.output();
StringOfCars string1;
input(string1);
string1.output();
Car car3;
string1.pop(car3);
car3.output();
string1.output();
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.