Question

CIS 22B Lab 3 Itty Bitty Airfreight (IBA) Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1...

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 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 LAB CODE 2.2:

#include <iostream>
#include <cstdlib>
#define MAX 100
using namespace std;

// Defines a class to store cargo information
class Cargo
{
string uld;
string abbreviation;
string uldid;
int aircraft;
int weight;
string destination;

// Function to return true if the parameter no is within second parameter min and third parameter max
// otherwise returns false
bool valid(int no, int min, int max)
{
// Checks if no is greater than or equals to min and less than or equals to max return true
if(no >= min && no <= max)
return true;
// Otherwise returns false
else
return false;
}// End of function
public:
// Function to accept cargo information from the user
void input()
{
int type, abb;
// Loops till valid unit load entered by the user
do
{
// Displays menu
cout<<"\n 1 - Container \t 2 - Pallet";
// Accepts unit load
cout<<"\n Enter the Unit Load: ";
cin>>type;

// Calls the function to validate input type
if(valid(type, 1, 2))
{
// Checks the type and assigns appropriate constant
if(type == 1)
uld = "Container";
else
uld = "Pallet";

// Come out of the loop
break;
}// End of if condition

// Otherwise invalid data displays error message and continue
else
cout<<"\n ERROR: Invalid type Unit Load. Try again.";
}while(true);// End of do - while loop

// Loops till valid container type entered by the user
do
{
// Checks if type is 1 then Container
if(type == 1)
{
// Displays menu
cout<<"\n Container type: \t 1 - AYF \t 2 - AYK \t 3 - AAA \t 4 - AYY ";
// Accepts abbreviation
cout<<"\n Enter the Abbreviation: ";
cin>>abb;
// Calls the function to validate input type
if(valid(abb, 1, 4))
{
// Checks the abb and assigns appropriate constant
if(abb == 1)
abbreviation = "AYF";
else if(abb == 2)
abbreviation = "AYK";
else if(abb == 3)
abbreviation = "AAA";
else if(abb == 4)
abbreviation = "AYY";
// Come out of the loop
break;
}// End of inner if condition

// Otherwise invalid data displays error message and continue
else
cout<<"\n ERROR: Invalid type Abbreviation. Try again.";
}// End of if outer condition

// Checks if type is 2 then Pallets
else if(type == 2)
{
// Displays menu
cout<<"\n Pallets type: \t 1 - PAG \t 2 - PMC \t 3 - PLA ";
cout<<"\n Enter the Abbreviation: ";
cin>>abb;
// Calls the function to validate input type
if(valid(abb, 1, 3))
{
// Checks the abb and assigns appropriate constant
if(abb == 1)
abbreviation = "PAG";
else if(abb == 2)
abbreviation = "PMC";
else if(abb == 3)
abbreviation = "PLA";
// Come out of the loop
break;
}// End of if condition

// Otherwise invalid data displays error message and continue
else
cout<<"\n ERROR: Invalid type Abbreviation. Try again.";
}// End of else if condition
}while(true);// End of do - while loop

// Loops till valid unit id entered by the user
do
{
string uid;
// Accepts 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)
{
// Checks if type is 1 then Container
if(type == 1)
{
// Checks the abb and concatenate constant with uid accordingly
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";
}// End of inner if condition

// Otherwise type is 2 for Pallets
else
{
// Checks the abb and concatenate constant with uid accordingly
if(abb == 1)
uldid = "PAG" + uid + "IB";
else if(abb == 2)
uldid = "PMC" + uid + "IB";
else
uldid = "PLA" + uid + "IB";
}// End of else
// Come out of the loop
break;
}// End of outer if condition

// Otherwise invalid data displays error message and continue
else
cout<<"\n ERROR: Invalid Unit ID. Try again.";
}while(true);// End of do - while loop

cout<<"\n Enter Aircraft type: ";
cin>>aircraft;

cout<<"\n Enter Weight: ";
cin>>weight;

// Loops till valid destination entered by the user
do
{
// Accepts destination
cout<<"\n Enter Destination (3 characters): ";
cin>>destination;

// Checks if length of the destination is 3 then valid
if(destination.length() == 3)
// Come out of the loop
break;

// Otherwise invalid data displays error message and continue
else
cout<<"\n ERROR: Invalid Destination. Try again.";
}while(true);// End of do - while loop
}// End of function

// Function to display cargo information
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 ******************";
}// End of function
};// End of structure

