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

Below is E1, need E2/3/4 done please make sure they run with outputs included th

ID: 666108 • Letter: B

Question

Below is E1, need E2/3/4 done please make sure they run with outputs included thank you.

#include<iostream>

#include<string>

#include<vector>

using namespace std;

//Car class

class Car

{

private:

string reportingMark;

int carNumber;

string kind;

bool loaded;

string destination;

public:

//Default constructor

Car()

{

}

//Parameterized constructor

Car(string _reportingMark,int _carNumber,string _kind,bool _loaded,string _destination)

{

reportingMark = _reportingMark;

carNumber = _carNumber;

kind = _kind;

loaded = _loaded;

destination = _destination;

}

//Destructor

~Car()

{

}

//Copy constructor

Car( const Car &other)

{

reportingMark = other.reportingMark;

carNumber = other.carNumber;

kind = other.kind;

loaded = other.loaded;

destination = other.destination;

}

Car operator=(Car other)

{

reportingMark = other.reportingMark;

carNumber = other.carNumber;

kind = other.kind;

loaded = other.loaded;

destination = other.destination;

return *this;

}

friend bool operator==(Car c1,Car c2);

void setUp()

{

cout<<"Implementing Progress ";

}

void output()

{

cout<<"Reporting Mark: "<< reportingMark<<endl;

cout<<"Card Number: "<< carNumber<<endl;

cout<<"Kind: "<< kind<<endl;

cout<<"Loaded: "<< loaded<<endl;

cout<<"Destination: "<< destination<<endl;

}

};

//Friend function

bool operator==(Car c1,Car c2)

{

if(c1.reportingMark==c2.reportingMark

&& c1.carNumber == c2.carNumber

&& c1.kind == c2.kind

&& c1.loaded == c2.loaded

&& c1.destination == c2.destination)

{

return true;

}

else

{

return false;

}

}

/* ********** StringOfCars member functions ********** */

class StringOfCar

{

private:

vector<Car> cars;

public:

//Default constructor

StringOfCar()

{

}

//Parameterized constructor

StringOfCar(vector<Car> _cars)

{

cars = _cars;

}

//Copy construtor

StringOfCar( const StringOfCar &obj)

{

cars = obj.cars;

}

//Destructor

~StringOfCar()

{

}

void output()

{

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

{

cars[i].output();

}

}

void push(Car car)

{

cars.push_back(car);

}

Car pop()

{

Car lastObj = cars.back();

cars.pop_back();

return lastObj;

}

void input()

{

cout<<"In progress! ";

}

};

int main()

{

/* ********Create a Car object named car1 with the following constants as initial values:****/

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

cout<<"---------- TEST1 car1 object content-------------- ";

car1.output();

/* ******** Create a Car object named car2 using the default constructor. */

Car car2;

//Use the = operator to copy the data from car1 to car 2.

car2 = car1;

cout<<" ----------car2 object content-------------- ";

//Print car2

car2.output();

//Create a default StringOfCars object named string1.

StringOfCar string1;

string1.push(car1);

//Print: STRING 1

cout<<" ---------- TEST2 string1 object content-------------- ";

string1.output();

//Create a car named car3.

Car car3;

car3 = string1.pop();

//Print car3.

cout<<" ----------TEST3 car3 object content-------------- ";

car3.output();

//Then print the contents of string1 again

cout<<" ----------string1 object content-------------- ";

string1.output();

return 0;

}

Problem E2

Copy the solution from problem E1 and make the name E2.

We will use an enum to keep the kind, rather than using a string as we

did in previous problems. Not all of these kind values will be used in

this problem, but will be needed later in this assignment.

In the global area, before the Car class, define an enum named Kind,

with the following values in this order:

business, maintenance, other, box, tank, flat, otherFreight, chair,

sleeper, otherPassenger

Also in the global area define an array of const string objects named

KIND_ARRAY.

