Polymorph Is M
Polymorph Is M
(LAB-12)
Polymorphism in Object Oriented Programming
Table of Contents
1. Introduction 108
A member function of a base class can be made a virtual function if it is probable to redefine (override) this function
in the derived class. The significance of making a function virtual becomes evident when you want to invoke function
of derived class from a pointer of base class referencing derived class‟ object. Following code provides an example
of polymorphism mechanism in C++.
USER
2013-08-23 15:58:16
class Base
--------------------------------------------
{
class's
public:
virtual void Func()
{
cout<<”Base Class Function”;
}
};
Upon calling Func() with bPtr, Derived class Func() will be invoked and output would be “Base Class Function”.
In the absence of virtual keyword, Func() of base would be called each time. Hence, it can be concluded that virtual
USER
keyword allow a pointer of base class to call appropriate function of any of its derived class2013-08-23
to whom it15:59:34
is referencing.
In this way, each time a different function will be invoked with the same function call. With polymorphism, compiler
--------------------------------------------
does not know which function to call at compile and thus appropriate function call is deferred to runtime. At runtime,
funt()
the type of object that a base class pointer is pointing is recognized to call proper function. This is known as dynamic
or late binding.
Virtual keyword can also be used with destructors and classes. However, virtual keyword possess different meanings
when used with destructor and classes as explained below.
Virtual Destructors:
In practice, it is always recommended to make the base class destructor as virtual. In the absence of virtual keyword
with base class destructor, upon deletion of any derived class‟ object only destructor of base class would be called.
class Base
{
protected:
void Func()
{ cout<<”A Base Class Function”; }
};
class Derived1: public Base{ };
class Derived2: public Base{ };
class Derived3: public Derived1, public Derived2
{ };
In the code above, both (Derived1 and Derived2) classes contain a copy of Func(), which is inherited from class Base.
Upon the invocation of Func() with an object of Derived3 class, compiler would be unable to decide which copy to
use.
To overcome this situation, it is required to use virtual keyword with base class inheritance. Hence, the above code
would be implemented as
class Base
{
protected:
void Func()
{ cout<<”A Base Class Function”; }
};
class Derived1: virtual public Base{ };
class Derived2: virtual public Base{ };
class Derived3: public Derived1, public Derived2
{ };
class Base
{
protected:
virtual void Func() = 0;
};
Instantiation of an abstract class is not possible and you should override pure virtual function in derived class(es).
4. Concept Map
Following encapsulation and inheritance, polymorphism is the third important capability of OOP paradigm. The
fundamental inspiration behind polymorphism is that of dynamic binding. In this way, at compile time, compiler
knows nothing about which function to call. On the contrary, decision is made at runtime. Polymorphism helps
programmer to make program in general rather than specific. In simple words, it provides a single interface with
different implementations. Moreover, polymorphism facilitates program extensibility while permitting new derived
classes and functions to be added to an inheritance hierarchy without modifying application programs that already
utilize various interfaces of that inheritance hierarchy. However, for beginners, it is difficult to grasp and implement
the concept of polymorphism, dynamic binding, and abstract classes.
Draw UML class diagrams of the following task. You are required to bring this design with you and submit to
your lab instructor.
Derive a class named Exam inherited from Student class and contains
USER
Two additional data members i.e. course1 and course2 2013-08-23 16:36:54
A parameterized constructor to initialize its own data fields along with the inherited data field
--------------------------------------------
Two getter functions that return the value of course1 and course2, respectively Exam and Student can be associated but
E
mhad no real world inheritance relation
a
x
with Student.
Derive a class named Result inherited from class Exam and has
Draw UML diagram for each class and show inheritance relationship between these classes.
5.3.1 Task-1
Consider four classes i.e. Person, Staff, Professor, and Researcher with the following inheritance hierarchy:
Class Researcher is derived from both Staff and Professor classes that are ultimately derived from class Person.
Class Person has two attributes i.e. name and age and one member function display() to show its attribute values.
Class Staff has two attributes i.e. staffID and department.
Class Professor has two attributes i.e. courseID and courseName.
USER
Class Researcher has an additional attribute named labID and experimentNo. 2013-08-23 16:45:57
USER
--------------------------------------------
2013-08-23 16:46:24
Draw UML diagram for each class and show inheritance relationship between these classes.
--------------------------------------------
no logical abstraction of both attributes
toProfesor. as above
same
5.3.2 Task-2
Illustrate the implementation of classes that have been specified in Task-1 using C++ compiler in a way that avoid
USER
diamond problem of multiple inheritance. 2013-08-23 16:51:48
--------------------------------------------
6. Procedure & Tools A task should be given on Diamond Shaped
problemin multiple inheritance considering
6.1 Tools thefigure 1 of lab 10 at page 92. as it is
related to that case.
Visual Studio 2008.
In this task, you are required to write a C++ code which would elaborate polymorphism concept. The following lines
show the output of a basic polymorphism concept:
In the source file, which is created in the project “Polymorphism” write following C++ code:
.
Figure 1: Polymorphism
6.3.2 Compilation
After writing the code, compile your code according to the guidelines mentioned. Remove any errors and warnings
that are present in your code.
USER
2013-08-23 16:54:43
--------------------------------------------
not visible
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You need to finish the
tasks in the required time. When you finish them, put these tasks in the following folder:
\\dataserver\assignments$\OOP\Lab12
A class named Desktop inherits Computer class and adds fields representing
color, monitor size, and processor type and
Override function named show() to display values of its all attributes
A class named Laptop inherits Computer class and adds fields representing
color, size, weight, and processor type and
Override function named show() to display values of its all attributes
Write a main() function that instantiates objects of derived classes to access respective show() function using
dynamic binding.
In the main function, create instances of derived classes to access respective print() function using dynamic
binding.
7.3 Outcomes
After completing this lab, student will be able to implement the concepts of virtual functions, dynamic binding, pure
virtual function, and abstract classes.
7.4 Testing
For Practice Task 1, it is required to check the assignment of derived class instances to base class pointer.
Moreover, (while considering the dynamic binding) test the invocation of respective show() function.
For Practice Task 2, it is required to check the assignment of derived class instances to base class pointer.
Moreover, (while considering the dynamic binding) test the invocation of respective print() function.
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is assigned the
marks percentage which will be evaluated by the instructor in the lab whether the student has finished the
complete/partial task(s).
10.1 Books
10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available at
\\dataserver\jinnah$\