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

IN C++ please! Use the style guide. In Problem C1 we will start building the Car

ID: 3607088 • Letter: I

Question

IN C++ please!

Use the style guide.

In Problem C1 we will start building the Car class. We will use this class throughout the rest of the assignments.

In Problem C2 we will add constructors to the Car class.

In Problem C3 we will add an operator== friend function for the Car class.

Problem C1

Order of functions in the code:

Within the Car class:

setup

output

Global functions, outside of the Car class:

main

input

Define a class named: Car
containing the following private data:

Note:
A destination is required if the car is loaded.
If it is not loaded the destination may be either a destination or the word "NONE".
Be sure to allow the user the option of entering a destination when the car is not loaded.

Within the class create the following member functions:

setup

Is a member function of the class Car

Takes the five parameters by value: reportingMark, carNumber, kind, loaded, and destination

Puts the data in the object

Has a return type of void

output

Is a member function of the class Car

Has no parameters

Prints the member data in a neat format

Has a return type of void

After the class, create the following global functions:

main

Contains six variables:

a pointer named ptr which points to a Car object

a string named reportingMark to contain two to four characters

an int named carNumber

a string named kind which could contain: "business" "maintenance" or "other"

a bool named loaded

a string named destination containing a destination or the word NONE

Uses new to obtain space for an object of type Car

Calls the input, setup, and output functions

Deletes the space obtained using new

input

Is a global function, not a member of the class

Takes the reportingMark, carNumber, kind, loaded, and destination as reference parameters

Reads the reportingMark, carNumber, kind, loaded, and destination from the user

Has a return type of void

Test your program with the following data:

Problem C2

Copy the solution from problem C1.

Order of functions in the code:
Note that the setup and output functions have been moved to a different location.

Car constructors and destructor within the Car class definition

default constructor

copy constructor

other constructors

destructor

main

Car member functions declared within the Car class but defined later

setup

output

input

Make the following additions and changes:

Build three constructors and a destructor for your Car class:A default constructor. Build this constructor with only one line of code that calls the setup member function. Set the following values:

A copy constructor. Build this constructor with only one line of code that calls the setup member function.

A constructor that accepts five parameters. Build this constructor with only one line of code that calls the setup member function.

A destructor that does nothing.

Put a declaration in the Car class for the setup and output functions. (The declaration is just the prototype)
Put the definition of these functions later. (The definition of a function contains the code)

Remove the call to the setup function from the main function.

Remove new and delete from the main function.

Revise the main function to create three Car objects in the stack by using the three constructors:

car1 which uses the values read from the user. Use the same values used to test assignment C1.

car2 which is copied from car1.

car3 which is created with the default constructor.

Then add code in the main function to print these headings and use the output member function to print the data for each of the three cars:

Problem C3

Copy the solution from problem C2.

Order of functions in the code:
Note that the order is not changed, but with the operator== function added.

Car constructors and destructor within the Car class definition

default constructor

copy constructor

other constructors

destructor

main

Car member functions declared within the Car class but defined later

setup

output

operator== with Car parameters

input

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 carNumber (do not look at the kind, loaded, and destination fields).

Add the following code in main:

Test with the same data as in problem C2.

reportingMark a string with the AAR reporting mark carNumber an int kind a string which could contain "business" "maintenance" or "other" loaded a bool destination a string with a destination or the word "NONE"

Explanation / Answer

//MAIN.CPP

#include <iostream>

class Car{
private:
std::string reportingMark;
int carNumber;
std::string kind;
bool loaded;
std::string destination;
  
public:
void setUp(std::string rm, int cn, std::string k, bool l, std::string d);
void output();
Car(){
reportingMark = "";
carNumber = 0;
kind = "other";
loaded = false;
destination = "None";
}
Car(const Car& obj){
reportingMark = obj.reportingMark;
carNumber = obj.carNumber;
kind = obj.kind;
loaded = obj.loaded;
destination = obj.destination;
}
Car(std::string rm, int cn, std::string k, bool l, std::string d)
{setUp(rm, cn, k, l, d);}
~Car(){}
friend bool operator==(Car &car_a, Car &car_b){
return (car_a.reportingMark == car_b.reportingMark && car_a.carNumber == car_b.carNumber);
}
};

void input(std::string* rm, int* cn, std::string* k, bool* l, std::string* d){
// Let user to enter the data into the class. //
std::cout << "Enter the reporting mark (2-4 uppercase characters): ";
std::cin >> *rm;
  
std::cout << "Enter the car number: ";
std::cin >> *cn;
  
std::cout << "Enter the kind of car (Box, Tank, Flat, Other): ";
std::cin >> *k;
  
std::string temp;
std::cout << "Enter true if the car is loaded. If not, enter false: ";
std::cin >> temp;
  
if (temp == "true")
*l = true;
else
*l = false;
  
if (*l)
std::cout << "Enter your destination: ";
else
std::cout << "Enter your destination or None: ";
std::cin.ignore();
getline(std::cin, *d);
}
void Car::output(){
// Print the data input into the screen. //
std::cout << "Reporting Mark: " << reportingMark << std::endl;
std::cout << "Car Number : " << carNumber << std::endl;
std::cout << "Kind : " << kind << std::endl;
std::cout << "Loaded : " << loaded << std::endl;
std::cout << "Destination : " << destination << std::endl;
}
void Car::setUp(std::string mark, int num, std::string kind_car, bool loaded_car, std::string place){
// Store the data entered into a car object. //
reportingMark = mark;
carNumber = num;
kind = kind_car;
loaded = loaded_car;
destination = place;
}

int main() {
std::string report_mark;
int car_num;
std::string kind_car;
bool loaded_car;
std::string d_place;
  
input(&report_mark, &car_num, &kind_car, &loaded_car, &d_place);
  
Car car1(report_mark, car_num, kind_car, loaded_car, d_place);
Car car2(car1);
Car car3;
  
car1.output();
car2.output();
car3.output();
  
if (car1 == car2)
std::cout << "car1 is the same car as car2" << std::endl;
else
std::cout << "car1 is not the same as car2" << std::endl;
  
if (car2 == car3)
std::cout << "car2 is the same car as car3" << std::endl;
else
std::cout << "car2 is not the same as car3" << std::endl;
  
return 0;
}

=======================================================================