Question
C++ program Problem 3.3 Copy the solution from problem 3.2 Create a friend function for the function operator == which tests to see if two
C++ program
Problem 3.3
Copy the solution from problem 3.2
Create a friend function for the function operator== which tests to see
if two objects are equivalent. The two objects are equivalent if they
have the same reportingMark and number (do not look at the kind, loaded,
and destination fields).
Test with the following code in main:
if (car1 == car2)
cout << "car1 is the same car as car2 ";
else
cout << "car1 is not the same car as car2 ";
if (car2 == car3)
cout << "car2 is the same car as car3 ";
else
cout << "car2 is not the same car as car3 ";
Write the code to change car2 to have the following information:
reportingMark: UP
carNumber: 81002
kind: box
loaded: true
destination: Spokane
Print out car1, car2 and car3 after the edits.
MY problem 3.2 code is :
#include
#include
#include
#include
#include
using namespace std;
void input(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination);
class Car
{
private:
static const int FIELD_WIDTH = 22;
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
public:
Car()
{
setUpCar(reportingMark = "", carNumber = 0, kind = "other", loaded = false, destination ="NONE");
}
Car(const Car &userCar)
{
setUpCar(userCar.reportingMark, userCar.carNumber, userCar.kind, userCar.loaded, userCar.destination);
}
Car(string reportingMark, int carNumber, string kind, bool loaded, string destination)
{
setUpCar(reportingMark, carNumber, kind, loaded, 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();
};
int main()
{
string reportingMark;
int carNumber;
string kind;
bool loaded;
string destination;
input(reportingMark, carNumber, kind, loaded, destination);
Car car1(reportingMark, carNumber, kind, loaded, destination);
Car car2(car1);
Car car3;
cout << endl << "Contents of car1:" << endl;
car1.output();
cout << endl << "Contents of car2:" << endl;
car2.output();
cout << endl << "Contents of car3:" << endl;
car3.output();
if (car1 == car2)
cout << "car1 is the same car as car2 ";
else
cout << "car1 is not the same car as car2 ";
if (car2 == car3)
cout << "car2 is the same car as car3 ";
else
cout << "car2 is not the same car as car3 ";
}
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;
}
void Car::output()
{
cout << 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;
}
bool operator==(const Car &car1,const Car &car2)
{
if((car2.reportingMark.compare(car1.reportingMark) == 0) && car2.carNumber == car1.carNumber)
return true;
else
return false;
}
void input(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination)
{
string isLoaded = "";
cout << "Enter Reporting Mark: ";
cin >> reportingMark;
cout << "Enter Car Number: ";
cin >> carNumber;
cout << "Enter kind: ";
cin >> kind;
cout << "Enter loaded: ";
cin >> isLoaded;
if(isLoaded == "yes")
loaded = true;
else
loaded = false;
cout << "Enter destination: ";
cin.ignore();
getline(cin, destination);
while(loaded && destination == "NONE")
{
cout << "Since the car is loaded it must have a destination, please enter destination: ";
getline(cin, destination);
}
}
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