USING C++
Topics:
Friend functions
Copy constructor
-----------------------------------------------------------------------------------------------------------------------------------------
Lab 3.1
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 object on the stack with the following information:
uld – Container
abbreviation - AYK
uldid – AYK68943IB
aircraft - 737
weight – 1654 Kilograms
destination – PDX
Output the contents of your object.
-------------------------------------------------------------------------------------------------------------------------------------
Lab 3.2
Utilizing Lab 3.1 code, add a copy constructor.
Create an object on the stack using the following data:
uld – Container
abbreviation - ABB
uldid – ABB31545IB
aircraft - 737
weight – 1156
destination – BUR
Copy the first object using a copy constructor. Output the contents of both objects.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Lab 2.2 is allowed to be used for construction
#include <iostream>
#include <cstdlib>
#define MAX 100
using namespace std;
class Cargo
{
string uld;
string abbreviation;
string uldid;
int aircraft;
int weight;
string destination;
bool valid(int no, int min, int max)
{
if(no >= min && no <= max)
return true;
else
return false;
}
public:
void input()
{
int type, abb;
do
{
cout<<"\n 1 - Container \t 2 - Pallet";
cout<<"\n Enter the Unit Load: ";
cin>>type;
if(valid(type, 1, 2))
{
if(type == 1)
uld = "Container";
else
uld = "Pallet";
break;
}
else
cout<<"\n ERROR: Invalid type Unit Load. Try again.";
}while(true);
do
{
if(type == 1)
{
cout<<"\n Container type: \t 1 - AYF \t 2 - AYK \t 3 - AAA \t
4 - AYY ";
cout<<"\n Enter the Abbreviation: ";
cin>>abb;
if(valid(abb, 1, 4))
{
if(abb == 1)
abbreviation = "AYF";
else if(abb == 2)
abbreviation = "AYK";
else if(abb == 3)
abbreviation = "AAA";
else if(abb == 4)
abbreviation = "AYY";
break;
}
else
cout<<"\n ERROR: Invalid type Abbreviation. Try
again.";
}
else if(type == 2)
{
cout<<"\n Pallets type: \t 1 - PAG \t 2 - PMC \t 3 - PLA
";
cout<<"\n Enter the Abbreviation: ";
cin>>abb;
if(valid(abb, 1, 3))
{
if(abb == 1)
abbreviation = "PAG";
else if(abb == 2)
abbreviation = "PMC";
else if(abb == 3)
abbreviation = "PLA";
// Come out of the loop
break;
}
else
cout<<"\n ERROR: Invalid type Abbreviation. Try
again.";
}
}while(true);
do
{
string uid;
cout<<"\n Enter the Unit ID (5 digit): ";
cin>>uid;
// Checks if length of the uid is 5 then valid
if(uid.length() == 5)
{
if(type == 1)
{
if(abb == 1)
uldid = "AYF" + uid + "IB";
else if(abb == 2)
uldid = "AYK" + uid + "IB";
else if(abb == 3)
uldid = "AAA" + uid + "IB";
else
uldid = "AYY" + uid + "IB";
}
else
{
if(abb == 1)
uldid = "PAG" + uid + "IB";
else if(abb == 2)
uldid = "PMC" + uid + "IB";
else
uldid = "PLA" + uid + "IB";
}
break;
}
else
cout<<"\n ERROR: Invalid Unit ID. Try again.";
}while(true);
cout<<"\n Enter Aircraft type: ";
cin>>aircraft;
cout<<"\n Enter Weight: ";
cin>>weight;
do
{
cout<<"\n Enter Destination (3 characters): ";
cin>>destination;
if(destination.length() == 3)
break;
else
cout<<"\n ERROR: Invalid Destination. Try again.";
}while(true);
}
void output()
{
cout<<"\n ******************";
cout<<"\n\t Unit Load: "<<uld;
cout<<"\n\t Abbreviation: "<<abbreviation;
cout<<"\n\t Unit ID: "<<uldid;
cout<<"\n\t Aircraft Type: "<<aircraft;
cout<<"\n\t Weight: "<<weight;
cout<<"\n\t Destination: "<<destination;
cout<<"\n ******************";
}
};
int main()
{
Cargo cargo[MAX];
int counter = 0;
int choice;
do
{
cout<<"\n ******** MENU **********";
cout<<"\n\t 1 - Input Cargo Information \n\t 2 - Display
Cargo Information \n\t 3 - Exit";
cout<<"\n\t\t What is your choice? ";
cin>>choice;
switch(choice)
{
case 1:
if(counter == MAX)
cout<<"\n ERROR: Full List. Can not add.";
else
{
cargo[counter++].input();
}
break;
case 2:
if(counter == 0)
cout<<"\n ERROR: Nothing to display. Empty list.";
else
{
for(int c = 0; c < counter; c++)
cargo[c].output();
}
break;
case 3:
cout<<"\n\t\t Thanks.";
exit(0);
default:
cout<<"\n Invalid choice!!";
}
}while(true);// End of do - while loop
return 0;
}
Here is the solution to above problem in C++. PLEASE READ THE CODE COMMENTS FOR MORE INFORMATION GIVE A THUMBS UP!!!
See the bold lines in code for changes
C++ CODE
#include <iostream>
#include <cstdlib>
#define MAX 100
using namespace std;
class Cargo
{
string uld;
string abbreviation;
string uldid;
int aircraft;
int weight;
string destination;
bool valid(int no, int min, int max)
{
if(no >= min && no <= max)
return true;
else
return false;
}
public:
//kilogram to pounds conevertor
friend KiloToPounds(Cargo &c)
{
//1 kilo has 2.2 pounds
c.weight = c.weight*2.2;
}
//default constructor
Cargo()
{
uld = "";
abbreviation = "";
uldid = "";
aircraft= 0;
weight= 0;
destination = "";
}
//copy constructor
Cargo(Cargo &c)
{
uld = c.uld;
abbreviation = c.abbreviation;
uldid = c.uldid;
aircraft= c.aircraft;
weight= c.weight;
destination = c.destination;
}
void input()
{
int type, abb;
do
{
cout<<"\n 1 - Container \t 2 - Pallet";
cout<<"\n Enter the Unit Load: ";
cin>>type;
if(valid(type, 1, 2))
{
if(type == 1)
uld = "Container";
else
uld = "Pallet";
break;
}
else
cout<<"\n ERROR: Invalid type Unit Load. Try again.";
}while(true);
do
{
if(type == 1)
{
cout<<"\n Container type: \t 1 - AYF \t 2 - AYK \t 3 - AAA \t 4 - AYY ";
cout<<"\n Enter the Abbreviation: ";
cin>>abb;
if(valid(abb, 1, 4))
{
if(abb == 1)
abbreviation = "AYF";
else if(abb == 2)
abbreviation = "AYK";
else if(abb == 3)
abbreviation = "AAA";
else if(abb == 4)
abbreviation = "AYY";
break;
}
else
cout<<"\n ERROR: Invalid type Abbreviation. Try
again.";
}
else if(type == 2)
{
cout<<"\n Pallets type: \t 1 - PAG \t 2 - PMC \t 3 - PLA
";
cout<<"\n Enter the Abbreviation: ";
cin>>abb;
if(valid(abb, 1, 3))
{
if(abb == 1)
abbreviation = "PAG";
else if(abb == 2)
abbreviation = "PMC";
else if(abb == 3)
abbreviation = "PLA";
// Come out of the loop
break;
}
else
cout<<"\n ERROR: Invalid type Abbreviation. Try
again.";
}
}while(true);
do
{
string uid;
cout<<"\n Enter the Unit ID (5 digit): ";
cin>>uid;
// Checks if length of the uid is 5 then valid
if(uid.length() == 5)
{
if(type == 1)
{
if(abb == 1)
uldid = "AYF" + uid + "IB";
else if(abb == 2)
uldid = "AYK" + uid + "IB";
else if(abb == 3)
uldid = "AAA" + uid + "IB";
else
uldid = "AYY" + uid + "IB";
}
else
{
if(abb == 1)
uldid = "PAG" + uid + "IB";
else if(abb == 2)
uldid = "PMC" + uid + "IB";
else
uldid = "PLA" + uid + "IB";
}
break;
}
else
cout<<"\n ERROR: Invalid Unit ID. Try again.";
}while(true);
cout<<"\n Enter Aircraft type: ";
cin>>aircraft;
cout<<"\n Enter Weight: ";
cin>>weight;
char weightType;
//ask user if weight is in kilograms
cout<<"Weight in kilograms (Y/N): ";
cin>>weightType;
//if yes convert it to pounds
if(weightType=='y'||weightType=='Y')
KiloToPounds(*this);
do
{
cout<<"\n Enter Destination (3 characters): ";
cin>>destination;
if(destination.length() == 3)
break;
else
cout<<"\n ERROR: Invalid Destination. Try again.";
}while(true);
}
void output()
{
cout<<"\n ******************";
cout<<"\n\t Unit Load: "<<uld;
cout<<"\n\t Abbreviation: "<<abbreviation;
cout<<"\n\t Unit ID: "<<uldid;
cout<<"\n\t Aircraft Type: "<<aircraft;
cout<<"\n\t Weight: "<<weight<<"
Kilograms";
cout<<"\n\t Destination: "<<destination;
cout<<"\n ******************";
}
};
int main()
{
Cargo cargo[MAX];
int counter = 0;
int choice;
do
{
cout<<"\n ******** MENU **********";
cout<<"\n\t 1 - Input Cargo Information \n\t 2 - Display
Cargo Information \n\t 3 - Exit";
cout<<"\n\t\t What is your choice? ";
cin>>choice;
switch(choice)
{
case 1:
if(counter == MAX)
cout<<"\n ERROR: Full List. Can not add.";
else
{
cargo[counter++].input();
}
break;
case 2:
if(counter == 0)
cout<<"\n ERROR: Nothing to display. Empty list.";
else
{
for(int c = 0; c < counter; c++)
cargo[c].output();
}
break;
case 3:
cout<<"\n\t\t Thanks.";
exit(0);
default:
cout<<"\n Invalid choice!!";
}
}while(true);// End of do - while loop
return 0;
}
SCREENSHOT OF OUTPUT
Get Answers For Free
Most questions answered within 1 hours.