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

In Problem E1 we use an array of pointers to Car and make a copy of the users Ca

ID: 3778160 • Letter: I

Question

In Problem E1 we use an array of pointers to Car and make a copy of the users Car objects in the heap.

Keep the order for the functions

Problem 1

1.Car constructors and destructor within the Car class definition

a.default constructor

b.copy constructor

c.other constructors

d.destructor

2.StringOfCars constructors and destructor within the StringOfCar class definition

a.default constructor

b.copy constructor

c.other constructors

d.destructor

3.main

4.Car member functions declared within the Car class but defined later

a.setup

b.output

c.operator=

5.StringOfCars member functions declared within the StringOfCars class but defined later

a.output

b.push

c.pop

6.global functions

a.operator== with Car parameters

b.input

In the StringOfCars class, Change the array of Car objects to an array of pointers to Car objects. To do this you need to change the pointer type from Car * to Car **
Then we will need to make changes to use these pointers. The constructors, destructor, and member functions will have similar operation, but will need changes to work with pointers. I suggest you make the following changes, one at a time.

*All the StringOfCars constructors will get space for Car * elements, rather than Car elements in the array. To allow this the pointer in the private data for the StringOfCars class will be: Car ** ptr;

*All the StringOfCars constructors will set each unused element in the array to zero. This might best be done by setting them all to zero before using the array.

*The output function needs to dereference the pointers to get at the Car objects.

*Change the push function. It will take a parameter that is a Car by constant reference. It will allocate space in the heap for one Car object that is a copy of the Car parameter and then put the pointer to that Car in the array. Then it will increment the carCount.

*The copy constructor will get space for the array of Car* elements and set them to zero. It will set the carCount to zero. Then it will use the push function to push each each Car object into the array.

*The pop function will take a Car parameter by reference. It will copy the Car to the reference parameter. Then it will delete the Car from the heap. It will set the entry in the array that is no longer used to 0. It will decrement the carCount.

*The distructor will need to delete each Car pointed to by the array, and then delete the array.

Test with the tests as below:

Replace the main function to do the following tests:

1.Test the Car operator= function.
Print: TEST 1
Creat a Car object named car1 by using a constructor 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


#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

class Car
{
private:
    static const int FIELD_WIDTH = 22;
    std::string reportingMark;
    int carNumber;
    std::string kind;
    bool loaded;
    std::string destination;
public:

    Car()
     {setUp(reportingMark = "", carNumber = 0, kind = "other", loaded = false, destination ="NONE");}
    Car(const Car &userCar)
     {setUp(userCar.reportingMark, userCar.carNumber, userCar.kind, userCar.loaded, userCar.destination);}
    Car(std::string reportingMark, int carNumber, std::string kind, bool loaded, std::string destination)
     {setUp(reportingMark, carNumber, kind, loaded, destination);}
    ~Car(){};

    Car &operator= (const Car &carB);
    friend bool operator == (const Car &car1, const Car &car2);
    void setUp(std::string reportingMark, int carNumber, std:: string kind, bool loaded, std::string destination);
    void output();
};
class StringOfCars
{
private:
    Car ** ptr;
    static const int ARRAY_SIZE = 10;
    int carCount;
public:
    StringOfCars()
     {
        ptr = new Car*[ARRAY_SIZE];
        for(int i = 0; i < ARRAY_SIZE; i++)
        {
            ptr[i] = 0;
        }
        carCount = 0;
     }
    StringOfCars(const StringOfCars &oldPtr)
     {
        for(int i = 0; i < ARRAY_SIZE; i++)
        {

        }
     }
    ~StringOfCars()
     {
        delete [] ptr;
     }

    void output();
    void push(Car &car1);
    void pop(Car &car1);
};

//prototype for the input function
void input(StringOfCars &string1);

int main()
{
    std::string reportingMark = "SP";
    std::string kind = "business";
    std::string destination = "Salt Lake City";
    int carNumber = 34567;
    bool loaded = true;
    Car car1(reportingMark, carNumber, kind, loaded, destination);
    Car car2 = car1;
    Car car3;
    StringOfCars string1;
    std::cout << "TEST 1" << std::endl;
    car2.output();
    std::cout << std::endl << "TEST 2" << std::endl;
    std::cout << "STRING 1" << std::endl;
    input(string1);
    string1.output();
    std::cout << std::endl << "TEST 3" << std::endl;
    //testing pop function
    string1.pop(car3);
    std::cout << std::endl << "CAR 3" << std::endl;
    car3.output();
    std::cout << "STRING 1" << std::endl;
    string1.output();
return 0;
}