// main function definition
int main()
{
// Creates an array object of class cargo
Cargo cargo[MAX];
// To store number of records
int counter = 0;
// To store user choice
int choice;

// Loops till user choice is not 3
do
{
// Displays menu
cout<<"\n ******** MENU **********";
cout<<"\n\t 1 - Input Cargo Information \n\t 2 - Display Cargo Information \n\t 3 - Exit";
// Accepts user choice
cout<<"\n\t\t What is your choice? ";
cin>>choice;

// Checks user choice and calls appropriate function
switch(choice)
{
case 1:
// Checks if current record counter is equals to MAX then display error message
// list full
if(counter == MAX)
cout<<"\n ERROR: Full List. Can not add.";

else
{
// Calls the function to accept cargo data
cargo[counter++].input();
}// End of else
break;
case 2:
// Checks if current record counter is equals to 0 then display error message
// empty list
if(counter == 0)
cout<<"\n ERROR: Nothing to display. Empty list.";
else
{
for(int c = 0; c < counter; c++)
// Calls the function to display data
cargo[c].output();
}// End of else
break;
case 3:
cout<<"\n\t\t Thanks.";
exit(0);
default:
cout<<"\n Invalid choice!!";
}// End of switch - case
}while(true);// End of do - while loop
return 0;
}// End of main function

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Short Summary:

  • Cargo cargo[MAX] would create objects in the stack memory.  
  • The following would create objects in heap memory.

Cargo* cargo = new Cargo[MAX];

  • Added friend function kilotopound and copy constructors.
  • Changes made to the code are given in BOLD.
  • Provided the output.

SOURCE CODE:

#include <iostream>
#include <cstdlib>
#define MAX 100
using namespace std;

// Defines a class to store cargo information
class Cargo
{
string uld;
string abbreviation;
string uldid;
int aircraft;
int weight;
string destination;
  
// Function to return true if the parameter no is within second parameter min and third parameter max
// otherwise returns false
bool valid(int no, int min, int max)
{
// Checks if no is greater than or equals to min and less than or equals to max return true
if(no >= min && no <= max)
return true;
// Otherwise returns false
else
return false;
}// End of function
  
public:
  
// default constructor, does nothing
Cargo(){
}
  
// Copy constructor
Cargo(const Cargo &c2) {
uld = c2.uld;
abbreviation = c2.abbreviation;
uldid = c2.uldid;
aircraft = c2.aircraft;
weight = c2.weight;
destination = c2.destination;
}
  
// friend function, kilotopound, which will convert kilograms to pounds
friend int kilotopound(Cargo c)
{
// return pounds
return (c.weight * 2.2);
}
  

  
// Function to accept cargo information from the user
void input()
{
int type, abb;
// Loops till valid unit load entered by the user
do
{
// Displays menu
cout<<"\n 1 - Container \t 2 - Pallet";
// Accepts unit load
cout<<"\n Enter the Unit Load: ";
cin>>type;
  
// Calls the function to validate input type
if(valid(type, 1, 2))
{
// Checks the type and assigns appropriate constant
if(type == 1)
uld = "Container";
else
uld = "Pallet";
  
// Come out of the loop
break;
}// End of if condition
  
// Otherwise invalid data displays error message and continue
else
cout<<"\n ERROR: Invalid type Unit Load. Try again.";
}while(true);// End of do - while loop
  
// Loops till valid container type entered by the user
do
{
// Checks if type is 1 then Container
if(type == 1)
{
// Displays menu
cout<<"\n Container type: \t 1 - AYF \t 2 - AYK \t 3 - AAA \t 4 - AYY ";
// Accepts abbreviation
cout<<"\n Enter the Abbreviation: ";
cin>>abb;
// Calls the function to validate input type
if(valid(abb, 1, 4))
{
// Checks the abb and assigns appropriate constant
if(abb == 1)
abbreviation = "AYF";
else if(abb == 2)
abbreviation = "AYK";
else if(abb == 3)
abbreviation = "AAA";
else if(abb == 4)
abbreviation = "AYY";
// Come out of the loop
break;
}// End of inner if condition
  
// Otherwise invalid data displays error message and continue
else
cout<<"\n ERROR: Invalid type Abbreviation. Try again.";
}// End of if outer condition
  
// Checks if type is 2 then Pallets
else if(type == 2)
{
// Displays menu
cout<<"\n Pallets type: \t 1 - PAG \t 2 - PMC \t 3 - PLA ";
cout<<"\n Enter the Abbreviation: ";
cin>>abb;
// Calls the function to validate input type
if(valid(abb, 1, 3))
{
// Checks the abb and assigns appropriate constant
if(abb == 1)
abbreviation = "PAG";
else if(abb == 2)
abbreviation = "PMC";
else if(abb == 3)
abbreviation = "PLA";
// Come out of the loop
break;
}// End of if condition
  
// Otherwise invalid data displays error message and continue
else
cout<<"\n ERROR: Invalid type Abbreviation. Try again.";
}// End of else if condition
}while(true);// End of do - while loop
  
// Loops till valid unit id entered by the user
do
{
string uid;
// Accepts 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)
{
// Checks if type is 1 then Container
if(type == 1)
{
// Checks the abb and concatenate constant with uid accordingly
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";
}// End of inner if condition
  
// Otherwise type is 2 for Pallets
else
{
// Checks the abb and concatenate constant with uid accordingly
if(abb == 1)
uldid = "PAG" + uid + "IB";
else if(abb == 2)
uldid = "PMC" + uid + "IB";
else
uldid = "PLA" + uid + "IB";
}// End of else
// Come out of the loop
break;
}// End of outer if condition
  
// Otherwise invalid data displays error message and continue
else
cout<<"\n ERROR: Invalid Unit ID. Try again.";
}while(true);// End of do - while loop
  
cout<<"\n Enter Aircraft type: ";
cin>>aircraft;
  
cout<<"\n Enter Weight: ";
cin>>weight;
  
// declare a variable to store userChoice of weight
int userWeightChoice;
  
// Prompting the user to select the unit of weight
cout <<"\nEnter 1 if weight is in kilograms or 2 if weight is in pounds: " <<endl;
// getting user userWeightChoice
cin >> userWeightChoice;

  
// if the weight choice selected by user is kilogram
if(userWeightChoice == 1){
// call friend function kilotopound
weight = kilotopound(*this);
}

  
// Loops till valid destination entered by the user
do
{
// Accepts destination
cout<<"\n Enter Destination (3 characters): ";
cin>>destination;
  
// Checks if length of the destination is 3 then valid
if(destination.length() == 3)
// Come out of the loop
break;
  
// Otherwise invalid data displays error message and continue
else
cout<<"\n ERROR: Invalid Destination. Try again.";
}while(true);// End of do - while loop
}// End of function

// Function to display cargo information
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 ******************";
}// End of function
};// End of structure

