Member Avatar for FlynnLags

Hey guys,

After fixing one of my earlier problems I ran into another one. Basically I'm trying to make an inherited class and add one more parameter to its constructor, but I can't seem to do it properly

Here is the masterclass definition Package

#ifndef PACKAGE_H
#define PACKAGE_H

#include <string>
using std::string;

class Package
{
public:
// constructor initiliazes data members
	Package( const string &, const string &, const string &,
             const string &, int, const string &, const string &, const string &,
             const string &, int, double, double );

	void setSenderName( const string & ); // set sender's name
	string getSenderName() const; // return sender's name
	void setSenderAddress( const string & ); // set sender's address
	string getSenderAddress() const; // return sender's address
	void setSenderCity( const string & ); // set sender's city
	string getSenderCity() const; // return sender's city
	void setSenderState( const string & ); // set sender's state
	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 string & ); // set recipient's name
	string getRecipientName() const; // return recipient's name
	void setRecipientAddress( const string & ); // set recipient's address
	string getRecipientAddress() const; // return recipient's address
	void setRecipientCity( const string & ); // set recipient's city
	string getRecipientCity() const; // return recipient's city
	void setRecipientState( const string & ); // set recipient's state
	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
private:
// data members to store sender and recipient's address information
	string senderName;
	string senderAddress;
	string senderCity;
	string senderState;
	int senderZIP;
	string recipientName;
	string recipientAddress;
	string recipientCity;
	string recipientState;
	int recipientZIP;
	double weight; // weight of the package
	double costPerOunce; // cost per ounce to ship the package
}; // end class Package

#endif

The functions have all been declared correctly.

Here is the inherited class TwoDayPackage:

class TwoDayPackage: public Package
{
public:
	TwoDayPackage( const string &, const string &, const string &,
             const string &, int, const string &, const string &, const string &,
             const string &, int, double = 0.0, double = 0.0, [I]double = 0.0[/I] );
 
 	void setFlatfee ( double );
 	double getFlatfee () const;
 	double calculateCost() const;
	
private:
	double flatfee;
};

The red part is the additional data member parameter.

So when I try to define TwoDayPackage's constructor like this:

TwoDayPackage::TwoDayPackage(const string &sn, const string &saddress, const string &scity, const string &sstate, int szipcode, const string &rname, const string &raddress, const string &rcity, const string &rstate, int rzipcode, double wt, double shipCost, double f)
{
        setSenderName(sn);
        setSenderAddress (saddress);
        setSenderCity(scity);
        setSenderState(sstate);
        setSenderZIP (szipcode);
        setRecipientName(rname);
        setRecipientAddress(raddress);
        setRecipientCity(rcity);
        setRecipientState(rstate);
        setRecipientZIP(rzipcode);
        setWeight(wt);
        setCostPerOunce(shipCost);
        setFlatfee(f);
}

it gives me the following error
[Error] C:\Users\Flynn\Documents\Cpp\Package.cpp:235: no matching function for call to `Package:: Package ()'

I think I'm doing something wrong when I'm overwriting the default Package constructor with the one I want for TwoDayPackage which contains an additional data member in its parameter and definition.

Anyone know what I'm doing wrong?

Thanks in advance

When you instantiate a derived class before calling the constructor of the derived class (TwoDayPackage) it calls the constructor of the parent class (Package). Unless otherwise told it calls the default constructor so

TwoDayPackage::TwoDayPackage(const string &sn, const string &saddress, const string &scity, const string &sstate, int szipcode, const string &rname, const string &raddress, const string &rcity, const string &rstate, int rzipcode, double wt, double shipCost, double f)
{
        setSenderName(sn);
        setSenderAddress (saddress);
        setSenderCity(scity);
        setSenderState(sstate);
        setSenderZIP (szipcode);
        setRecipientName(rname);
        setRecipientAddress(raddress);
        setRecipientCity(rcity);
        setRecipientState(rstate);
        setRecipientZIP(rzipcode);
        setWeight(wt);
        setCostPerOunce(shipCost);
        setFlatfee(f);
}

is equivalent to

TwoDayPackage::TwoDayPackage(const string &sn, const string &saddress, const string &scity, const string &sstate, int szipcode, const string &rname, const string &raddress, const string &rcity, const string &rstate, int rzipcode, double wt, double shipCost, double f)
: Package()
{
        setSenderName(sn);
        setSenderAddress (saddress);
        setSenderCity(scity);
        setSenderState(sstate);
        setSenderZIP (szipcode);
        setRecipientName(rname);
        setRecipientAddress(raddress);
        setRecipientCity(rcity);
        setRecipientState(rstate);
        setRecipientZIP(rzipcode);
        setWeight(wt);
        setCostPerOunce(shipCost);
        setFlatfee(f);
}

However Package has no default constructor, either you have to create a default constructor for the parent class or, and this is what I think you need to do, you put in a specific call to a non default constructor.

Additionally your derived class should not initialise/assign the members of the parent class and the parent class constructor should do that (unless it needs to override some values) so your derived constructor should be

TwoDayPackage::TwoDayPackage(const string &sn, const string &saddress, const string &scity, const string &sstate, int szipcode, const string &rname, const string &raddress, const string &rcity, const string &rstate, int rzipcode, double wt, double shipCost, double f)
: Package(sn, saddress, scity, sstate, szipcode, rname, raddress, rcity, rstate, rzipcode, wt, shipCost)
{
        setFlatfee(f);
}

Also rather than call setters from your constructor it is better to initialise the members drectly. If they are classes it will be more efficient so this becomes

TwoDayPackage::TwoDayPackage(const string &sn, const string &saddress, const string &scity, const string &sstate, int szipcode, const string &rname, const string &raddress, const string &rcity, const string &rstate, int rzipcode, double wt, double shipCost, double f)
: Package(sn, saddress, scity, sstate, szipcode, rname, raddress, rcity, rstate, rzipcode, wt, shipCost),
  flatfee(f)
{
}
commented: Helpful and solved my issue +0
Member Avatar for FlynnLags

Thanks heaps mate that solved my problem =) +rep

I guess I tried to do it in a way similar to Java, which is another language I'm learning at the same time. Thanks again.

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.