n Polymorph is m
-
Upload
abhinav-garg -
Category
Documents
-
view
223 -
download
0
Transcript of n Polymorph is m
-
7/24/2019 n Polymorph is m
1/32
NCST 1
Object Oriented Programming Using C++
Polymorphism
Polymorphism
-
7/24/2019 n Polymorph is m
2/32
NCST 2
Object Oriented Programming Using C++
Polymorphism
Static Binding
Method binding is at compile-time Pointer is Base* though it contains Deried
instance
Compiler sees Base*
Deried instance in!o aailable only at run-
time
-
7/24/2019 n Polymorph is m
3/32
NCST "
Object Oriented Programming Using C++
Polymorphism
Dynamic Binding
Methods should get bound at run-time #ae a per!ormance oerhead
C$$ does not dynamically bind till
e%plicitly as&ed
Special mechanism proided
-
7/24/2019 n Polymorph is m
4/32
NCST '
Object Oriented Programming Using C++
Polymorphism
virtual
Special construct De!ers method binding to run-time
Class has to tag a method as virtual
Method become irtual !or it and its
subclasses
(irtual cannot be )unirtualied+ by
subclasses
-
7/24/2019 n Polymorph is m
5/32
NCST ,
Object Oriented Programming Using C++
Polymorphism
Example
class Fruit {virtual void print();
};
class Apple { void print(); };
class Orange { void print(); };
Print
for ( items in bag ) bag.fruit->print()
-
7/24/2019 n Polymorph is m
6/32
NCST
Object Oriented Programming Using C++
Polymorphism
Polymorphism
Di!!erent ob.ects belonging to di!!erentclasses can respond to the same message in
di!!erent /ays0
Polymorphism allo/s a programmer to send
a message to an ob.ect /ithout concern
about ho/ the S is going to implementthe operation0
-
7/24/2019 n Polymorph is m
7/32NCST
Object Oriented Programming Using C++
Polymorphism
Polymorphism in C++
C$$ supports polymorphism at both early and
late binding0
3arly binding4static binding5
through operator and !unction oerloading0
6ate binding4dynamic binding5
through irtual !unctions 4pure polymorphism5
The ob.ects must be o! compatible types !or
polymorphism to /or&0
7n C$$ a public base class is compatible /ith its
deried classes0
-
7/24/2019 n Polymorph is m
8/32
NCST 8
Object Oriented Programming Using C++
Polymorphism
Polymorphism using Virtual Functions
class geom9ob. :
public ;000
irtual oid dra/4 5 < => ?* pure irtual *?@>
class circle ; public geom9ob. :public ;
000
oid dra/4 5> ?? dra/ circle
@>class line ; public geom9ob. :
public ;
oid dra/4 5> ?? dra/ line
@>
-
7/24/2019 n Polymorph is m
9/32
NCST A
Object Oriented Programming Using C++
Polymorphism
geom9ob. *gp>circle c>
line l>
000
gp < c> gp-dra/4 5> ?? Circle is dra/n
gp < l> gp-dra/4 5> ?? 6ine is dra/n
3%ample; sing (irtual Eunctions
-
7/24/2019 n Polymorph is m
10/32
NCST 1=
Object Oriented Programming Using C++
Polymorphism
Example: (contd
Feom9ob. *ob.9list GMHI9JBKL>?? array o! pointers to geometric ob.ects such circle
rectangle line 000 etc0
?? To dra/ all geom ob.ects in the ob.9list
!or 4i< => i MHI9JBK> i$$ 5
ob.9listGiL-dra/4 5> ?? dynamic binding used
-
7/24/2019 n Polymorph is m
11/32
NCST 11
Object Oriented Programming Using C++
Polymorphism
!"stract Class
Hn abstract class is one that is designed to representabstract concepts !or /hich ob.ects cannot e%ist0
7t is not used to create ob.ects0
H class /ith a Pure (irtual !unction is an abstract class0
3%ample;
class geom9ob. : ?? class geom9ob. is an abstract class
public ;
irtual oiddra/4 5 < => ?? pure irtual !unction
000
@>
-
7/24/2019 n Polymorph is m
12/32
NCST 12
Object Oriented Programming Using C++
Polymorphism
Pure Virtual
Hbstract class e%pressed using pure irtualmethods
=0added to the method
7ndicates method ispure virtual 7nstances o! classes /ith pure irtual
methods cannot be created
-
7/24/2019 n Polymorph is m
13/32
NCST 1"
Object Oriented Programming Using C++
Polymorphism
Pure Virtual
Subclass has to proide de!inition !or pureirtual method
i! not proided it also becomes an abstract
class
Obj O i d P i U i C
-
7/24/2019 n Polymorph is m
14/32
NCST 1'
Object Oriented Programming Using C++
Polymorphism
Example
// abstract classclass !ape {
virtual void dra"()#$; // pure virtual
};
class %ine & public !ape {
void dra"();
};
Obj t O i t d P i U i C
-
7/24/2019 n Polymorph is m
15/32
NCST 1,
Object Oriented Programming Using C++
Polymorphism
!"stract Class
Oepresents an abstract concept eg0 Class Shape
7t only de!ines an inter!ace /ithout giing
any implementationNo ob.ects created o! this class
sed as the base class in a class hierarchy
Deried classes proide implementation o!
inter!ace
Obj t O i t d P i U i C+
-
7/24/2019 n Polymorph is m
16/32
NCST 1
Object Oriented Programming Using C++
Polymorphism
Example: !"stract classHn operating system may hide details o! its deice
driers behind an abstract deice drier inter!ace
class character9deice :
public;irtual int open4 5 < =>
irtual int close4const char*5 < =>
irtual int read4const char* int5 < =>irtual int /rite4const char* int5 < =>
??
@>
Obj t O i t d P i U i C+
-
7/24/2019 n Polymorph is m
17/32
NCST 1
Object Oriented Programming Using C++
Polymorphism
Example
!ape s' s;!ape *ps';
%ine l';
ps' # +l';
ps'->dra"();
s.dra"();
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
18/32
NCST 18
Object Oriented Programming Using C++
Polymorphism
Example
!ape s' s; // error!ape *ps'; // ok
%ine l';
ps' # +l'; // ok
ps'->dra"(); // ok
s.dra"(); // error
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
19/32
NCST 1A
Object Oriented Programming Using C++
Polymorphism
Example
class !ape {virtual void dra"()#$;
};
class ,losed!ape & public !ape {
};
class ect & public ,losed!ape {
void dra"();
};
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
20/32
NCST 2=
Object Oriented Programming Using C++
Polymorphism
Example
!ape s' s;!ape *ps';
,losed!ape cs';
,losed!ape *pcs';
ect r';
ps' # +r';
pcs' # +r';
pcs'->dra"();
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
21/32
NCST 21
Object Oriented Programming Using C++
Polymorphism
Example
!ape s' s; // error!ape *ps'; // ok
,losed!ape cs'; // error
,losed!ape *pcs'; // ok
ect r';
ps' # +r';
pcs' # +r';
pcs'->dra"();
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
22/32
NCST 22
Object Oriented Programming Using C++
Polymorphism
Virtual Destructor
Deallocator may use sie o! ob.ect duringdeletion
Problem i! Base* contains Deried instance
sie o! Base /ill be used
Base destructor declared irtual
deallocator gets proper sie
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
23/32
NCST 2"
Object Oriented Programming Using C++
Polymorphism
Virtual Destructor
sed /hen custom allocation?deallocationused
Choices
either declare destructor as irtual
re!rain !rom using sie during deletion
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
24/32
NCST 2'
Object Oriented Programming Using C++
Polymorphism
Example
class {() { cout 0 ctor1n0; }
2() { cout 0 dtor1n0; }
};
class 3 & public {
3() { cout 03 ctor1n0; }
23() { cout 03 dtor1n0; }
};
* 4 # ne" 3;delete 4;
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
25/32
NCST 2,
Object Oriented Programming Using C++
Polymorphism
#utput
3%pected ctor
3 ctor
3 dtor
dtor
Hctual
ctor
3 ctor
dtor
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
26/32
NCST 2
Object Oriented Programming Using C++
Polymorphism
Example
class {() { cout 0 ctor1n0; }
virtual 2() { cout 0 dtor1n0; }
};
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
27/32
NCST 2
Object Oriented Programming Using C++
Polymorphism
$ip
Proide irtual destructor in all classes thatact as base class /hen deried type is
manipulated through base pointer
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
28/32
NCST 28
Object Oriented Programming Using C++
Polymorphism
Virtual Constructor
Not possible Constructor needs to &no/ e%act type o!
ob.ect to be created
Constructor is not a normal !unctionNot called on an e%isting ob.ect
Pointer to constructor not possible
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
29/32
NCST 2A
j g g g+
Polymorphism
Cloning
Similar !unctionality can be proided bye%plicit irtual !unction
(irtual !unction oerridden by subclasses
sed !or ob.ect )cloning+
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
30/32
NCST "=
j g g g+
Polymorphism
Example
class Fruit {virtual Fruit* clone()#$;
};
class Apple {
virtual Fruit* clone() {
return ne" Apple();
}
};
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
31/32
NCST "1
j g g g+
Polymorphism
Example
Fruit *f' *f;
f' # bag.fruit();
f # f'->clone(); // f2 is Apple if f1 is Apple*
// f2 is Orange if f1 is Orange*
Object Oriented Programming Using C+
-
7/24/2019 n Polymorph is m
32/32
NCST "2
j g g g+
P l hi
Diagrammatic %epresentation
Bag
o1
Eruit*
a1
Copy o!
a1
f'->clone()
Still an Apple
a1a1