// main function definition
int main()
{
  
// Create object1
cout << "Enter values for object1" << endl;
Cargo object1;
object1.input();
  
// create ibject 2 using copy constructor
Cargo object2 = object1;
  
//print both
cout << "Object1" << endl;
object1.output();
cout << endl << "Object2 creating using copy constructor" << endl;
object2.output();
  

  
// Creates an array object of class cargo
Cargo cargo[MAX];
// To store number of records
int counter = 0;
// To store user choice
int choice;

// Loops till user choice is not 3
do
{
// Displays menu
cout<<"\n ******** MENU **********";
cout<<"\n\t 1 - Input Cargo Information \n\t 2 - Display Cargo Information \n\t 3 - Exit";
// Accepts user choice
cout<<"\n\t\t What is your choice? ";
cin>>choice;
  
// Checks user choice and calls appropriate function
switch(choice)
{
case 1:
// Checks if current record counter is equals to MAX then display error message
// list full
if(counter == MAX)
cout<<"\n ERROR: Full List. Can not add.";
  
else
{
// Calls the function to accept cargo data
cargo[counter++].input();
}// End of else
break;
case 2:
// Checks if current record counter is equals to 0 then display error message
// empty list
if(counter == 0)
cout<<"\n ERROR: Nothing to display. Empty list.";
else
{
for(int c = 0; c < counter; c++)
// Calls the function to display data
cargo[c].output();
}// End of else
break;
case 3:
cout<<"\n\t\t Thanks.";
exit(0);
default:
cout<<"\n Invalid choice!!";
}// End of switch - case
}while(true);// End of do - while loop
  
return 0;
}// End of main function

SAMPLE OUTPUT:

Enter values for objecti 1 - Container 2 - Pallet Enter the Unit Load: 1 2 - AYK 3 - AAA 4 - AYY Container type: 1 - AYF Ente

******************************************************************************

Feel free to rate the answer and comment your questions, if you have any.

Please upvote the answer and appreciate our time.

Happy Studying!!!

******************************************************************************

Add a comment
Know the answer?
Add Answer to:
CIS 22B Lab 3 Itty Bitty Airfreight (IBA) Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab 5.1 C++ Utilizing the code from Lab 4.2, replace your Cargo class with a new...

    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...

  • Hi please help with c++ programming. This is my code. there's a syntax error. PLEASE FIX...

    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>...

  • Lab 4.1 Utilizing the code from Lab 3.2, add an overloaded operator == which will be...

    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...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    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...

  • Please use my Lab 3.2 code for this assignment Lab 4.2 instruction Using the code from...

    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...

  • IN C++ My project will be a library management system. It will have seven functions. It...

    IN C++ My project will be a library management system. It will have seven functions. It HAS to contain: classes, structures, pointers and inheritance. fix code according to below 1. Add a new book allows the user to enter in book name and book code Book code has to be in # “Invalid book code” “book name has been added to library” book code will be used to borrow books 2. Add a new user must use first user must...

  • C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please...

    C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • i have created a program for a game of rock, paper, scissors but i must make...

    i have created a program for a game of rock, paper, scissors but i must make it run more than once in some kind of loop. i want to add a function named runGame that will control the flow of a single game. The main function will be used to determine which game mode to initiate or exit program in Player vs. Computer, the user will be asked to enter their name and specify the number of rounds for this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT