MULTIPLE INHERITANCE
MULTIPLE INHERITANCE
POLYMORPHISM IN C++
WHAT IS POLYMORPHISM?
• Polymorphism is the ability to use the same expression to denote different
operations
• Runtime polymorphism is the ability to associate multiple meanings to a single
function name though the use of late or dynamic binding
• Compile time polymorphism is the type that is achieved through function
overloading, operator overloading, and templates
• Another form is parametric polymorphism
• the (data) type is left unspecified and later instantiated
• templates provide parametric polymorphism
COMPILE TIME POLYMORPHISM
• Function that includes an object passed by refernce
void displayGrade(const GradedActivity &)
• We can pass any okbject that has an “is-a” relationship and the compiler will
bind that object to the base class definition
• Static binding: matching a function call with a function at compile time.
CLASS EXAMPLE
WHAT IS POLYMORPHISM?
• Provides a mechanism to allow programs to process
objects of classes that are part of the same class
inheritance hierarchy as though they are part of the base
class
• Allows you to design and implement systems that are
extensible
• classes can be added with little to no modifications to
portions of the program
• virtual functions provide a means to apply runtime
polymorphism
VIRTUAL FUNCTIONS
• A virtual function is specified by using the keyword virtual
• A function whose behavior can be overridden or replaced
• Function overriding is a feature that allows a derived class to
provide a specific implementation for a function that is provided
by a base class
• This is NOT the same as function overloading – the return type,
name, and parameters are the same in the base and derived
classes
PURE VIRTUAL FUNCTIONS
• A pure virtual function is specified by setting the function
“= 0” in the declaration
• Does not provide an implementation for the function, just
a declaration
• Each derived class must override all base-class pure virtual
functions with concrete implementations
• The compiler will report an error if a pure virtual function
is not overridden
ABSTRACT CLASSES
• A class is considered abstract if one or more of its virtual functions is pure
• Abstract Classes cannot be instantiated
• If you have decided that a class must be abstract, then you should make each
function that must be overridden pure virtual
• Remember: a “non-pure” virtual function does not have to be overridden!
• Note: classes that can be instantiated are called concrete classes
BASE CLASS POINTERS
• Pointers to a base class may be assigned the address of a derived class object.
GradedActivity *exam = new PassFailExam(100, 25, 70.0);
• This statement dynamically allocates a PassFailExam object and assigns its address to
exam, which is a GradedActivity pointer.
cout << exam->getScore() << endl;
cout << exam->getLetterGrade() << endl;
BASE CLASS POINTER LIMITATIONS
VIRTUAL DESTRUCTORS
• When you write a class with a destructor, and that class could
potentially become a base class, you should always declare the
destructor virtual
• This is because the compiler will perform static binding on the
destructor if it is not declared virtual
• lead to problems when a base class pointer or reference variable
references a derived class object
• the derived class has its own destructor, it will not execute when the
object is destroyed or goes out of scope
REFERENCES
• D & D Chapter 12
• GADDIS Chapter 15