6.5.  Overloading, Hiding, and Overriding

[ fromfile: overloadhide.xml id: overloadhide ]

First, recall the definitions of two terms that often get confused:

Example 6.18 demonstrates overloading and overriding and introduces another relationship between functions that have the same name.

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

A member function of a derived class with the same name as a function in the base class hides all functions in the base class with that name. In such a case

Example 6.19 shows the difference between a hidden and an inaccessible member.

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.