Question: HELP WANTED C++ PLEASE. Using the code from lab 4.1, add the ability to read from a file. Modify the input function: * Move the
HELP WANTED C++ PLEASE.
Using the code from lab 4.1, add the ability to read from a file.
Modify the input function:
* Move the input function out of the Cargo class to just below the end of the Cargo class
* At the bottom of the input function, declare a Cargo object named
temp using the constructor that takes the six parameters.
* Use the Cargo output function to print the Cargo object.
* Call the input function from the main function with no arguments. Remove the rest of the code
from main
3. Create a file and use it for input. This is good because you will be
using the input many times.
* Create a file named cardata4.txt (or use the one provided),
containing the following three lines of data.
If you create your own, make sure to follow the format
below:
Pallet PAG PAG45982IB 737 4978 OAK
Container AYF AYF23409AA 737 2209 LAS
Container AAA AAA89023DL 737 5932 DFW
* All weights are in pounds, dont worry about kilograms. You may
comment out that code.
* In the input function, declare an object of type ifstream named
inputFile, which we will use to read from the file.
* At the beginning of the code for the input function, open the
file. If the open fails, send a message to stderr and exit the
program.
* In all the reads within the input function, remove the user
prompt and read from the inputFile object, rather than reading
from the stdin object.
* Hint: We need to use getline when reading the strings.
using >> skips leading white space before reading the data.
getline does not skip this leading whitespace. So, before using
getline use the following code:
while(inputFile.peek() == ' ')
inputFile.get();
peek looks at the next character you are about to read. If it is
a space, get is used to read the space character, to get it out
of the way. Your output will then be much neater.
* Use a loop to read each line from the file. To do this use a
while loop including all the reading in the input function, as
well building and output of the Car.
Hint: you can do this with the following while statement:
while(inputFile.peek() != EOF) or use this form while(inputFile >> type)
The peek function will return EOF if there is no next character.
* At the bottom of the input function, close the file.
Put an eye catcher before the beginning of each function, class, and the
global area:
// class name function name comment(if any)
*************************************************
MY LAB 4.1 CODE 
#include
#include
using namespace std;
// class to define the parameters of the load;
class load{
//private members
private:
string uId;
string abbreviation;
string uldid;
string aircraft;
double weight;
string destination;
//public functions
public:
//default constructor
//doesnt require any function body
load(){}
//copy constructor
load(const load &l);
//mutator function
void input();
//this function prints the elements in object
void output();
//friend function to convert kilo to pounds
friend void kilotopound(load &l);
//friend function to compare two objects
friend bool operator==(load& obj1,load& obj2);
};
int main (){
//Object unit1
load unit1;
unit1.input(); // Calling separate function for taking input.
unit1.output(); // Calling separate function for displaying output.
//defining a new object unit2 on stack using copy constrcutor
load unit2(unit1);
cout
//output second object unit2
unit2.output();
//Creating default object unit3
load unit3;
cout
//Testing overloaded operator
if (unit1 == unit2)
cout
else
cout
if (unit2 == unit3)
cout
else
cout
return 0;
}
//copy constructor definintion
load::load(const load &l){
uId = l.uId;
abbreviation = l.abbreviation;
uldid = l.uldid;
aircraft = l.aircraft;
weight = l.weight;
destination = l.destination;
}
void load::input(){
string x="";
double w=0;
char ch;
cout>x;
this->uId = x;
cout>x;
this->abbreviation = x;
cout>x;
this->uldid = x;
cout>x;
this->aircraft = x;
cout>w;
cout
cin >> ch;
this->weight = w;
cout>x;
this->destination = x;
// if the user wants weight to be converted to pounds call kilotopound and
// pass the current object
if(ch == 'y' || ch == 'Y')
kilotopound(*this);
}
void load::output(){
cout
cout
cout
cout
cout
cout
}
void kilotopound(load &l){
l.weight = l.weight * 2.2;
}
bool operator==(load& obj1,load& obj2){
//Two objects are equal if their abbreviation and uldid are equal
if((obj1.abbreviation.compare(obj2.abbreviation) == 0) &&
(obj1.uldid.compare(obj2.uldid) == 0))
//If equal returns true
return true;
//else returns false
return false;
}
Transcribed image text
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
