writing a program about shipping

class Package
{
private:
// data members to store sender and recipient's address information
std::string senderName;
std::string senderAddress;
std::string senderCity;
std::string senderState;
int senderZIP;
std::string recipientName;
std::string recipientAddress;
std::string recipientCity;
std::string recipientState;
int recipientZIP;

double weight; // weight of the package
double costPerOunce; // cost per ounce to ship the package

public:
// constructor initializes data members
Package( const std::string &, const std::string &, const std::string &,
const std::string &, int, const std::string &, const std::string &, const std::string &,
const std::string &, int, double, double );

void setSenderName( const std::string & ); // set sender's name
std::string getSenderName() const; // return sender's name
void setSenderAddress( const std::string & ); // set sender's address
std::string getSenderAddress() const; // return sender's address
void setSenderCity( const std::string & ); // set sender's city
std::string getSenderCity() const; // return sender's city
void setSenderState( const std::string & ); // set sender's state
std::string getSenderState() const; // return sender's state
void setSenderZIP( int ); // set sender's ZIP code
int getSenderZIP() const; // return sender's ZIP code
void setRecipientName( const std::string & ); // set recipient's name
std::string getRecipientName() const; // return recipient's name
void setRecipientAddress( const std::string & ); // set recipient's address
std::string getRecipientAddress() const; // return recipient's address
void setRecipientCity( const std::string & ); // set recipient's city
std::string getRecipientCity() const; // return recipient's city
void setRecipientState( const std::string & ); // set recipient's state
std::string getRecipientState() const; // return recipient's state
void setRecipientZIP( int ); // set recipient's ZIP code
int getRecipientZIP() const; // return recipient's ZIP code
void setWeight( double ); // validate and store weight
double getWeight() const; // return weight of package
void setCostPerOunce( double ); // validate and store cost per ounce
double getCostPerOunce() const; // return cost per ounce

double calculateCost() const; // calculate shipping cost for package

}; // end class Package


// constructor initializes data members
Package::Package( const string &sName, const string &sAddress,
const string &sCity, const string &sState, int sZIP,
const string &rName, const string &rAddress, const string &rCity,
const string &rState, int rZIP, double w, double cost )
: senderName( sName ), senderAddress( sAddress ), senderCity( sCity ),
senderState( sState ), senderZIP( sZIP ), recipientName( rName ),
recipientAddress( rAddress ), recipientCity( rCity ),
recipientState( rState ), recipientZIP( rZIP )
{
setWeight( w ); // validate and store weight
setCostPerOunce( cost ); // validate and store cost per ounce
} // end Package constructor

// set sender's name
void Package::setSenderName( const string &name )
{
senderName = name;
} // end function setSenderName

// return sender's name
string Package::getSenderName() const
{
return senderName;
} // end function getSenderName

// set sender's address
void Package::setSenderAddress( const string &address )
{
senderAddress = address;
} // end function setSenderAddress

// return sender's address
string Package::getSenderAddress() const
{
return senderAddress;
} // end function getSenderAddress

// set sender's city
void Package::setSenderCity( const string &city )
{
senderCity = city;
} // end function setSenderCity

// return sender's city
string Package::getSenderCity() const
{
return senderCity;
} // end function getSenderCity

// set sender's state
void Package::setSenderState( const string &state )
{
senderState = state;
} // end function setSenderState

// return sender's state
string Package::getSenderState() const
{
return senderState;
} // end function getSenderState

// set sender's ZIP code
void Package::setSenderZIP( int zip )
{
senderZIP = zip;
} // end function setSenderZIP

// return sender's ZIP code
int Package::getSenderZIP() const
{
return senderZIP;
} // end function getSenderZIP

// set recipient's name
void Package::setRecipientName( const string &name )
{
recipientName = name;
} // end function setRecipientName

// return recipient's name
string Package::getRecipientName() const
{
return recipientName;
} // end function getRecipientName

