Question

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 have the information for our 737 aircraft, with the addition of maxload, which is 46000 pounds.

Child class two will have the information for our new 767 aircraft, which has the following information:

Unit and type: Container or Pallet: AKE, APE, AKC, AQP, AQF, P1P, AAP, P6P

Unit ID: Container or Pallet type: five digits + airline code; our ID code is IB, e.g. AYF12345IB

Aircraft type: Ours is a 767

Weight: The weight, in pounds, of the loaded container or pallet

Destination: A three alpha character IATA string, e.g. MFR (Medford, OR), or BUR (Burbank, CA)

Maxload: 767-300 max load is 116000 pounds

Change private to protected in the Cargo class only.

Make two classes which inherit from the Cargo class: Boeing737 and Boeing767.
Each class will need to inherit a default constructor, a copy constructor, and a constructor that has six parameters. Only one more function will be built in each class; all the rest will be inherited.
The unit and type data are different for each class as is the aircraft type. Maxload is provided for both aircraft. Our

737 can hold up to 46000 pounds, and our 767 can hold up to 116000 pounds.

Create two new global functions named load737 and load767. These are used to build a unit for Boeing737 or a Boeing767, respectively.

Revise the main function to call the input function and do nothing else.

The data file lab5data.txt is provided for your use. Your program will be tested with different data, but the format

Is guaranteed to be correctly formatted, and not to exceed 20 lines.

In the input function loop reads one line from the file each time through the loop, look at the aircraft field in the record and call the corresponding build function to build that type of unit. Before calling the appropriate build function, print a header giving the sequence number of the unit read, with the number 1 for the first unit and incremented for each successive unit. For both child classes, ensure that the units are compatible with the aircraft. For example, the 737 cannot hold an AQF type container. Ensure that the weight totals for both types of aircraft are not exceeded by the units assigned to them. If they are at or near capacity, check to ensure that another unit would not put them over the weight limit, if it would, then reject the unit and move on to the next unit in the file.

Store each unit object in an array of objects of the appropriate type, either for the 737 or the 767.

Once input has completed, you will have a number of objects of two different types stored in different arrays.  

Use output to send the contents of each array’s objects to the screen, taking care to output all of the 737 units

together and all of the 767 units together as well as the weight totals for both aircraft.

* Use the lab5data.txt file which will contain data similar to

   the following three lines of data; lab5data.txt will have no

   more than 20 lines of data

Pallet PAG PAG45982IB 737 4978 OAK

Container AYF AYF23409AA 767 2209 LAS

Container AAA AAA89023DL 767 5932 DFW

      * All weights are in pounds, don’t worry about kilograms.

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

        

Hint: you can do this with the following while statement:

                while(inputFile.peek() != EOF)

                

        The peek function will return EOF is there is no next character.

      * At the bottom of the input function, close the file.

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

4.2

Use 4.2 Lab Code:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class Cargo {

public:
string uld;
string abbreviation;
string uldid;
int aircraft;
double weight;
string destination;


Cargo(string uld,string abbreviation,string uldid,int aircraft,double weight,string destination)
{
this->uld = uld;
this->abbreviation = abbreviation;
this->uldid = uldid;
this->aircraft = aircraft;
this->weight = weight;
this->destination = destination;
}

Cargo(Cargo& 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 << " pounds" << endl;
cout << "destination -- " << destination << endl;
}

//friend void kilotopound(Cargo);

friend bool operator==(const Cargo& item1, const Cargo& item2);

};
/**
void kilotopound(Cargo I)
{
cout << "Weight in pounds is: " << I.weight * 2.2 << " Pounds " << endl;
}
*/

bool operator==(const Cargo& item1, const Cargo& item2)
{
return item1.abbreviation == item2.abbreviation && item1.uldid == item2.uldid;
}

void input()
{
// variable to take input from file
string uld, abbreviation, uldid, destination;
int aircraft;
double weight;

// opening file in read mode
fstream file("cardata4.txt", ios::in);
// if unable to read file then display error
if(!file.is_open())
{
cout << "Unable to open file! \nAborting program execution.\n";
return;
}
// reading line by line from file until EOF is reached
cout << "Cargo Details in input file :\n\n";
while(file >> uld >> abbreviation >> uldid >> aircraft >> weight >> destination){
// creating a cargo object with the read values
Cargo temp(uld, abbreviation, uldid, aircraft, weight, destination);
// printing the details
temp.print();
cout << endl;
}
cout << endl << "Reading Cargo details from file successfully finished!\n";
// closing file after successful reading
file.close();
}

