//Hi i do the delivery company project,this are my members of the base class
//how do i create 2 constructors in base,one for sender and one for recipient?
//I did one  for sender ,but visual studio does not allow me to create identical for recipient
//Can some one  help to figure it out,please?
#pragma once
#include<string>
using namespace std;

class Package
{
public:
    Package();
    ~Package();
protected:
    string senderName;
    string senderAddress;
    string senderCity;
    string senderState;
    string senderZipCode;




    string recipientName;
    string recipientAddress;
    string recipientCity;
    string recipientState;
    string recipientZipCode;
    double weight;
    double ShippingCost;
public:
    virtual void CalculateCost(void) = 0;
    virtual void Display(void) = 0;

    //setters for sender
    void SetName(string name);
    void SetSenderAddress(string address);
    void SetSenderCity(string city);
    void SetSenderState(string state);
    void SetSenderZipCode(string zipCode);

    //getters for sender
    string GetSenderName(void);
    string GetSenderAddress(void);
    string GetSenderCity(void);
    string GetSenderState(void);
    string GetSenderZipCode(void);

    //setters for recipient
    void SetrecipientName(string name);
    void SetrecipientAddress(string address);
    void SetrecipientCity(string city);
    void SetrecipientState(string state);
    void SetrecipientZipCode(string zipCode);

    //getters for recipient
    string GetRecipientName(void);
    string GetRecipientAddress(void);
    string GetRecipientCity(void);
    string GetRecipientState(void);
    string GetRecipientZipCode(void);

    //constructors

    Package(string sName, string sAddress, string sCity, string sState, string sZip);
};

Recommended Answers

All 10 Replies

I would recommend that you create a support class, call it Person, that encapsulates name, address, city, state, and zipcode. That will simplify the underlying code considerably. IE:

class Person
{
private:
    string name;
    string address;
    string city;
    string state;
    string zipCode;
public:
    Person() {}
    Person(const string& aName,
           const string& anAddress,
           const string& aCity,
           const string& aState,
           const string& aZipCode) : name(aName),
                                     address(anAddress),
                                     city(aCity),
                                     state(aState),
                                     zipCode(aZipCode) {}
    ~Person() {}

    // Getters
    const string& getName() const { return name; }
    const string& getAddress() const { return address; }
    const string& getCity() const { return city; }
    const string& getState() const { return state; }
    const string& getZipCode() const { return zipCode; }

    // Setters
    void setName( const string& aName );
    void setAddress( const string& anAddress );
    void setCity( const string& aCity );
    void setState( const string& aState );
    void setZipCode( const string& aZipCode );
};

class Package
{
private:
    Person sender;
    Person recipient;
public:
    Package();
    Package( const Person& aSender, const Person& aRecipient)
     : sender(aSender), recipient(aRecipient) {}
    virtual ~Package();

    virtual void CalculateCost(void) = 0;
    virtual void Display(void) = 0;

    //setters
    void setSender(const Person& aPerson);
    void setRecipient(const Person& aPerson);

    //getters
    const Person& getSender() const { return sender; }
    const Person& getRecipient() const { return recipient; }
protected:
    Person& getSender() { return sender; }
    Person& getRecipient() { return recipient; }
};

There are a couple of important methods I left out of both classes - a copy constructor and assignment operator. I leave that to you as an exercise! :-)

commented: Great recommendation. +15

Sorry for asking,but never came across it.My question is should i use copy constructor and assignment operator on this 2 classes or just in one?

Can i get a little bit more explanation on copy constractor,how they perform.As i just only guessing.I have seen some examples online,but i do not know the syntax,so hard to do it ar this stage.I learn slowly ,but move on fast.

I think that rather begs the question why would you want to create a package that either doesn't have a sender or doesn't have a receiver? Almost by definition a package should have both.

Building on what @rubberman provided you can easily do this just by using the default constructor of package and then only setting the sender or receiver, you don't have to try and do everything with a constructor.

On a side note I thing a good addition to @rubberman Person class would be a method to indicate if the person was valid. Since you can clearly default construct a package without any people you would probably want to know that both the sender and receiver where valid before trying to dispatch the package. You will have to decide what criteria defines a valid Person, just name filled in or just name and zip or everything for example.

You need to create a copy constructor and assignment operator (they should always be created together) if you envisage a situation in the code where you will need to construct a new Package (or Person) from an existing one. However the code as written has the advantage that since it contains no allocated data it does not require an explicit deep copy (a constructor/assignment that allocates new memory) and therefore the default copy constructor and assignment operator provided by the compiler should work fine.

However you might still want to explicitly do it, our company coding standard states that you should either explicitly provide the copy constructor/assignment operator or prevent them being called (normally by declaring them private and not providing an implementation (yes I know C++11 provides a better way to do this but we are not using C++11 yet))

//How about this version,i try to avoid copy constructor,as i never used before
//and not to sure how to apply.
#pragma once
#include<string>
using namespace std;

class Package
{
public:
    Package();
    ~Package();
protected:
    string senderName;
    string senderAddress;
    string senderCity;
    string senderState;
    string senderZipCode;
    string recipientName;
    string recipientAddress;
    string recipientCity;
    string recipientState;
    string recipientZipCode;
    double weight;
    double CostPerOunce;
public:
    //virtual void CalculateCost(double amount) = 0;
    double CalculateCost();
    virtual void Display(void) = 0;

    //setters for sender
    void SetName(string name);
    void SetSenderAddress(string address);
    void SetSenderCity(string city);
    void SetSenderState(string state);
    void SetSenderZipCode(string zipCode);

    //getters for sender
    string GetSenderName(void);
    string GetSenderAddress(void);
    string GetSenderCity(void);
    string GetSenderState(void);
    string GetSenderZipCode(void);

    //setters for recipient
    void SetrecipientName(string name);
    void SetrecipientAddress(string address);
    void SetrecipientCity(string city);
    void SetrecipientState(string state);
    void SetrecipientZipCode(string zipCode);

    //getters for recipient
    string GetRecipientName(void);
    string GetRecipientAddress(void);
    string GetRecipientCity(void);
    string GetRecipientState(void);
    string GetRecipientZipCode(void);

    //constructors


    Package(string sName, string sAddress, string sCity, string sState, string sZip, string rName, string rAddress, string rCity, string rState, string rZip, double Weight, double OunceCost);


    void SetWeight(double Weight);
    double GetWeight();
    void SetCostPerOunce(double Cost);
    double GetCostPerOunce();
};
// In @rubberman example on line 40,41 i can not declare in my package class this 2 lines.
// sender is underlined on line 40 for me.Did i missed something?

You probably forgot the definition of Person, or forgot to include the Person.h header file.

Should the class person be a derived class of package?

That is your choice but it seems fairly general, I would just make it a whole separate class.

Solved, maybe, but a good discussion nonetheless. :-)

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.