// set recipient's address
void Package::setRecipientAddress( const string &address )
{
recipientAddress = address;
} // end function setRecipientAddress

// return recipient's address
string Package::getRecipientAddress() const
{
return recipientAddress;
} // end function getRecipientAddress

// set recipient's city
void Package::setRecipientCity( const string &city )
{
recipientCity = city;
} // end function setRecipientCity

// return recipient's city
string Package::getRecipientCity() const
{
return recipientCity;
} // end function getRecipientCity

// set recipient's state
void Package::setRecipientState( const string &state )
{
recipientState = state;
} // end function setRecipientState

// return recipient's state
string Package::getRecipientState() const
{
return recipientState;
} // end function getRecipientState

// set recipient's ZIP code
void Package::setRecipientZIP( int zip )
{
recipientZIP = zip;
} // end function setRecipientZIP

// return recipient's ZIP code
int Package::getRecipientZIP() const
{
return recipientZIP;
} // end function getRecipientZIP

// validate and store weight
void Package::setWeight( double w )
{
if ( w >= 0.0 )
weight = w;
else
throw invalid_argument( "Weight must be >= 0.0" );
} // end function setWeight

// return weight of package
double Package::getWeight() const
{
return weight;
} // end function getWeight

// validate and store cost per ounce
void Package::setCostPerOunce( double cost )
{
if ( cost >= 0.0 )
costPerOunce = cost;
else
throw invalid_argument( "Cost must be >= 0.0" );
} // end function setCostPerOunce

// return cost per ounce
double Package::getCostPerOunce() const
{
return costPerOunce;
} // end function getCostPerOunce

// calculate shipping cost for package
double Package::calculateCost() const
{
return getWeight() * getCostPerOunce();

} // end function calculateCost

class TwoDayPackage : public Package
{
private:
double flatFee;
public:
TwoDayPackage( const string &, const string &, const string &,
const string &, int, const string &, const string &, const string &, const string &, int, double, double, double );

void TwoDayPackage::setFlatFee( double fee )
{
flatFee = ( fee < 0.0 ) ? 0.0 : fee;
}


double TwoDayPackage::getFlatFee() const
{
return flatFee;
}

double calculateCost() const
{
return Package::calculateCost() + getFlatFee();
}

};

class OvernightPackage : public Package
{
private:
double overnightFeePerOunce;
public:
OvernightPackage( const string &, const string &, const string &,
const string &, int, const string &, const string, const string &, const string &, int, double, double, double );



void OvernightPackage::setOvernightFeePerOunce( double overnightFee )
{
overnightFeePerOunce = ( overnightFee < 0.0 ) ? 0.0 : overnightFee;
}

double OvernightPackage::getOvernightFeePerOunce() const
{
return overnightFeePerOunce;
}

double calculateCost() const
{
return getWeight() * ( getCostPerOunce() + getOvernightFeePerOunce() );
}



};



