I’m trying to make my program to have 3 shipment objects. I want shipment 1 and 2 to be data that the user entered. Shipment 3 I would like it to be the default values but for now I can ignore it since my shipment 2 fields aren’t being copied properly. When I run my code, I can see that shipment 1 is properly being outputted out but when I use output(shipment2); the fields aren’t being copied from shipment output(shipment);. To what I understand the copy constructor is being called but nothing is being copied? I can see it’s displaying my default values which is good but it’s just not being copied.
#include<iostream>
#include<string>
using namespace std;
class cargo{
public:
void input(); //Functions
void output();
void display();
cargo(const cargo& object); //Copy const
cargo(); //Default const
void setUnit(string unitName); //Setters
void setAbbrev(string abbrevName);
void setUnitID(string unitIDName);
void setAircraft(int aircraftname);
void setWeight(double weightName);
void setDestinatoin(string destinationName);
string getUnit() const; //Getters
string getAbbrev() const;
string getUnitID() const;
int getAircraft() const;
double getWeight() const;
string getDestination() const;
friend int weightConvert(int weightName); //Weight convert function
private:
string unit; //Field
string abbrev;
string unitID;
int aircraft;
double weight;
string destination;
};
cargo::cargo(){ //Default const
unit = "N/a";
abbrev = "N/a";
unitID = "N/a";
aircraft = 0;
weight = 0;
destination = "N/a";
}
cargo::cargo(const cargo& object){ //Copy Const
unit = object.getUnit();
abbrev = object.getAbbrev();
unitID = object.getUnitID();
aircraft = object.getAircraft();
weight = object.getWeight();
destination = object.getDestination();
cout << "--Copy Constructor called--" << endl;
/*cout << "Unit load type: " << unit << endl;
cout << "Unit load abbreviation: " << abbrev << endl;
cout << "Unit ID: " << unitID << endl;
cout << "Aircraft type: " << aircraft << endl;
cout << "Weight: " << weight << endl;
cout << "Destination: " << getDestination() << endl;
cout << "--End of copy constructor--" << endl;*/
}
void cargo::input(){ //Input function, get data from user
string tempString;
int tempInt;
cout << "Is it a pallet or container" << endl;
getline(cin, tempString);
setUnit(tempString);
cout << "What is the load abbreviatoin?" << endl;
getline(cin, tempString);
setAbbrev(tempString);
cout << "What is the unitID?" << endl;
getline(cin, tempString);
setUnitID(tempString);
cout << "What is the aircraft type?" << endl;
cin >> tempInt;
setAircraft(tempInt);
cout << "Enter the weight of your unit." << endl;
double tempDouble;
cin >> tempDouble;
setWeight(tempDouble);
cout << "What is the destination for the unit?" << endl;
cin.ignore();
getline(cin, tempString);
setDestinatoin(tempString);
}
void output(cargo shipment){ //Call Copy const/Output data to user
cout << "---Shipment---" << endl;
cout << "Unit load type: " << shipment.getUnit() << endl;
cout << "Unit load abbreviation: " << shipment.getAbbrev() << endl;
cout << "Unit ID: " << shipment.getUnitID() << endl;
cout << "Aircraft type: " << shipment.getAircraft() << endl;
cout << "Weight: " << shipment.getWeight() << endl;
cout << "Destination: " << shipment.getDestination() << endl;
cout << "---End of shipment ---" << endl;
}
int main(){
cargo shipment;
cargo shipment2;
cargo shipment3;
shipment.input();
output(shipment);
output(shipment2);
output(shipment3);
return 0;
}
//Setters
double weightConvert(double weightName){
weightName = weightName * 2.2046;
return weightName;
}
void cargo::setUnit(string unitName){
unit = unitName;
}
void cargo::setAbbrev(string abbrevName){
abbrev = abbrevName;
}
void cargo::setUnitID(string unitIDName){
unitID = unitIDName;
}
void cargo::setAircraft(int aircraftName){
aircraft = aircraftName;
}
void cargo::setDestinatoin(string destinationName){
destination = destinationName;
}
void cargo::setWeight(double weightName){
cout << "Is your weight in kilograms or pounds? k/p" << endl;
char x;
cin >> x;
if(x == 'k' || x == 'K'){
weight = weightConvert(weightName);
} else if(x == 'p' || x == 'P') {
weight = weightName;
}
}
//Getters
string cargo::getUnit() const{
return unit;
}
string cargo::getAbbrev() const{
return abbrev;
}
string cargo::getUnitID() const{
return unitID;
}
int cargo::getAircraft() const{
return aircraft;
}
double cargo::getWeight() const{
return weight;
}
string cargo::getDestination() const{
return destination;
}
Source: Windows Questions C++