int main()
{

/*Cargo unit1;
unit1.uld = "Pallet";
unit1.abbreviation = "PAG";
unit1.uldid = "PAG32597IB";
unit1.aircraft = 737;
unit1.weight = 3321;
unit1.destination = "SEA";
Cargo unit2(unit1);
Cargo unit3;

if (unit1 == unit2)
cout << "\nunit1 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";
*/

input();
return 0;
}

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

C++ code given below. In this program Cargo is the main class and Boeing737 and Boeing 767 are the subclasses.

Read data from the file using getline function. Then split the words by using split()(User defined) . Create the object of class Boeing737 if aircraft =737 otherwise create object of Boeing767. Invoke the super class constructer at the time of object creation. Then add the object into the object array. two seperate arrays are used for each subclasses. For calculating the total weight using static variable.(because i an done the calculation in the load menthod. if we use a normal variable each tome function call value set to 0. to retain the total in wttotal make it as static). Calculate the seperate total for each aircraft.

Print function make as virtual. In both subclasses redifine the print method. At the runtime call the print method according to the object type.

Not using other methods in cargo class. So only call input function in main.

c++code

#include <iostream>
#include <string>
#include <fstream>
#include <bits/stdc++.h> 
using namespace std;
void split(string str);
void load737(string uld,string abbreviation,string uldid,int aircraft,double weight,string destination);
void load767(string uld,string abbreviation,string uldid,int aircraft,double weight,string destination);
void input();
class Cargo
{
    protected:
string uld;
string abbreviation;
string uldid;
int aircraft;
double weight;
string destination;
public:
Cargo()
{
    
}

Cargo(string uld,string abbreviation,string uldid,int aircraft,double weight,string destination)
{
this->uld = uld;
this->abbreviation = abbreviation;
this->uldid = uldid;
this->aircraft = aircraft;
this->weight = weight;
this->destination = destination;
}

Cargo(Cargo& i)
{
uld = i.uld;
abbreviation = i.abbreviation;
uldid = i.uldid;
aircraft = i.aircraft;
weight = i.weight;
destination = i.destination;
}

virtual void print()
{
cout << "Unit -- " << uld << endl;
cout << "Type -- " << abbreviation << endl;
cout << "Unit Id -- " << uldid << endl;
cout << "aircraft type -- " << aircraft << endl;
cout << "weight -- " << weight << " pounds" << endl;
cout << "destination -- " << destination << endl;
}
friend bool operator==(const Cargo& item1, const Cargo& item2);    
};

class Boeing737:public Cargo
{
    int maxload;
    public:
    Boeing737(){}
    Boeing737(string uld,string abbreviation,string uldid,int aircraft,double weight,string destination):Cargo(uld,abbreviation,uldid,aircraft,weight,destination)
    {
        maxload=46000;
    }
    void print()
    {
    cout<<endl<<"Boeing737"<<endl;
    cout << "Unit -- " << uld << endl;
    cout << "Type -- " << abbreviation << endl;
    cout << "Unit Id -- " << uldid << endl;
    cout << "aircraft type -- " << aircraft << endl;
    cout << "weight -- " << weight << " pounds" << endl;
    cout << "destination -- " << destination << endl;
    }
    
};

class Boeing767:public Cargo
{
    int maxload;
    public:
    Boeing767()
    {
        
    }
    
    Boeing767(string uld,string abbreviation,string uldid,int aircraft,double weight,string destination):Cargo(uld,abbreviation,uldid,aircraft,weight,destination)
    {
       maxload=116000; 
    }
    void print()
    {
        
    cout<<endl<<"Boeing767"<<endl;
    cout << "Unit -- " << uld << endl;
    cout << "Type -- " << abbreviation << endl;
    cout << "Unit Id -- " << uldid << endl;
    cout << "aircraft type -- " << aircraft << endl;
    cout << "weight -- " << weight << " pounds" << endl;
    cout << "destination -- " << destination << endl;
    }
    
};