It will contain strings with the same text as the names of the values in

the enum, in the same order.

Change the Kind field in the Car class so it is: Kind kind;

Create a function named setKind that is a member function of the class

Car. It takes the string containing the kind as a parameter. It sets the

kind field to business if the parameter is "business". It sets the kind

firld to maintenance if the parameter is "maintenance". Otherwise it

sets the kind field to other.

Change the setup member function so it calls the setKind function to set

the kind.

Change the output member function to use the KIND_ARRAY values to print

the string corresponding to the enum value.

Change the copy constructor so that when it calls the setup function, it

passses the string from the KIND_ARRAY corresponding to the kind enum

value from the old Car.

Make the same change in the operator= member function.

Test with the same tests as in Problem D2.

      Problem E3

Copy the solution from problem E2 and make the name E3.

In this problem we will use inheritance to create two new classes, both

of which will inherit from the class Car

Do not change the StringOfCar class .

We are not using a StringOfCars in this problem, but will need it later.

The kind of cars for the three classes will be:

Car:   business, maintenance, other

FreightCar:   box, tank, flat, otherFreight

PassengerCar:   chair, sleeper, otherPassenger

Change private to protected in the Car class only.

Make two classes that inherit from the Car class:   FreightCar and

PassengerCar.

No additional member data will be added.

Each class will need a default constructor, a copy constructor, and a

constructor that has five parameters.

Create setKind functions for the FreightCar and PassengerCar classes

that are similar to the setKind function for the Car class, but with

different values.

The setKind function for the FreightCar class uses only the values:

box, tank, flat, otherFreight

The setKind function for the PassengerCar class uses only the values:

chair, sleeper, otherPassenger

Make the setKind and destructor functions virtual in the Car class only.

This is only done in the declaration, not the definition of the functions.

This is only done in the parent class, not the children classes.

Remove all the code from the main function and replace it with the

following.

Create a Car object named car1 with the following data:

|SLSF 46871 wrecker true Memphis|

Print the car.

Create a Freight Car object named car2 with the following data:

|MP 98765 gondola true Saint Louis|

Print the freignt car.

Create a Passenger Car object named car3 with the following data:

|PAPX 145 combine true Tucson|

Print the passenger car.

Problem E4

Copy the solution from problem E3 and make the name E4.

In this problem we will use the StringOfCars class to contain Car,

FreightCar, and PassengerCar objects all in the same string of cars.

This works because a pointer of type Car * can point to Car objects as

well as point to the child FreightCar and PassengerCar objects.

Because we have pointers of type Car * that may point to any one of the

three types of objects, a StringOfCars object does not know which type

object will be encountered until execution time. The mechanism to select

the correct version of the function at execution time, rather than

having it fixed at compile time, is to use virtual functions. That is

why we made the setKind and destructor functions virtual earlier in this

assignment.

In the input function loop read one line from the file each time

throught the loop, look at the Type field in the record. If the type is

Car create a Car object and call the push function to put it in the

StringOfCars object. Similarly if the type is FreightCar, create and

push a FreightCar object. If it is a PassengerCar, create and push a

PassengerCar object.

We have a push member function that accepts a Car parameter, creates a

copy of the Car parameter that is a Car object in the heap, then puts

the pointer to that Car object into the array. Build another member

function, also named push, that takes a FreightCar parameter, creates a

FreightCar in the heap, and then puts the pointer to that FreightCar

object into the array. Also build a similar member function for a

PassengerCar.

The file for this problem should contain:

Type ARR number kind loaded destination

Car CN 819481 maintenance false NONE

Car SLSF 46871 business true Memphis

Car AOK 156 tender true McAlester

FreightCar MKT 123456 tank false Fort Worth

FreightCar MP 98765 box true Saint Louis

FreightCar SP 567890 flat true Chicago

FreightCar GMO 7878 hopper true Mobile

PassengerCar car8 KCS 7893 chair true Kansas City

