ok basically what my program is about. I am to create a program that allows users to input aircraft/cargo details, add the cargo to the aircraft(up to 20), and a function to display all the cargo on that aircraft. So the main problem i am getting is with the listing of cargo on the aircraft. Since the cargo data is private, I am not sure how the function which is under Aircraft class is able to get the cargo info and display it.

#include <iostream>
#include <cstring>
using namespace std;

const int LIST_SIZE = 20;


/*
  Classes
*/
class Cargo
{
      public:                                                  
             Cargo();
             void setCargo(string, int, int, int, int, int);
             int volWeight;//volumetric weight of cargo            
                           
      private:
             string cargoId;//cargo id
             int weight;//cargo weight            
             int height;//cargo height
             int width;//cargo width
             int length;//cargo length
             
             
};


class Aircraft
{
      public:
             Aircraft();
             void setAircraft(string, int, int);               
             void listAircraft();  
             void listCargo(Cargo);               
             bool addCargo(Cargo);
             Cargo cargoList[LIST_SIZE];//array list of cargo
             
      private:
              string planeId;//aircraft ID
              int weightCap;//weight capacity             
              int cargoCount;//counter for cargo
               
};


/*
  Constructors
*/
Aircraft::Aircraft ()
{
     planeId = "string1" ;
     weightCap = 0;
          
}

Cargo::Cargo ()
{
     cargoId = "string2" ;
     weight = 0;
     height = 0;
     width = 0;
     length = 0;
     volWeight = 0;

}


/*
  Initialise Aircraft data
*/
void Aircraft::setAircraft(string pID, int wCap, int)
{
     planeId = pID;
     weightCap = wCap;
     cargoCount = 0 ;
}


/*
  Initialise Cargo data
*/
void Cargo::setCargo(string cID, int wt, int h, int w, int l, int vWeight)
{
     cargoId = cID;
     weight = wt;
     height = h;
     width = w;
     length = l;
     volWeight = vWeight;
     
}


/*
  Functions 
*/
void createAircraft(Aircraft& air1);
void createCargo(Cargo& cargo1);


/*
  MAIN
*/
int main()
{   
    int menu = 0;
        
    Aircraft air1;
    Cargo cargo1;
    
    while (menu != 4)
    {  
               
         cout << "Enter menu option: " << endl;
         cout << "1.Create new aircraft/cargo " << endl;
         cout << "2.List aircraft/cargo " << endl;
         cout << "3.Add cargo to aircraft " << endl;
         cout << "4.Quit " << endl;
    
         cin >> menu;
         cin.ignore (100, '\n');
       
         switch (menu)
         {
                 case 1: createAircraft(air1);
                         createCargo(cargo1);
                         break;
                         
                         
                 case 2: air1.listAircraft();
                         air1.listCargo(cargo1);
                         break;
                      
                 case 3: air1.addCargo(cargo1);
                         if (air1.addCargo(cargo1) == true)
                            cout << "cargo added" << endl;
                         else 
                            cout << "cargo cannot be added" << endl;
                         break;
                 
                 case 4: exit(1);
                 
                 default: cout << "Enter 1-4 only" << endl;
                 
          }
    }         
     
    system("pause");
    return 0;   
}


/*
  Create New Aircraft 
*/
void createAircraft(Aircraft& air1)
{
     string pID;
     int wCap;
     
     cout << "Enter aircraft ID: " << endl;
     cin >> pID;
     
     cout << "Enter weight capacity: " << endl;
     cin >> wCap;       
    
     air1.setAircraft(pID, wCap,0 );
     
     
}


/*
  Create new Cargo 
*/
void createCargo(Cargo& cargo1)
{    
     string cID;
     int wt, h, w, l, vWeight;
               
     cout << "Enter cargo ID: " << endl;
     cin >> cID;
     
     cout << "Enter cargo weight: " << endl;
     cin >> wt;
     
     cout << "Enter height: " << endl;
     cin >> h;
     
     cout << "Enter width: " << endl;
     cin >> w;
         
     cout << "Enter Length: " << endl;
     cin >> l;
     
     vWeight = (h * w * l)/6000;
     
     cargo1.setCargo(cID, wt, h, w, l, vWeight);
     
}
     

/*
  Add Cargo to Aircraft 
*/
bool Aircraft::addCargo (Cargo cargo1)
{    
     if (cargoCount < LIST_SIZE)
     {
           if (cargo1.volWeight < weightCap)
           {
            
            for (int i = 0; i < LIST_SIZE - 1; i++)
                {
                     cargoList[i] = cargo1;
                     cargoCount++;
               }
             return true;
           }
     }
     else
         return false;
}


/*
  List Aircraft
*/
void Aircraft::listAircraft()
{
     cout << "Aircraft ID: " << planeId << endl;
     cout << "Weight Capacity: " << weightCap << endl;
     
}


/*
  List Cargo 
*/
void Aircraft::listCargo(Cargo cargo1)
{
     for (int i = 0; i < cargoCount; i++)
     {
           cout << cargoList[i].cargoID << endl;
          //cout << cargoList[i] << endl;
     }     
     
}

Recommended Answers

All 6 Replies

In a general sense, your cargo class could have a display method, and the aircraft's display method would simply call that for each cargo item in the list.

You can have functions in Cargo that return the details and don't change any value.

class Cargo
{
public:
	int get_weight() const {return weight;} // the const here means that this function cannot change anything from the class
	int get_height() const {return height;}
	int get_width() const {return width;}
	/* . . . */
private:
	int weight, height, width;
};

Or if you don't like it like this:

class Cargo
{
public:
	int get_weight() const; // the const here means that this function cannot change anything from the class
	int get_height() const;
	int get_width() const;
	/* . . . */
private:
	int weight, height, width;
};

inline int Cargo::get_height() const // use inline for efficiency
{
	return weight;
}
/* . . . . */

ok so...i did something about the display functions and got this but it doesnt seem to work....
can someone explain how a function from another class calls a function for a diff class??....coz i read vmanes reply but i dont know how that works....im kinda new to classes and such...

void Aircraft::listCargo()
{
    
    for (int i = 0; i < cargoCount; i++)
    {
       cout << cargoList[i].displayCargo() << endl;
      
    }  
     
}


void Cargo::displayCargo()
{
     cout << "Cargo ID: " << cargoId << endl;
     cout << "Volumetric weight: " << volWeight << endl;
     
}

Your error is not related to classes here:

You are trying to display a function(displayCargo()) that returns void. Try this:

void Aircraft::listCargo()
{
    
    for (int i = 0; i < cargoCount; i++)
    {
       cargoList[i].displayCargo();
      
    }  
     
}


void Cargo::displayCargo()
{
     cout << "Cargo ID: " << cargoId << endl;
     cout << "Volumetric weight: " << volWeight << endl;
     
}

oh dang didnt notice i used cout twice...must be the stress...

and its working now :) thanks all for the help

You're welcome.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.