Computer Programming
Lecture No. 11
Virtual Functions and Polymorphism
Notes on virtual
• If a method is declared virtual in a class,
– … it is automatically virtual in all derived classes
• It is a really, really good idea to make destructors
virtual!
virtual ~Shape();
– Reason: to invoke the correct destructor, no matter how
object is accessed
Virtual Destructors
• Constructors cannot be virtual, but destructors can
be virtual.
• It ensures that the derived class destructor is called
when a base class pointer is used while deleting a
dynamically created derived class object.
Virtual Destructors (contd.)
class base { int main()
public: {
~base() {
cout << “destructing base\n”;
base *p = new derived;
} delete p;
}; return 0;
}
class derived : public base {
public:
~derived() {
Output:
destructing base
cout << “destructing derived\n”;
}
};
Using non-virtual destructor
Virtual Destructors (contd.)
class base { int main()
{
public:
virtual ~base() {
base *p = new derived;
cout << “destructing base\n”; delete p;
}
}; return 0;
}
class derived : public base {
public:
~derived() { Output:
cout << “destructing derived\n”; destructing derived
} destructing base
};
Using virtual destructor
Notes on virtual (continued)
• A derived class may optionally override a virtual function
• If not, base class method is used
class Shape {
public:
virtual void Rotate();
virtual void Draw();
...
}
class Line: public Shape {
public:
void Rotate();
//Use base class Draw method
...
}
Abstract and Concrete Classes
• Abstract Classes
– Classes from which it is never intended to instantiate any objects
• Incomplete—derived classes must define the “missing pieces”.
• Too generic to define real objects.
– Normally used as base classes and called abstract base classes
Definitions
• Provide appropriate base class frameworks from which other classes
can inherit.
• Concrete Classes
– Classes used to instantiate objects
– Must provide implementation for every member function they
define
Pure virtual Functions
• A class is made abstract by declaring one or more
of its virtual functions to be “pure”
– I.e., by placing "= 0" in its declaration
• Example
virtual void draw() const = 0;
– "= 0" is known as a pure specifier.
– Tells compiler that there is no implementation.
8
Pure virtual Functions (continued)
• Every concrete derived class must override all
base-class pure virtual functions
– with concrete implementations
• If even one pure virtual function is not overridden,
the derived-class will also be abstract
– Compiler will refuse to create any objects of the class
– Cannot call a constructor
Purpose
• When it does not make sense for base class to
have an implementation of a function
• Software design requires all concrete derived
classes to implement the function
• Themselves
Why Do we Want to do This?
• To define a common public interface for the
various classes in a class hierarchy
– Create framework for abstractions defined in our
software system
• The heart of object-oriented programming
• Simplifies a lot of big software systems
• Enables code re-use in a major way
• Readable, maintainable, adaptable code
12
Reading References
Polymorphism:
• http://www.cplusplus.com/doc/tutorial/polymorphism/
• http://www.cs.bu.edu/teaching/cpp/polymorphism/intro/
Virtual Functions
• Rober Lafore Chapter 11, Page 504