//why the getters declared twice,public,and than protected at the end of this code?
And how to assign the copy constructor between this 2 classes?
class Person
    string name;
    string address;
    string city;
    string state;
    string zipCode;
    Person() {}
    Person(const string& aName,
           const string& anAddress,
           const string& aCity,
           const string& aState,
           const string& aZipCode) : name(aName),
                                     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
    Person sender;
    Person recipient;
    Package( const Person& aSender, const Person& aRecipient)
     : sender(aSender), recipient(aRecipient) {}
    virtual ~Package();
    virtual void CalculateCost(void) = 0;
    virtual void Display(void) = 0;
    void setSender(const Person& aPerson);
    void setRecipient(const Person& aPerson);
    const Person& getSender() const { return sender; }
    const Person& getRecipient() const { return recipient; }
    Person& getSender() { return sender; }
    Person& getRecipient() { return recipient; }

The const getters return a non-mutable reference to the internel properties. The non-const getters (protected) provide a mutable reference to the properties, allowing them to be changed directly. Normally you want to use public setter methods to change properties, which can be coded to test for invalid inputs and throw the appropriate exceptions when necessary.

As for copy constructors and assignment operators, each class has its own. The compiler will create them for you if necessary, but that is not recommended.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.