[ fromfile: overloadhide.xml id: overloadhide ]
First, recall the definitions of two terms that often get confused:
When two or more versions of a function foo
exist in the same scope
(with different signatures), we say that foo
has been overloaded.
When a virtual
function from the base class also exists in the
derived class, with the same signature and
return type, we say that the derived version overrides the base class version.
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); 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; void deposit(double amt, QDate postDate); }; [ . . . . ]
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
Only the derived class function can be called directly.
The class scope resolution operator :: must be used to call hidden base functions explicitly.
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); acct.m_Balance += 6.23; acct.Account::deposit(6.23); // ... more client code return 0; }
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |