Lab 4.1
Utilizing the code from Lab 3.2, add an overloaded operator == which will be used to compare
two objects to see if they are equal. For our purposes, two objects are equal if their abbreviation
and uldid are the same. You’ll need to make your overloaded operator a friend of the Cargo class.
Create a unit1 object and load it with this data:
uld – Pallet
abbreviation – PAG
uldid – PAG32597IB
aircraft - 737
weight – 3321 Kilograms
destination – SEA
Make a copy of unit1 called unit2 utilizing a copy constructor.
Create a default unit3.
Test with code like this in main:
if (unit1 == unit2)
cout << "\n unit1 is the same as unit2 \n";
else
cout << " \nunit1 is not the same as unit2 \n";
if (unit2 == unit3)
cout << " \nunit2 is the same as unit3 \n";
else
cout << " \nunit2 is not the same as unit3\n";
Lab 4.2
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, don’t 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)
*************************************************
Lab 3.2 code
#include<iostream>
#include<string>
using namespace std;
class Item
{
public:
string uld;
string abbreviation;
string uldid;
int aircraft;
double weight;
string destination;
Item()
{
}
Item(Item & i)
{
uld = i.uld;
abbreviation= i.abbreviation;
uldid=i.uldid;
aircraft=i.aircraft;
weight=i.weight;
destination=i.destination;
}
void print()
{
cout<<"uld -- "<<uld<<endl;
cout<<"abbreviation -- "<<abbreviation<<endl;
cout<<"uldid -- "<<uldid<<endl;
cout<<"aircraft -- "<<aircraft<<endl;
cout<<"weight -- "<<weight<<" Kilograms"<<endl;
cout<<"destination -- "<<destination<<endl;
}
friend void kilotopound(Item );
};
void kilotopound(Item I)
{
cout<<"Weight in pounds is: "<<I.weight*2.2<<" Pounds "<<endl;
}
int main()
{
Item I;
I.uld="Container";
I.uldid="ABB";
I.abbreviation="ABB315451B";
I.aircraft=737;
I.weight=1156;
I.destination="BUR";
Item I2(I);
//printing data
I.print();
kilotopound(I);
cout<<"Second Object\n";
I2.print();
//printing weight in pounds using kilopounds
kilotopound(I2);
return 0;
}
for lab 4.1-
code in text-
#include<iostream> #include<string> using namespace std; class Item { public: string uld; string abbreviation; string uldid; int aircraft; double weight; string destination; Item() { } Item(Item & i) { uld = i.uld; abbreviation= i.abbreviation; uldid=i.uldid; aircraft=i.aircraft; weight=i.weight; destination=i.destination; } void print() { cout<<"uld -- "<<uld<<endl; cout<<"abbreviation -- "<<abbreviation<<endl; cout<<"uldid -- "<<uldid<<endl; cout<<"aircraft -- "<<aircraft<<endl; cout<<"weight -- "<<weight<<" Kilograms"<<endl; cout<<"destination -- "<<destination<<endl; } friend bool operator ==(Item &,Item &);//friend function friend void kilotopound(Item ); }; bool operator ==(Item &a, Item &b){//as given in the question only two values are compared if((a.abbreviation==b.abbreviation) && (a.uldid==b.uldid)){ return true; } else{ return false; } } void kilotopound(Item I) { cout<<"Weight in pounds is: "<<I.weight*2.2<<" Pounds "<<endl; } int main() { Item I; I.uld="Container"; I.uldid="ABB"; I.abbreviation="ABB315451B"; I.aircraft=737; I.weight=1156; I.destination="BUR"; Item I2(I); //printing data I.print(); kilotopound(I); cout<<"Second Object\n"; I2.print(); //printing weight in pounds using kilopounds kilotopound(I2); Item unit1;//unit1 as given in question unit1.uld="Pallet"; unit1.abbreviation="PAG"; unit1.uldid="PAG32597IB"; unit1.aircraft=737; unit1.weight=3321; unit1.destination="SEA"; Item unit2(unit1),unit3(I);//initializing unit2 and unit 3 by default copy constructor if (unit1 == unit2) cout << "\n unit1 is the same as unit2 \n"; else cout << " \nunit1 is not the same as unit2 \n"; if (unit2 == unit3) cout << " \nunit2 is the same as unit3 \n"; else cout << " \nunit2 is not the same as unit3\n"; return 0; }
screen shot of code-
output-
for lab 4.2
note- as a hint provided Hint: We need to use getline when reading the strings.
we take input as different values not as a sentence therefore getline is of no use in this case.
getline would only make code lengthy, if you want getline in your answer please comment down below I will edit the code according to that.
code in text-
#include<iostream> #include <fstream> #include<string> using namespace std; class Item { public: string uld; string abbreviation; string uldid; int aircraft; double weight; string destination; Item (){} Item(string iuld, string iabbreviation, string iuldid, int iaircraft, double iweight , string idestination) { uld=iuld; abbreviation=iabbreviation; uldid=iuldid; aircraft=iaircraft; weight=iweight; destination=idestination; } Item(Item & i) { uld = i.uld; abbreviation= i.abbreviation; uldid=i.uldid; aircraft=i.aircraft; weight=i.weight; destination=i.destination; } void print() { cout<<"uld -- "<<uld<<endl; cout<<"abbreviation -- "<<abbreviation<<endl; cout<<"uldid -- "<<uldid<<endl; cout<<"aircraft -- "<<aircraft<<endl; cout<<"weight -- "<<weight<<" Kilograms"<<endl; cout<<"destination -- "<<destination<<endl; } friend bool operator ==(Item &,Item &);//friend function friend void kilotopound(Item ); }; bool operator ==(Item &a, Item &b){//as given in the question only two values are compared if((a.abbreviation==b.abbreviation) && (a.uldid==b.uldid)){ return true; } else{ return false; } } void input(){ ifstream inputFile; string iuld; string iabbreviation; string iuldid; int iaircraft; double iweight ; string idestination;//for input // of data inputFile.open("cardata4.txt");//opens file if(!inputFile){//if file fails to open cout << "file unable to open"<< endl; exit(0); } else{ while(inputFile.peek() != EOF) {//till end of file while (inputFile.peek() == ' ')// as given in question inputFile.get(); inputFile >> iuld >> iabbreviation >> iuldid >> iaircraft >> iweight >> idestination;//takes input from file Item temp(iuld, iabbreviation, iuldid, iaircraft, iweight, idestination);//passes t his vale to temp temp.print();//prints that value } } inputFile.close(); } /*void kilotopound(Item I) { cout<<"Weight in pounds is: "<<I.weight*2.2<<" Pounds "<<endl; } *///because all weight are in pounds int main() { input(); return 0; }
screen shot of code-
input file-
output-
If you have any further query feel free to ask in comment section, elseif you like this answer please give an UPVOTE.
Lab 4.1 Utilizing the code from Lab 3.2, add an overloaded operator == which will be...
Lab 5.1 C++ Utilizing the code from Lab 4.2, replace your Cargo class with a new base class. This will be the base for two classes created through inheritance. The Cargo class will need to have virtual functions in order to have them redefined in the child classes. You will be able to use many parts of the new Cargo class in the child classes since they will have the same arguments/parameters and the same functionality. Child class one will...
Please use my Lab 3.2 code for this assignment Lab 4.2 instruction 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...
Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...
Hi please help with c++ programming. This is my code. there's a syntax error. PLEASE FIX MY CODE instead of re-designing the program. Thanks /* Description of problem: Introduction to Friends functions. Create objects in the stack (not on the heap) 3.1: Add a friend function, kilotopound, which will convert kilograms to pounds. 3.2: Add a copy constructor utilizing Lab 3.1 code. Copy the fist object using a copy constructor. Output the contents of both objects. */ #include <iostream>...
CIS 22B Lab 3 Itty Bitty Airfreight (IBA) Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Utilizing Lab 2.2 code Create your objects in the stack (not on the heap). Add a friend function, kilotopound, which will convert kilograms to pounds. Change your weight mutator to ask whether weight is input in kilograms or pounds. If it is kilograms, call the friend function kilotopound to convert it to pounds and return pounds.There are 2.2 pounds in one kilogram. Create an...
#include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...
My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...
IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file. Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g.,...
13.5 LAB: Zip code and population (class templates) Define a class StatePair with two template types (T1 and T2), a constructor, mutators, accessors, and a Printlnfo0 method. Three vectors have been pre-filled with StatePair data in main: . vector<StatePair <int, string>> zipCodeState: ZIP code - state abbreviation pairs . vector<StatePair<string, string>> abbrevState: state abbreviation - state name pairs • vector<StatePair<string, int>> statePopulation: state name - population pairs Complete mainO to use an input ZIP code to retrieve the correct state abbreviation from the vector zipCodeState. Then use...
In this lab, you will need to implement the following functions in Text ADT with C++ language(Not C#, Not Java please!): PS: The program I'm using is Visual Studio just to be aware of the format. And I have provided all informations already! Please finish step 1, 2, 3, 4. Code is the correct format of C++ code. a. Constructors and operator = b. Destructor c. Text operations (length, subscript, clear) 1. Implement the aforementioned operations in the Text ADT...