int main()
{


Package package1( "Lou Brown", "1 Main St", "Boston", "MA", 11111,
"Mary Smith", "7 Elm St", "New York", "NY", 22222, 8.5, .5 );
TwoDayPackage package2( "Lisa Klein", "5 Broadway", "Somerville", "MA",
33333, "Bob George", "21 Pine Rd", "Cambridge", "MA", 44444, 10.5, .65, 2.0 );
OvernightPackage package3( "Ed Lewis", "2 Oak St", "Boston", "MA", 55555, "Don Kelly", "9 Main St", "Denver", "CO", 66666,
12.25, .7, .25 );

cout << "Package 1:\n\nSender:\n" << package1.getSenderName()
<< '\n' << package1.getSenderAddress() << '\n'
<< package1.getSenderCity() << ", " << package1.getSenderState()
<< ' ' << package1.getSenderZIP();
cout << "\n\nRecipient:\n" << package1.getRecipientName()
<< '\n' << package1.getRecipientAddress() << '\n'
<< package1.getRecipientCity() << ", "
<< package1.getRecipientState() << ' '
<< package1.getRecipientZIP();
cout << "\n\nCost: $" << package1.calculateCost() << endl;

cout << "\nPackage 2:\n\nSender:\n" << package2.getSenderName()
<< '\n' << package2.getSenderAddress() << '\n'
<< package2.getSenderCity() << ", " << package2.getSenderState()
<< ' ' << package2.getSenderZIP();
cout << "\n\nRecipient:\n" << package2.getRecipientName()
<< '\n' << package2.getRecipientAddress() << '\n'
<< package2.getRecipientCity() << ", "
<< package2.getRecipientState() << ' '
<< package2.getRecipientZIP();
cout << "\n\nCost: $" << package2.calculateCost() << endl;

cout << "\nPackage 3:\n\nSender:\n" << package3.getSenderName()
<< '\n' << package3.getSenderAddress() << '\n'
<< package3.getSenderCity() << ", " << package3.getSenderState()
<< ' ' << package3.getSenderZIP();
cout << "\n\nRecipient:\n" << package3.getRecipientName()
<< '\n' << package3.getRecipientAddress() << '\n'
<< package3.getRecipientCity() << ", "
<< package3.getRecipientState() << ' '
<< package3.getRecipientZIP();
cout << "\n\nCost: $" << package3.calculateCost() << endl;
return 0;

}

error:

1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall TwoDayPackage::TwoDayPackage(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,double,double,double)" (??0TwoDayPackage@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@000H0000HNNN@Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall OvernightPackage::OvernightPackage(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,double,double,double)" (??0OvernightPackage@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@000H0V12@00HNNN@Z) referenced in function _main
fatal error LNK1120: 2 unresolved externals
And? What your problem is?
I don't know, it keeps saying this:

1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall TwoDayPackage::TwoDayPackage(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,double,double,double)" (??0TwoDayPackage@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@000H0000HNNN@Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall OvernightPackage::OvernightPackage(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,double,double,double)" (??0OvernightPackage@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@000H0V12@00HNNN@Z) referenced in function _main
fatal error LNK1120: 2 unresolved externals
Where is that mostruocity body declared?
1
2
TwoDayPackage( const string &, const string &, const string &,
const string &, int, const string &, const string &, const string &, const string &, int, double, double, double );
The first linker error is telling you the linker can't find a constructor for TwoDayPackage. I see none in your implementation, but you try to invoke it in your main.

Ditto for OvernightPackage's constructor.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class OvernightPackage : public Package
{
private:
double overnightFeePerOunce;
public:
OvernightPackage( const string &, const string &, const string &,
const string &, int, const string &, const string, const string &, const string &, int, double, double, double );



void OvernightPackage::setOvernightFeePerOunce( double overnightFee )
{
overnightFeePerOunce = ( overnightFee < 0.0 ) ? 0.0 : overnightFee;
} 

double OvernightPackage::getOvernightFeePerOunce() const
{
return overnightFeePerOunce;
}

double calculateCost() const
{
return getWeight() * ( getCostPerOunce() + getOvernightFeePerOunce() );
}



};
OK, let me write something.
BTW, did you happen to notice that sender and recipient are exactly the same constructs. It would make sense to make an Address class. sender and recipient become instances of that class. You can then pass instances of Address to your package constructors. Would simplify your program considerably.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Address 
{  std::string Name;
    std::string Address;
    std::string City;
    std::string State;
    int Zip;
public:
    Address (string name, string addr, string city, string state, int zip);
..
//  getters and setters
};
class Package
{   Address  sender;
    Address recipient;
    double weight; // weight of the package
    double costPerOunce; // cost per ounce to ship the package
public:
// constructor initializes data members
Package (const Address &sender, const Address &recipient, double, double );
...
};

Last edited on
I have my constructors, still errors.
ok, my problem is in the int main(), i have to change it and rewrite it again.
Topic archived. No new replies allowed.