In: Computer Science
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.
Here is the solution to above problem in C++. PLEASE GIVE A THUMBS UP!!!
Read the code comments for more information
LAB 3.1 SOLUTION
C++ CODE
#include<iostream>
#include<string>
using namespace std;
//solution to lab 3.1
class Item
{
//class item members
public:
string uld;
string abbreviation;
string uldid;
int aircraft;
double weight;
string destination;
//print the object contents;
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 function kilotopound
friend void kilotopound(Item );
};
void kilotopound(Item I)
{ //weight*2.2 = weight in pounds
cout<<"Weight in pounds is:
"<<I.weight*2.2<<" Pounds "<<endl;
}
int main()
{
//this will create item on the stack , new
keyword
//declares memory on the heap
Item I;
//setting all values for the members
I.uld="Container";
I.uldid="AYK68943IB";
I.abbreviation="AYK";
I.aircraft=737;
I.weight=1654;
I.destination="PDX";
//printing data
I.print();
//printing weight in pounds using kilopounds
kilotopound(I);
return 0;
}
SCREENSHOT OF OUTPUT
LAB 3.2 Solution
C++ CODE
#include<iostream>
#include<string>
using namespace std;
//solution to lab 3.1
class Item
{
//class item members
public:
string uld;
string abbreviation;
string uldid;
int aircraft;
double weight;
string destination;
//default constructor
Item()
{
}
//copy constructor
Item(Item & i)
{
uld = i.uld;
abbreviation= i.abbreviation;
uldid=i.uldid;
aircraft=i.aircraft;
weight=i.weight;
destination=i.destination;
}
//print the object contents;
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 function kilotopound
friend void kilotopound(Item );
};
void kilotopound(Item I)
{ //weight*2.2 = weight in pounds
cout<<"Weight in pounds is:
"<<I.weight*2.2<<" Pounds "<<endl;
}
int main()
{
//this will create item on the stack , new
keyword
//declares memory on the heap
Item I;
//setting all values for the members
I.uld="Container";
I.uldid="ABB";
I.abbreviation="ABB315451B";
I.aircraft=737;
I.weight=1156;
I.destination="BUR";
Item I2(I); //copy constructor
//printing data
I.print();
//printing weight in pounds using kilopounds
kilotopound(I);
cout<<"Second Object\n";
//printing data
I2.print();
//printing weight in pounds using kilopounds
kilotopound(I2);
return 0;
}
SCREENSHOT OF OUTPUT