/*******************************
Car class functions
*******************************/

/******** Car::setUp() **
sets values in the object
************************/
void Car::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;
}
/******** Car::output() **********
outputs the contents of the object
*********************************/
void Car::output()
{
std::cout << std::setw(FIELD_WIDTH) << std::left << "Reporting Mark: " << std::setw(FIELD_WIDTH) << this->reportingMark << std::endl;
std::cout << std::setw(FIELD_WIDTH) << "Car Number: "     << std::setw(FIELD_WIDTH) << this->carNumber     << std::endl;
std::cout << std::setw(FIELD_WIDTH) << "Kind: "           << std::setw(FIELD_WIDTH) << this->kind          << std::endl;
std::cout << std::setw(FIELD_WIDTH) << "Loaded: "         << std::setw(FIELD_WIDTH);
if(this->loaded)
    std::cout << "true" << std::endl;
else
    std::cout << "false" << std::endl;
std::cout << std::setw(FIELD_WIDTH) << "Destination: "    << std::setw(FIELD_WIDTH) << destination   << std::endl;
std::cout << std::endl;
}
/************** 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
***************************/

/** StringOfCars::out() **
outputs the contents of a
StringOfCars object
*************************/
void StringOfCars::output()
{
    for(int i = 0; i < carCount; i++)
     {
        std::cout<< "Car " << i + 1 << ":" << std::endl;
        ptr[i]->output();
     }
}
/** StringOfCars::push() **
add a Car object to the
StringOfCars array
**************************/
void StringOfCars::push(Car &car1)
{
     Car * tempCar = new Car();
     tempCar = &car1;
    if(carCount < ARRAY_SIZE)
    {
        ptr[carCount] = tempCar;;
        carCount++;
    }
    else
        std::cout << "The list is full." << std::endl;
}

void StringOfCars::pop(Car &car)
{
   Car tempCar = car;
   for(int i = 0; i < ARRAY_SIZE; i++)
   {
        if(ptr[i] == &tempCar)
        {
            delete &tempCar;
            ptr[i] = 0;
        }
        ptr++;
   }

}


/*********************************
global functions
*********************************/

/********* operator== ************
this operator overload is used to establish
equality between two Car objects based
on their reportingMark and carNumber
*********************************/
bool operator == (const Car &car1, const Car &car2)
{
if(car1.reportingMark == car2.reportingMark && car1.carNumber == car2.carNumber)
    return true;
else
    return false;
}

/********* input() **************
this functions reads information from a designated file
and places the information within a temporary Car object
if it has a carType of Car. It then prints the data to
the screen.
*********************************/
void input(StringOfCars &string1)
{
std::ifstream inputFile;
std::string reportingMark;
std::string kind;
std::string destination;
std::string carType;
std::string isLoaded;
int carNumber;
bool loaded;
inputFile.open("carData.txt");

while(inputFile.peek() != EOF)
   {
    inputFile >> carType >> reportingMark >> carNumber >> kind >> isLoaded;

    if(isLoaded == "true")
        loaded = true;
    else
        loaded = false;

    while(inputFile.peek() == ' ')
     {
        inputFile.get();
     }
    std::getline(inputFile, destination);

    if(carType == "Car")
     {
      Car temp(reportingMark, carNumber, kind, loaded, destination);
      string1.push(temp);
     }
    else
      std::cout << " ERROR: Not a valid vehicle type. ";
   }
inputFile.close();
}


sample output


TEST 1
Reporting Mark:       SP
Car Number:           34567
Kind:                 business
Loaded:               true
Destination:          Salt Lake City


TEST 2
Car 1:
Reporting Mark:       CN
Car Number:           819481
Kind:                 maintenance
Loaded:               false
Destination:          NONE

Car 2:
Reporting Mark:       SLSF
Car Number:           46871
Kind:                 business
Loaded:               true
Destination:          Memphis

Car 3:
Reporting Mark:       AOK
Car Number:           156
Kind:                 tender
Loaded:               true
Destination:          McAlester


TEST 3
CAR 3
Reporting Mark:       AOK
Car Number:           156
Kind:                 tender
Loaded:               true
Destination:          McAlester

STRING 1
Car 1:
Reporting Mark:       CN
Car Number:           819481
Kind:                 maintenance
Loaded:               false
Destination:          NONE

Car 2:
Reporting Mark:       SLSF
Car Number:           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