First, let us 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 { protected: static const int SS_LEN = 80; public: virtual void deposit(double amt); virtual const char* toString() const; virtual const char* toString(char delimiter); <-- 1 protected: unsigned m_AcctNo; double m_Balance; char m_Owner[SS_LEN]; }; class InsecureAccount: public Account { public: const char* toString() const; <-- 2 void deposit(double amt, QDate postDate); <-- 3 }; [ . . . . ]
|
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 addition:
The client code in Example 6.19 shows how to call a base class function that has been hidden by a derived class function with the same name.
Example 6.19. src/derivation/overload/account.cpp
#include "account.h" #include #include using namespace std; int main() { InsecureAccount acct; acct.deposit(6.23); <-- 1 acct.m_Balance += 6.23; <-- 2 acct.Account::deposit(6.23); <-- 3 return 0; }
|
Part I: Introduction to C++ and Qt 4
C++ Introduction
Classes
Introduction to Qt
Lists
Functions
Inheritance and Polymorphism
Part II: Higher-Level Programming
Libraries
Introduction to Design Patterns
QObject
Generics and Containers
Qt GUI Widgets
Concurrency
Validation and Regular Expressions
Parsing XML
Meta Objects, Properties, and Reflective Programming
More Design Patterns
Models and Views
Qt SQL Classes
Part III: C++ Language Reference
Types and Expressions
Scope and Storage Class
Statements and Control Structures
Memory Access
Chapter Summary
Inheritance in Detail
Miscellaneous Topics
Part IV: Programming Assignments
MP3 Jukebox Assignments
Part V: Appendices
MP3 Jukebox Assignments
Bibliography
MP3 Jukebox Assignments