Book Image

Microsoft Visual C++ Windows Applications by Example

By : Stefan Bjornander, Stefan Björnander
Book Image

Microsoft Visual C++ Windows Applications by Example

By: Stefan Bjornander, Stefan Björnander

Overview of this book

Table of Contents (15 chapters)
Microsoft Visual C++ Windows Applications by Example
Credits
About the Author
About the Reviewer
Preface
Index

Dynamic Binding


In the example above, it really is no problem to create static objects of the classes. When we call Print on each object, the corresponding version of Print will be called. It becomes a bit more complicated when we introduce a pointer to a Person object and let it point at an object of one of the subclasses. As Print in Person is virtual, dynamic-binding comes into force. This means that the version of Print in the object the pointer actually points at during the execution will be called. Had it not been virtual, Print in Person would always have been called. To access a member of an object given a pointer to the object, we could use the dot notation together with the dereferring operator. However, the situation is so common that an arrow notation equivalent to those operations has been introduced. The following two lines are by definition interchangeable:

pPerson->Print();
(*pPerson).Print();

Main.cpp

#include <iostream>
using namespace std;

#include "Person.h"
#include...