bool operator==(const Cargo& item1, const Cargo& item2)
{
return item1.abbreviation == item2.abbreviation && item1.uldid == item2.uldid;
}
void input()
{
    ifstream inputFile;
    string str;
    int unitNo=0;
   inputFile.open("lab5data.txt");
    if(!inputFile)
    {
        std::cerr << "Error opening the file" << endl;
        exit(0);
    }
    while (inputFile.peek() != EOF)
    {
        unitNo++;
               cout<<endl<<unitNo<<endl;
       while(inputFile.peek() == ' '){
            inputFile.get();
       }
       getline( inputFile, str );
      
       split(str);
    }
}
void split(string str)
{
     string uld, abbreviation, uldid, destination;
    int aircraft;
    double weight;
    //cout<<str<<endl;
    istringstream ss(str); 
       while( ss >> uld >> abbreviation >> uldid >> aircraft >> weight >> destination){
               
           if(aircraft==737 )
           {  
               if((abbreviation!="AKE"||abbreviation!="APE"||abbreviation!="AKC"
            ||abbreviation!="AQP"||abbreviation!="AQF"||abbreviation!="P1P"||abbreviation!="AAP"
            ||abbreviation!="P6P"))
            {
               load737(uld,abbreviation,uldid,aircraft,weight,destination);
            }
           }
           else if(aircraft==767)
           {
               //Not sure about the condition needed or not 
            if((abbreviation=="AKE"||abbreviation=="APE"||abbreviation=="AKC"
            ||abbreviation=="AQP"||abbreviation=="AQF"||abbreviation=="P1P"
            ||abbreviation=="AAP"||abbreviation== "P6P"))
              {
               load767(uld,abbreviation,uldid,aircraft,weight,destination);
              }
           }
       }
}
void load737(string uld,string abbreviation,string uldid,int aircraft,double weight,string destination)
{
    static int wttotal=0;
    int i=0;
    Boeing737 objArray[20];
     wttotal+=weight;
     if(wttotal>46000)
     {
         cout<<"not able to load";
         wttotal=wttotal-weight;
     }
     else
     { 
         Boeing737 obj(uld,abbreviation,uldid,aircraft,weight,destination);
         objArray[i]=obj;
         i++;
     }
     for(int j=0;j<i;j++)
     {
         objArray[j].print();
     }
     
     cout<<"Total Weight: "<<wttotal<<" pounds"<<endl;
    
    
}
void load767(string uld,string abbreviation,string uldid,int aircraft,double weight,string destination)
{
   static int wttotal7=0;
    int i=0;
    Boeing767 objArray[20];
     wttotal7+=weight;
     if(wttotal7>116000)
     {
         cout<<"not able to load";
         wttotal7=wttotal7-weight;
     }
     else
     { 
         Boeing767 obj(uld,abbreviation,uldid,aircraft,weight,destination);
         objArray[i]=obj;
         i++;
     }
     for(int j=0;j<i;j++)
     {
         objArray[j].print();
     }
    cout<<"Total Weight: "<<wttotal7<<" pounds"<<endl;
}



int main()
{
    input();

    return 0;
}

input

1 Pallet PAG PAG459821B 737 4978 OAK 2 Container APE APE23409AA 767 2209 LAS 3 Container AAP AAP89023DL 767 5932 DFW

output

1 Boeing 737 Unit Pallet Type PAG Unit Id PAG459821B aircraft type 737 weight 4978 pounds destination -- OAK Total Weight: 49

program screenshot.

1 #include <iostream> 2 #include <string> 3 #include <fstream> 4 #include <bits/stdc++.h> 5 using namespace std; 6 void split

30 Cargo(Cargo& i) 31 - { 32 uld = i.uld; 33 abbreviation i.abbreviation; 34 uldid i.uldid; 35 aircraft i.aircraft; 36 weight} 58 { 59 cout<<endl<<Boeing 737<<endl; 60 cout << Unit -- << uld << endl; 61 cout << Type << abbreviation << endl; 62بها 86 pool operator==(const Cargo& item1, const Cargo& item2) 87* { 88 return item1.abbreviation == item2.abbreviation && it{ { 115 string uld, abbreviation, uldid, destination; 116 int aircraft; 117 double weight; 118 //cout<<str<<endl; 119 istring{ } { 143 void load737(string uld, string abbreviation, string uldid, int aircraft, double weight, string destination) 144 {} 171 if(wttotal7>116000) 172 { 173 cout<<not able to load; 174 wttotal7=wttotal7-weight; 175 176 else{ 177 Boeing767 obj(u

Add a comment
Know the answer?
Add Answer to:
Lab 5.1 C++ Utilizing the code from Lab 4.2, replace your Cargo class with a new...
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 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...

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

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

  • In this lab, you will need to implement the following functions in Text ADT with C++...

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

  • I need help finding the error in my code for my Fill in the Blank (Making a Player Class) in C++. Everything looks good...

    I need help finding the error in my code for my Fill in the Blank (Making a Player Class) in C++. Everything looks good to me but it will not run. Please help... due in 24 hr. Problem As you write in your code, be sure to use appropriate comments to describe your work. After you have finished, test the code by compiling it and running the program, then turn in your finished source code. Currently, there is a test...

  • I need help with this assignment in C++, please! *** The instructions and programming style detai...

    I need help with this assignment in C++, please! *** The instructions and programming style details are crucial for this assignment! Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...

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