6.4.  Overloading, Hiding, and Overriding

[ fromfile: overloadhide.xml id: overloadhide ]

Example 6.18. src/derivation/overload/account.h

[ . . . . ]
class Account {
public:
    Account(unsigned acctno, double bal, QString owner);
    virtual ~Account() { }
    virtual void deposit(double amt);
    virtual QString toString() const;
    virtual QString toString(char delimiter);   1
protected:
    unsigned  m_AcctNo;
    double    m_Balance;
    QString   m_Owner;
};

class InsecureAccount: public Account {
public:
    InsecureAccount(unsigned acctno, double bal, QString owner);
    QString toString() const;                   2
    void deposit(double amt, QDate postDate);   3
};
[ . . . . ]

1

Overloaded function.

2

Overrides base method and hides toString(char).

3

Does not override any method, but hides all Account::deposit() methods.


Function Hiding

Example 6.19. src/derivation/overload/account-client.cpp

#include  "account.h"
#include  <QTextStream>

int main() {
    InsecureAccount acct(12345, 321.98, "Luke Skywalker");
    acct.deposit(6.23);             1
    acct.m_Balance += 6.23;         2
    acct.Account::deposit(6.23);    3
    // ... more client code
    return 0;
}

1

Error! No matching function - hidden by deposit(double, int).

2

Error! Member is protected, inaccessible.

3

Hidden does not mean inaccessible. You can still access hidden public members via scope resolution.