Question: * * It seems that I cannot directly access the private data in the input function definition. The program cannot run. ( I need to

**It seems that I cannot directly access the private data in the input function definition. The program cannot run. (I need to use getters or setters but I have difficulty doing so)** The code is below:
#include
#include
using namespace std;
class Cargo {
private:
string uldtype;
string abbrev;
string uldid;
int aircraft;
double weight;
string destination;
public:
// Constructors
Cargo() : aircraft(0), weight(0.0){}
Cargo(const string& uldtype,
const string& abbrev,
const string& uldid,
int aircraft,
double weight,
const string& destination)
: uldtype(uldtype),
abbrev(abbrev),
uldid(uldid),
aircraft(aircraft),
weight(weight),
destination(destination){}
// Destructor
~Cargo(){
cout << "Cargo destructor called" << endl;
}
// Setters
void setuldtype(const string& uldtype){ this->uldtype = uldtype; }
void setabbrev(const string& abbrev){ this->abbrev = abbrev; }
void setuldid(const string& uldid){ this->uldid = uldid; }
void setaircraft(int aircraft){ this->aircraft = aircraft; }
void setweight(double weight){ this->weight = weight; }
void setdestination(const string& destination){ this->destination =
destination; }
// Getters
string getuldtype() const { return uldtype; }
string getabbrev() const { return abbrev; }
string getuldid() const { return uldid; }
int getaircraft() const { return aircraft; }
double getweight() const { return weight; }
string getdestination() const { return destination; }
// Overloaded operator ==
friend bool operator==(const Cargo& c1, const Cargo& c2){
return (c1.abbrev == c2.abbrev) && (c1.uldid == c2.uldid);
}
};
// Function prototypes
void inputCargo(Cargo& cargo);
void outputCargo(const Cargo& cargo);
// Input function definition
void inputCargo(Cargo& cargo){
cout << "Enter unit load type: ";
cin >> cargo.uldtype;
cout << "Enter unit load abbreviation: ";
cin >> cargo.abbrev;
cout << "Enter unit identifier: ";
cin >> cargo.uldid;
cout << "Enter aircraft type: ";
cin >> cargo.aircraft;
cout << "Enter unit weight (in pounds): ";
cin >> cargo.weight;
cout << "Enter destination code: ";
cin >> cargo.destination;
}
// Output function definition
void outputCargo(const Cargo& cargo){
cout <<"--------------------------------------------"<< endl;
cout << "Unit load type: "<< cargo.getuldtype()<< endl;
cout << "Unit load abbreviation: "<< cargo.getabbrev()<< endl;
cout << "Unit identifier: "<< cargo.getuldid()<< endl;
cout << "Aircraft type: "<< cargo.getaircraft()<< endl;
cout << "Unit weight: "<< cargo.getweight()<<" pounds" << endl;
cout << "Destination code: "<< cargo.getdestination()<< endl;
cout <<"--------------------------------------------"<< endl;
}
int main(){
Cargo unit1("PAG", "PAG32597","IB737",3321, "SEA");
Cargo unit2= unit1; // Copy constructor
Cargo unit3; // Default constructor
cout << "Unit 1:"<< endl;
outputCargo(unit1);
cout << "Unit 2:"<< endl;
outputCargo(unit2);
cout << "Unit 3(default):"<< endl;
outputCargo(unit3);
if (unit1== unit2)
cout << "unit1 is the same as unit2"<< endl;
else
cout << "unit1 is not the same as unit2"<< endl;
// Test if unit2 is equal to unit3
if (unit2== unit3)
cout << "unit2 is the same as unit3"<< endl;
else
cout << "unit2 is not the same as unit3"<< endl;
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!