Question
In C++ please. Problem 2.2 and solution will be posted below. Problem 3.1 Copy the solution from problem 2.2 Make the following additions and changes:
In C++ please. Problem 2.2 and solution will be posted below.
Problem 3.1
Copy the solution from problem 2.2
Make the following additions and changes:
1. Arrange the functions in the order:
* main
* output
* setUp (was setUpCar)
* input
2. Change the output function so it is a member function of the class Car.
3. Change the name of the setUpCar function to setUp and make it a
member function of the class Car, with five parameters specified as
constant reference parameters.
4. Make all the data in the Car class private and make the member
functions public.
5. Do NOT make the input function a member function.
6. Change the parameters of the input function to five reference
parameters corresponding to the five fields in the class.
7. In main define five variables. Pass these variables to the input
function so the input function can set their values.
8. In main define a Car object. This time the Car object will be in the
stack for main, rather than in the heap. Name this object car1.
9. In main call the setUp function and pass the five variables, rather
than calling it from the input function.
10. In main call the output function.
11. Test the program with the same data as was used in assignment 2.
Problem 3.2
Copy the solution from problem 3.1
Make the following additions and changes:
1. Build three constructors and a destructor for your Car class:
* A constructor that accepts five constant reference parameters.
Build this constructor with only one line of code that calls the
setUp member function.
* A copy constructor.
* A default constructor that sets the following values:
reportingMark (an empty string)
carNumber 0
kind other
loaded false
destination NONE
* A destructor that does nothing, basically a stub.
2. Arrange the constructors and destructor in the class before the
other member functions in the order:
default constructor, copy constructor, constructor that takes five
constant reference parameters, destructor
3. Arrange the function definitions in the same order.
4. Remove the call to the setUp function from the main function.
5. Revise the main function to create three Car objects in the stack:
6. car1 with the same values used in assignment B.
7. car2 which is a copy of car1.
8. car3 which is created with the default constructor.
Now, add code in the main function to print these headings and use the
Car member function to print the data for each of the three cars:
Contents of car1:
reportingMark: SP
carNumber: 34567
kind: box
loaded: true
destination: Salt Lake City
Contents of car2:
reportingMark: SP
carNumber: 34567
kind: box
loaded: true
destination: Salt Lake City
Contents of car3:
reportingMark: B & O
carNumber: 0
kind: other
loaded: false
destination: NONE
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 ";
Prob 2.2
#include
#include
#include
#include
using namespace std;
class Car {
public:
char reportingMark[5]; //reportingMark
int carNumber; //integer type carNumber
string kind; //string type kind
bool loaded; //bool type
string destination; //string type destination
void input(Car *c); //function prototype declarations
void output(Car *c);
}; //class Car declaration
int main() {
Car *c = new Car(); //allocating for class
c->input(c); //calling input function using class object
c->output(c); //calling output function using class object
delete c; //deallocating for class
}
void Car :: input(Car *c) {
string str, s;
cout << " Enter the reporting mark using a 5 or less character uppercase string: ";
cin >> str; //taking a temporary string as user input
for (int i = 0; i < str.length(); i++)
c->reportingMark[i] = toupper(str[i]); //converting any lowercase input to uppercase using toupper function and storing in reportingMark
cout << " Enter the car number: ";
cin >> c->carNumber; //taking carNumber as user input
cout << " Enter the kind: ";
cin >> c->kind; //taking kind as user input
cout << " Enter true or false for loaded status: ";
cin >> s; //taking temporary string as user input
istringstream(s) >> boolalpha >> c->loaded; //converting the string 'true/false' to boolean true/false using bootalpha and storing in loaded
if (c->loaded == true) {
cout << " Enter destination: ";
cin.ignore();
getline(cin, c->destination); //taking destination as user input if loaded is true and getline is used to take strings with spaces
}
else
c->destination = "NONE"; //setting destination to NONE if false
}
void Car :: output(Car *c) { //output function
cout << " Reporting Mark: " << c->reportingMark; //printing reportingMark
cout << " Car Number: " << c->carNumber; //printing carNumber
cout << " Kind: " << c->kind; //printing kind
cout << " Loaded Status: " << boolalpha << c->loaded;
cout << " Destination: " << c->destination << " "; //printing destination
}
Solution:
Reporting Mark: SP
Car Number: 12345
Kind: flat
Loaded Status: true
Destination: Salt Lake City
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