CIS 22B - Notes for Monday 12/3

Announcements and Reminders


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.



Non-virtual functions

Virtual functions and polymorphism

More virtual functions

Still more virtual functions

Why write a virtual destructor?

Example 1

Example 2

How does polymorphism work?

And what is meant by dynamic binding (or late binding) and static binding?

Non-Virtual, Virtual, and Pure Virtual Functions


  • 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.


  • 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 vitual.
  • The presence of a pure virtual function in a class makes it an abstract class Abstract classes may not be instantiated.
Example - Abstract Classes and Pure Virtual Functions

Example - Abstract Class and Polymorphism

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:

class Pet
    string type;
    string name;
    Pet(const string& arg1, const string& arg2);
    virtual void whoAmI() const;
    virtual string speak() const = 0;

class Dog : public Pet
    void whoAmI() const;    // override the whoAmI() function

class Cat : public Pet
    // Do not override the whoAmI() function

ostream& operator<<(ostream& out, const Pet& p)
    out << "I say " << p.speak();
    return out;

int main()
    Dog spot("dog","Spot");
    Cat socks("cat","Socks");
    Pet* ptr = &spot;
    cout << *ptr << endl;
    ptr = &socks;
    cout << *ptr << endl;


I am a great dog and you may call me "Spot"      <== Don't forget the quotes
I say Arf!
I am a cat and my name is "Socks"
I say Meow!