PassengerCar car9 PAPX 145 sleeper true Tucson

PassengerCar car10 GN 744 combine false NONE

Remove the code from the main funtion and replace it with the following

Define a StringOfCars object in the stack. Then pass that object to the

input function. Then call the output function for that object.

You may note that the pop member function and copy constructor for a

StringOfCars do not determine what type of car is being retrieved.

Fixing this problem is more than what I want you to do in the

assignment. So, ignore this problem.

Explanation / Answer

#include<iostream>
#include<string>
#include<vector>
using namespace std;
//Car class
class Car
{
private:
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public:
//Default constructor
Car()
{
}
//Parameterized constructor
Car(string _reportingMark,int _carNumber,string _kind,bool _loaded,string _destination)
{
reportingMark = _reportingMark;
carNumber = _carNumber;
kind = _kind;
loaded = _loaded;
destination = _destination;
}
//Destructor
~Car()
{
}
//Copy constructor
Car( const Car &other)
{
reportingMark = other.reportingMark;
carNumber = other.carNumber;
kind = other.kind;
loaded = other.loaded;
destination = other.destination;
}
Car operator=(Car other)
{
reportingMark = other.reportingMark;
carNumber = other.carNumber;
kind = other.kind;
loaded = other.loaded;
destination = other.destination;
return *this;
}
friend bool operator==(Car c1,Car c2);
void setUp()
{
cout<<"Implementing Progress ";
}
void output()
{
cout<<"Reporting Mark: "<< reportingMark<<endl;
cout<<"Card Number: "<< carNumber<<endl;
cout<<"Kind: "<< kind<<endl;
cout<<"Loaded: "<< loaded<<endl;
cout<<"Destination: "<< destination<<endl;
}
};
//Friend function
bool operator==(Car c1,Car c2)
{
if(c1.reportingMark==c2.reportingMark
&& c1.carNumber == c2.carNumber
&& c1.kind == c2.kind
&& c1.loaded == c2.loaded
&& c1.destination == c2.destination)
{
return true;
}
else
{
return false;
}
}
/* ********** StringOfCars member functions ********** */
class StringOfCar
{
private:
vector<Car> cars;
public:
//Default constructor
StringOfCar()
{
}
//Parameterized constructor
StringOfCar(vector<Car> _cars)
{
cars = _cars;
}
//Copy construtor
StringOfCar( const StringOfCar &obj)
{
cars = obj.cars;
}
//Destructor
~StringOfCar()
{
}
void output()
{
for(int i=0; i<cars.size(); i++)
{
cars[i].output();
}
}
void push(Car car)
{
cars.push_back(car);
}
Car pop()
{
Car lastObj = cars.back();
cars.pop_back();
return lastObj;
}
void input()
{
cout<<"In progress! ";
}
};
int main()
{
/* ********Create a Car object named car1 with the following constants as initial values:****/
Car car1("SP",34567 ,"business",true,"Salt Lake City ");
cout<<"---------- TEST1 car1 object content-------------- ";
car1.output();
cout<<" thank you ";
/* ******** Create a Car object named car2 using the default constructor. */
Car car2;
//Use the = operator to copy the data from car1 to car 2.
car2 = car1;
cout<<" ----------car2 object content-------------- ";
//Print car2
car2.output();
cout<<" thank you ";
//Create a default StringOfCars object named string1.
StringOfCar string1;
string1.push(car1);
//Print: STRING 1
cout<<" ---------- TEST2 string1 object content-------------- ";
string1.output();
cout<<" thank you ";
//Create a car named car3.
Car car3;
car3 = string1.pop();
//Print car3.
cout<<" ----------TEST3 car3 object content-------------- ";
car3.output();
cout<<" thank you ";
//Then print the contents of string1 again
cout<<" ----------string1 object content-------------- ";
string1.output();
cout<<" thank you ";
return 0;
}

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