Question

In: Computer Science

Lab 3.1 Create your objects in the stack (not on the heap). Add a friend function,...

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.

Solutions

Expert Solution

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


Related Solutions

In this class add Comparable interface. In the driver program create a few objects and In...
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public...
In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
Write a Python program to (a) create a new empty stack. Then, (b) add items onto...
Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents:
Using the Stack ADT: Create a program that uses a stack. Your program should ask the...
Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. (Optional) Create a similar program that uses a stack. Your new program should ask the user to input a line of text and then it should print out the line of text in reverse. To do this your application should use a stack of Character. In Java...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated. a) Write code that allocates static memory (only include one declaration): b) Write code that allocates stack memory (only include one declaration): c) Write code that allocates heap memory (only include one declaration): 2) Edit the C++ program below to include...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask...
2. Using the Stack ADT: Create a program that uses a stack. Your program should ask the user to input a few lines of text and then outputs strings in reverse order of entry. In Java please.
The first part of this lab is making your stack generic. Take the code from your...
The first part of this lab is making your stack generic. Take the code from your working StringStack class and paste it into the GenericStack class. Change the name of the constructors to match the new name of the class (this has been done to this point), then modify the whole class so it uses generics, and can store any type that a programmer asks for. Until you successfully complete this, the main method will give you nasty compiler errors....
In python please Problem Create two server objects using the class create a function, biggest_server_object_sum, outside...
In python please Problem Create two server objects using the class create a function, biggest_server_object_sum, outside of the ServerClass class, that: 1. Takes the IP 2. Sums the octets of the IP together (example 127.0.0.1 = 127+0+0+1 = 128) 3. Returns the Server object with the larger sum Example: server_one = ServerClass("127.0.0.1") server_two = ServerClass("192.168.0.1") result = biggest_ip_sum(server_one, server_two) print(result) Hint Modify get_server_ip in the previous problem. -------------------- Please use this code to start class ServerClass: """ Server class for...
Python Create a move function that is only defined in the base class called Objects. The...
Python Create a move function that is only defined in the base class called Objects. The move function will take two parameters x,y and will also return the updated x,y parameters.
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework....
Implement heap sort by using the bottom-up insertion method. Add this sort to your sorting framework. Evaluate its performance in terms of the numbers of comparisons and exchanges, and compare it to the performance of the two advanced sorting methods that you have previously implemented. Submit your report with detailed empirical results and a thorough explanation of these results. Which of the three advanced sorting method is the best choice for a) ordered data, b) data in reverse order, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT