0% found this document useful (0 votes)
52 views

Lecture # 10

This document discusses polymorphism in object-oriented programming. It defines polymorphism as values taking more than one type, including simple data, functions, or objects. There are two types of polymorphism: ad-hoc polymorphism (which includes coercion and overloading) and universal polymorphism (which includes parametric and inclusion polymorphism). Polymorphism is achieved through the use of virtual functions in abstract base classes. When a virtual function is called, C++ chooses the correct overridden function in the derived class. Polymorphism promotes extensibility by allowing new objects to respond to existing messages without modifying the base system. An example demonstrates polymorphism by defining rectangle and triangle classes that derive from a shape base class and override

Uploaded by

cynicstud
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Lecture # 10

This document discusses polymorphism in object-oriented programming. It defines polymorphism as values taking more than one type, including simple data, functions, or objects. There are two types of polymorphism: ad-hoc polymorphism (which includes coercion and overloading) and universal polymorphism (which includes parametric and inclusion polymorphism). Polymorphism is achieved through the use of virtual functions in abstract base classes. When a virtual function is called, C++ chooses the correct overridden function in the derived class. Polymorphism promotes extensibility by allowing new objects to respond to existing messages without modifying the base system. An example demonstrates polymorphism by defining rectangle and triangle classes that derive from a shape base class and override

Uploaded by

cynicstud
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Lecture # 10

Object Oriented Programming


Polymorphism
Muhammad Aamir Khan
Polymorphism
 Poly= many
 Morph = forms, shapes, types

 Polymorphism = values taking more than


one type

 Values may be simple data, function or


objects
Thursday, December 17, 2009 OOP by M.A.K 2
Types of polymorphism
 There are two types of polymorphism
 Ad-hoc Polymorphism
Not true but apparent polymorphism
 Syntactic reuse

 Universal Polymorphism
True polymorphism
 Semantic Reuse

Thursday, December 17, 2009 OOP by M.A.K 3


Ad-hoc Polymorphism
 Adhoc polymorphism is divided into two
types

 Coercion (Implicit/ Explicit Type Conversion)


Type conversion
Explicit type casting

 Overloading

Thursday, December 17, 2009 OOP by M.A.K 4


Overloading
 In C++ methods can be overloaded
Example:
Void print(int x);
Void print(String x);

 The right function is selected by compiler


based on type information

Thursday, December 17, 2009 OOP by M.A.K 5


Universal polymorphism
 There are two types of universal
polymorphism
 Parametric polymorphism
 Inclusion polymorphism

Thursday, December 17, 2009 OOP by M.A.K 6


Virtual functions
 Recall the concept of abstract class

 Any class containing one or more than


one virtual function is termed as abstract
class

 Polymorphism is achieved through the use


of virtual functions
Thursday, December 17, 2009 OOP by M.A.K 7
Virtual and Pure Virtual
A virtual function is declared like
virtual int area();

A pure virtual function is declared like


virtual int area()=0;

Thursday, December 17, 2009 OOP by M.A.K 8


Polymorphism
 Polymorphism is implemented through the
use of virtual function

 When a call is made to use a virtual


function, C++ chooses the correct
overridden funtion in the appropriate
derived class with the object

Thursday, December 17, 2009 OOP by M.A.K 9


Benefits of Polymorphism
 Polymorphism promotes extensibility

 New objects that respond to existing


messages can be added to the base
system without modifying it

Thursday, December 17, 2009 OOP by M.A.K 10


Practice
Pointer to base class
#include<iostream.h>
class shape {
protected:
int width,height;
public:
void setvalues(int a, int b){width=a;height=b;}
};

class rectangle:public shape {


public:
int area(){
return (width*height);}
};
class triangle:public shape {
public:
int area() {
return(width*height/2);}
};
Thursday, December 17, 2009 OOP by M.A.K 11
Practice
Pointer to base class
int main()
{

rectangle rect;
triangle trg;
shape *sh1=&rect;
shape *sh2 = &trg;
sh1->setvalues(5,4);
sh2->setvalues(5,4);
cout<<rect.area()<<endl;
cout<<trg.area()<<endl;

return 0;
}

Thursday, December 17, 2009 OOP by M.A.K 12


That’s All for today

You might also like