Polymorphism
Polymorphism
is implemented when you have (a) derived class(es) containing a member
function with the same signature as a base class. A function
invoked through a pointer to the base class, will execute the correct
implementation regardless of whether the pointer is pointing at a base
class object or a derived class object. Functions that behave
in this way are called virtual functions. The determination
of which function to call is not known at compile-time, so the correct
function is selected during execution. This process is called
late binding, or dynamic binding. The usual call of a
function through an object, is known to the compiler, hence, early
binding or static binding.
Notes
Examples
Virtual functions and polymorphism
More virtual functions
Still more virtual functions
Why
write a virtual destructor?
How
does polymorphism work?
And
what is meant by dynamic binding (or late binding) and static binding?
Non-Virtual,
Virtual, and Pure Virtual Functions
Non-Virtual
- This is the default type of class member
function. The keyword virtual does not appear in the function
prototype.
- Non-virtual functions, as a rule, are not usually
overridden in the derived class.
Virtual
- The keyword virtual appears at the beginning of the
function prototype in the base class. It doesn’t have to be
used in derived class function prototypes, but it’s not a bad idea to
use it.
- Virtual functions, as a rule, are usually overridden
in the derived class.
- Virtual functions make polymorphism possible.
Pure
Virtual
- The keyword virtual appears at the beginning
and = 0 at the end of the function prototype in the
base class. The = 0 is not repeated in
derived classes unless that class is intended to serve as a base class
for other descendent classes.
- Pure virtual functions must be overridden in the
derived class, unless, that class is also a base class for other
classes.
- Pure virtual functions are not defined in the class
in which they are declared as pure virtual.
- The presence of a pure virtual function in a class
makes it an abstract class. Abstract
classes may
not be instantiated.
|
Lab
Exercise #11
Put
your name, the compiler and operating system used, and Lab Exercise 11 in a comment at the
top of your program. Email your source code. Complete the following program: #include <iostream> #include <string> using namespace std;
class Animal { protected: string type; string name; public: Animal(const string& typ, const string& nam); virtual void whoAmI() const; virtual string sound() const = 0; };
class Pig : public Animal { public: // do not override the whoAmI function // add other functions };
class Cow : public Animal { public: // override the whoAmI function // add other functions };
ostream& operator<<(ostream& out, const Animal& object) { ??? }
int main() { Cow spot("cow","Bossy"); Pig socks("pig","Porky"); Animal* ptr = &spot; cout << *ptr << endl; ptr = &socks; cout << *ptr << endl; }
|
Output I am a milk cow and you may call me Bossy I make the sound, "Moo!" I am a pig and my name is Porky I make the sound, "Oink!" |
|