CIS 22B - Notes for Mon Dec 04

Announcements and Reminders

  • Assignment 9 is due NOW
  • On-line time tomorrow night 7:00-8:15 pm  CCC Confer
  • Final is next Monday 11:30-1:30

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

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

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 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 used, and Lab Exercise #11 in a comment at the top of your program. Email your source code. 

Complete the following program:

class Pet
{
protected:
    string type;
    string name;
public:
    Pet(const string& arg1, const string& arg2);
    virtual void describe() const;
    virtual string speak() const = 0;
};

class Dog : public Pet
{
public:
    void describe() const;  // override the describe() function
   ?
};

class Cat : public Pet
{
   ?
    // Do not override the describe() function
};

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

int main()
{
    Dog fido("dog","Fido");
    Cat garfield("cat","Garfield");
    Pet* ptr = &fido;
    cout << *ptr << endl;
    ptr = &garfield;
    cout << *ptr << endl;
}

OUTPUT

I am an excellent dog and you may refer to me as Fido
I say woof
I am a cat and my name is Garfield
I say meow