Object-Oriented Programming 3 0 0 3
Object-Oriented Programming 3 0 0 3
Object-Oriented Programming 3 0 0 3
OBJECT-ORIENTED PROGRAMMING 3 0 0 3
(Common to CSE & IT)
Aim: To understand the concepts of object-oriented programming and master OOP using C!
UNIT I
9
Object oriented programming concepts " objects " c#asses " methods and messages " abstraction and
encapsu#ation " inheritance " abstract c#asses " po#$morphism! Introduction to C " c#asses " access
specifiers " function and data members " defau#t arguments " function o%er#oading " friend functions " const
&
and %o#ati#e functions - static members " Objects " pointers and objects " constant objects " nested c#asses "
#oca# c#asses
UNIT II
9
Constructors " defau#t constructor " Parameteri'ed constructors " Constructor (ith d$namic a##ocation " cop$
constructor " destructors " operator o%er#oading " o%er#oading through friend functions " o%er#oading the
assignment operator " t$pe con%ersion " e)p#icit constructor
UNIT III 9
*
+unction and c#ass temp#ates - E)ception hand#ing " tr$-catch-thro( paradigm " e)ception specification "
terminate and une)pected functions " ,ncaught e)ception!
UNIT IV 9
Inheritance " pub#ic- pri%ate- and protected deri%ations " mu#tip#e inheritance - %irtua# base c#ass " abstract
c#ass " composite objects .untime po#$morphism " %irtua# functions " pure %irtua# functions " .TTI " t$peid "
d$namic casting " .TTI and temp#ates " cross casting " do(n casting !
UNIT V 9
Streams and formatted I/O " I/O manipu#ators - fi#e hand#ing " random access " object seria#i'ation "
namespaces - std namespace " 01SI String Objects " standard temp#ate #ibrar$!
2
Total: 45
TEXT BOO!:
1! 3! Tri%edi- 4Programming (ith 01SI C5- O)ford ,ni%ersit$ Press- &667!
RE"ERENCE!:
1! Ira Poh#- 4Object Oriented Programming using C5- Pearson Education- Second Edition .eprint
&662!!
8
&! S! 3! 9ippman- :osee 9ajoie- 3arbara E! ;oo- 4C Primer5- +ourth Edition- Pearson Education-
&668!
*! 3! Stroustrup- 4The C Programming #anguage5- Third edition- Pearson Education- &662!
<
UNIT I
9
Object oriented programming concepts " objects " c#asses " methods and messages " abstraction and
encapsu#ation " inheritance " abstract c#asses " po#$morphism!
Introduction to C " c#asses " access specifiers " function and data members " defau#t arguments " function
o%er#oading " friend functions " const and %o#ati#e functions - static members " Objects " pointers and objects
" constant objects " nested c#asses " #oca# c#asses
7
# Ma$%&
'( Gi)* t+* *)ol,tio- .ia/$am o0 OOP! 1o-1*2t3
;achine #anguage
Procedure #anguage
0ssemb#$ #anguage
OOPS
#( 4+at i& P$o1*.,$* o$i*-t*. la-/,a/*5
Con%entiona# programming- using high-#e%e# #anguage such as CO3O9- +O.T.01 and C are common#$
=
>no(n as Procedure oriented #anguage (POP)! In POP number of functions are (ritten to accomp#ish the tas>s
such as reading- ca#cu#ating and printing!
3( Gi)* &om* 1+a$a1t*$i&ti1& o0 2$o1*.,$*-o$i*-t*. la-/,a/*3
? Emphasis is on doing things (a#gorithms)!
? 9arger programs are di%ided into sma##er programs >no(n as functions!
? ;ost of the functions share g#oba# data!
? @ata mo%e open#$ around the s$stem from function to function!
? Emp#o$s top-do(n approach in program design!
A
4( 4$it* a-6 0o,$ 0*at,$*& o0 OOP!3
? Emphasis is on data rather than on procedure!
? Programs are di%ided into objects!
? @ata is hidden and cannot be accessed b$ e)terna# functions!
? +o##o(s bottom -up approach in program design!
5( 4+at a$* t+* 7a&i1 1o-1*2t& o0 OO!5
? Objects!
? C#asses!
? @ata abstraction and Encapsu#ation!
? Inheritance!
16
? Po#$morphism!
? @$namic binding!
? ;essage passing!
8( 4+at a$* o79*1t&5
Objects are basic run-time entities in an object-oriented s$stem! The$ ma$ represent a person- a p#ace- a ban>
account- a tab#e of data or an$ item that the program has to hand#e! Each object has the data and code to
manipu#ate the data and theses objects interact (ith each other!
:(4+at i& a 1la&&5
? The entire set of data and code of an object can be made a user-defined data t$pe (ith the he#p of a c#ass!
11
? Once a c#ass has been defined- (e can create an$ number of objects be#onging to the c#asses!
? C#asses are user-defined data t$pes and beha%e #i>e bui#t-in t$pes of the programming #anguage!
=( ;+at i& *-1a2&,latio-5
Brapping up of data and function (ithin the structure is ca##ed as encapsu#ation!
9(4+at i& .ata a7&t$a1tio-5
The insu#ation of data from direct access b$ the program is ca##ed as data hiding or information binding!
The data is not accessib#e to the outside (or#d and on#$ those functions- (hich are (rapped in the c#ass- can
access it!
1&
'0(4+at a$* .ata m*m7*$& a-. m*m7*$ 0,-1tio-&5
C#asses use the concept of abstraction and are defined as a #ist of abstract attributes such as si'e- (eight- and
cost and uses functions to operate on these attributes! The attributes are sometimes ca##ed as data members
because the$ ho#d information! The functions that operate on these data are ca##ed as methods or member
functions!
EgC int a-bD // a-b are data members
Eoid getdata ( ) D // member function
''(4+at i& .6-ami1 7i-.i-/ o$ lat* 7i-.i-/5
3inding refers to the #in>ing of a procedure to the code to be e)ecuted in response to the ca##! @$namic binding
1*
means that the code associated (ith a gi%en procedure ca## is not >no(n unti# the time of the ca## at the run-
time!
'#(4$it* t+* 2$o1*&& o0 2$o/$ammi-/ i- a- o79*1t-o$i*-t*. la-/,a/*5
? Create c#asses that define objects and their beha%ior!
? Creating objects from c#ass definition!
? Estab#ishing communication among objects!
'3(Gi)* a-6 0o,$ a.)a-ta/*& o0 OOP!3
? The princip#e of data hiding he#ps the programmer to bui#d secure programs that cannot be in%aded b$ code
in other parts of the program!
12
? It is possib#e to ha%e mu#tip#e instances of an object to co-e)ist (ithout an$ interference!
? Object oriented programming can be easi#$ upgraded from sma## to #arge s$stems!
? Soft(are comp#e)it$ can be easi#$ managed!
'4(4+at a$* t+* 0*at,$*& $*<,i$*. 0o$ o79*1t-7a&*. 2$o/$ammi-/ =a-/,a/*5
? @ata encapsu#ation!
? @ata hiding and access mechanisms!
? 0utomatic initia#i'ation and c#ear up of objects!
? Operator o%er#oading!
18
'5(4+at a$* t+* 0*at,$*& $*<,i$*. 0o$ o79*1t o$i*-t*. la-/,a/*5
? @ata encapsu#ation!
? @ata hiding and access mechanisms!
? 0utomatic initia#i'ation and c#ear up of objects!
? Operator o%er#oading!
? Inheritance!
? @$namic binding!
'8(Gi)* a-6 0o,$ a22li1atio-& o0 OOP!
? .ea#-time s$stems!
? Simu#ation and mode#ing!
1<
? Object-oriented databases!
? 0I and e)pert s$stems!
':( Gi)* a-6 0o,$ a22li1atio-& o0 1>>5
? Since c a##o(s us to create hierarch$-re#ated objects- (e can bui#d specia# object-oriented #ibraries- (hich
can be used #ater b$ man$ programmers!
? C are easi#$ maintainab#e and e)pandab#e!
? C part of C gi%es the #anguage the abi#it$ to get c#ose to the machine-#e%e# detai#s!
? It is e)pected that C (i## rep#ace C as a genera#-purpose #anguage in the near future!
17
'?( 4+at a$* to%*-&5
The sma##est indi%idua# units in a program are >no(n as to>ens! C has the fo##o(ing to>ens-
? Fe$(ord
? Identifiers
? Constants
? Strings
? Operator
'9(4+at a$* %*6;o$.&5
The >e$(ords imp#ement specific C #anguage features! The$ are e)p#icit#$ reser%ed identifiers and cannot
be used as names fro the program %ariab#es or other user defined program e#ements!
EgC go to- If- struct - e#se -union etc!
1=
#0( R,l*& 0o$ -ami-/ t+* i.*-ti0i*$& i- C>>3
? On#$ a#phabetic characters- digits and underscore are permitted!
? The name cannot start (ith a digit!
? The upper case and #o(er case #etters are distinct!
? 0 dec#ared >e$(ord cannot be used as a %ariab#e name!
#'(4+at a$* t+* o2*$ato$& a)aila7l* i- C>>5
0## operators in C are a#so used in C! In addition to insertion operator GG and e)traction operator HH the
other ne( operators in C are-
C C Scope reso#ution operator
1A
C C I Pointer-to-member dec#arator
-HI Pointer-to-member operator
!I Pointer-to-member operator
de#ete ;emor$ re#ease operator
end# 9ine feed operator
ne( ;emor$ a##ocation operator
set( +ie#d (idth operator
##(4+at i& a &1o2* $*&ol,tio- o2*$ato$5
Scope reso#ution operator is used to unco%er the hidden %ariab#es! It a#so a##o(s access to g#oba# %ersion of
%ariab#es!
&6
EgC
Jinc#udeGiostream! hH
int mK16D // g#oba# %ariab#e m
%oid main ( )
L
int mK&6D // #oca# %ariab#e m
coutGG5mK5GGmGG5Mn5D
coutGG5C C mK5GGC C mGG5Mn5D
N
outputC
&6
&1
16 (C C m access g#oba# m)
Scope reso#ution operator is used to define the function outside the c#ass!
S$nta)C
.eturn t$pe Gc#ass nameH C C Gfunction nameH
EgC
Eoid ) C C getdata()
#3( 4+at a$* 0$** &to$* o2*$ato$& @o$( M*mo$6 ma-a/*m*-t o2*$ato$&5
1e( and @e#ete operators are ca##ed as free store operators since the$ a##ocate the memor$ d$namica##$!
1e( operator can be used to create objects of an$ data t$pe!
&&
Pointer-%ariab#e K ne( data t$peD
Initia#i'ation of the memor$ using ne( operator can be done! This can be done as-
Pointer-%ariab#e K ne( data-t$pe(%a#ue)
@e#ete operator is used to re#ease the memor$ space for reuse! The genera# form of its use is
@e#ete pointer-%ariab#eD
#4( 4+at a$* ma-i2,lato$&5
Set(- end# are >no(n as manipu#ators!
;anipu#ators are operators that are used to format the disp#a$! The end# manipu#ator (hen used in an output
statement causes a #inefeed to be inserted and its effect is simi#ar to that of the ne(#ine character5Mn5!
EgCCoutGGset((8)GGsumGGend#D
&*
#5( 4+at .o 6o, m*a- 76 *-,m*$at*. .atat62*5
0n enumerated datat$pe is another user-defined datat$pe- (hich pro%ides a (a$ for attaching names to
numbers- thereb$ increasing comprehensibi#it$ of the code!The s$nta) of an enum statement is simi#ar to that
of the struct statesmen!
EgC
enum shapeL circ#e- sOuare- triang#eN
enum co#orL red- b#ue- green- $e##o(N
#8( 4+at a$* &6m7oli1 1o-&ta-t&5
There are t(o (a$s for creating s$mbo#ic constants in CC
? ,sing the Oua#ifier constant!
&2
? @efining a set of integer constants using enum >e$(ord!
The program in an$ (a$ cannot modif$ the %a#ue dec#ared as constant in c!
EgC
Const int si'e K16D
Char name Psi'eQD
#:(4+at .o 6o, m*a- 76 .6-ami1 i-itialiAatio- o0 )a$ia7l*&5
C permits initia#i'ation of the %ariab#es at run-time! This is referred to as d$namic initia#i'ation of %ariab#es!
In C -a %ariab#e can be initia#i'ed at run-time using e)pressions at the p#ace of dec#aration as-
RR!!
R!!!!!!
&8
int n Kstr#en(string)D
RR!!
f#oat areaK*!12IradIradD
Thus dec#aration and initia#i'ation is done simu#taneous#$ at the p#ace (here the %ariab#e is used for the first
time!
#?( 4+at a$* $*0*$*-1* )a$ia7l*5
0 reference %ariab#e pro%ides an a#ias(a#ternati%e name) for a pre%ious#$ defined %ariab#e!
sum tota# +or e)amp#e - if ma>e the %ariab#e a reference to the %ariab#e - then sum and tota# can be used
interchancheab#$ to represent that %ariab#e!
S$nta) C
&<
@ata-t$pe &reference-name K %ariab#e-name
EgC
f#oat tota# K 166D
f#oat sum K tota#D
#9( 4+at i& m*m7*$-.*$*0*$*-1i-/ o2*$ato$5
C permits to access the c#ass members through pointers! It pro%ides three pointer-to-member operators for
this purpose-
C CI To dec#are a pointer to a member of a c#ass!
I To access a member using object name and a pointer to the member
-HI To access a member using a pointer to the object and a pointer to that member!
&7
30( 4+at i& 0,-1tio- 2$otot62* 5
The function protot$pe describes function interface to the compi#er b$ gi%ing detai#s such as number -t$pe of
arguments and t$pe of return %a#ues
+unction protot$pe is a dec#aration statement in the ca##ing program and is of the
fo##o(ing
T$pe functionSname(argument #ist)D Eg f#oat %o#ume(int )-f#oat $)D
3'(;+at i& a- i-li-* 0,-1tio- 5
0n in#ine function is a function that is e)panded in #ine (hen it is in%o>ed! That is compi#er rep#aces the
function ca## (ith the corresponding function code!
&=
The in#ine functions are defined as In#ine function-header
L
function bod$
N
3#( 4$it* &om* &it,atio-& ;+*$* i-li-* *B2a-&io- ma6 -ot ;o$%
? for functions returning %a#ues- if #oop- a s(itch- or a goto e)ists
? for functions not returning %a#ues -if a return statement e)ists
? if function contain static %ariab#es
? if in#ine functions are recursi%e
&A
33(;+at i& a .*0a,lt a$/,m*-t 5
@efau#t arguments assign a defau#t %a#ue to the parameter- (hich does not ha%e matching argument in the
function ca##! @efau#t %a#ues are specified (hen the f unction is dec#ared!
Eg C f#oat amount(f#oat princip#e- int period- f#oat rateK6! 18)
+unction ca## is
Ea#ueKamount(8666-7)D
Tere it ta>es princip#eK8666& periodK7
0nd defau#t %a#ue for rateK6!18
Ea#ueKamount(8666-7-6!*2)
Passes an e)p#icit %a#ue 6f 6!*2 to rate
Be must add defau#t %a#ue from right to #eft
*6
34( 4+at a$* 1o-&ta-t a$/,m*-t& 5
>e$(ord is const! The Oua#ifier const te##s the compi#er that the function shou#d not modif$ the argument! The
compi#er (i## generate an error (hen this condition is %io#ated! This t$pe of dec#aration is significant on#$
(hen (e pass arguments b$ reference or pointers
egC int str#en( const char Ip)D
35( Co; t+* 1la&& i& &2*1i0i*. 5
Uenera##$ c#ass specification has t(o parts
? c#ass dec#aration
It describes the t$pe and scope of its member
*1
? c#ass function definition
It describes ho( the c#ass functions are imp#emented
The genera# form is
C#ass c#assSname
L
pri%ateC
%ariab#e dec#arationsD
function dec#arationD
pub#icC
%ariab#e dec#arationD
*&
function dec#arationD
ND
38( Co; to 1$*at* a- o79*1t 5
Once the c#ass has been dec#ared- (e can create %ariab#es of that t$pe b$ using the c#assname
EgCc#assname )D //memor$ for ) is created
3:( Co; to a11*&& a 1la&& m*m7*$ 5
object-name! function-name(actua# arguments)
egC)!getdata(166-78!8)D
**
3?( Co; t+* m*m7*$ 0,-1tio-& a$* .*0i-*. 5
;ember functions can be defined in t(o (a$s
? outside the c#ass definition
;ember function can be defined b$ using scope reso#ution operatorCC
Uenera# format is
.eturn t$pe c#assS nameCCfunction-name(argument dec#aration)
L
N
? Inside the c#ass definition
This method of defining member function is to rep#ace the function dec#aration b$ the actua# function
*2
definition inside the c#ass! It is treated as in#ine function
EgCc#ass item
L
int a-b D
%oid getdata(int )-int $)
L
aK)D
bK$D
ND
39( 4+at i& &tati1 .ata m*m7*$5
Static %ariab#e are norma##$ used to maintain %a#ues common to the entire c#ass!
*8
+eatureC
? It is initia#i'ed to 'ero (hen the first object is created! 1o other initia#i'ation is permitted
? on#$ one cop$ of that member is created for the entire c#ass and is shared b$ a## the objects
? It is on#$ %isib#e (ithin the c#ass- but its #ife time is the entire c#ass t$pe and scope of each static member
%ariab#e must be defined outside the c#ass
? It is stored separate#$ rather than objects
EgC static int count//count is initia#i'ed to 'ero (hen an object is created!
int c#assnameCCcountD//definition of static data member
40( 4+at i& &tati1 m*m7*$ 0,-1tio-5
0 member function that is dec#ared as static has the fo##o(ing properties
*<
? 0 static function can ha%e access to on#$ other static member dec#ared in the same c#ass
? 0 static member function can be ca##ed using the c#assname as fo##o(s
c#assname CCfunctionSnameD
4'( Co; t+* o79*1t& a$* ,&*. a& 0,-1tio- a$/,m*-t5
This can be done in t(o (a$s
? 0 cop$ of the entire object is passed to the argument
? On#$ address of the objects is transferred to the f unction
4#( 4+at i& 1all*. 2a&& 76 $*0*$*-1*5
In this method address of an object is passed- the ca##ed function (or>s direct#$ on the actua# arguments!
*7
43( D*0i-* 1o-&t m*m7*$
If a member function does not a#ter an$ data in the c#ass- then (e ma$ dec#are it as const member function as
Eoid mu#(int -int)constD
44( D*0i-* 2oi-t*$& to m*m7*$
It is possib#e to ta>e the address of a member of a c#ass and assign it to a pointer! The address of a member can
be obtained b$ app#$ing the operator &to a 4fu##$ Oua#ified5 c#ass member name! 0 c#ass member pointer can
be dec#ared using the operatorCCI(ith the c#ass name!
EgC c#ass 0
L
*=
int mD
pub#icC
%oid sho(( )D
ND
pointer to member m is defined as
int 0CCIipK&0CCmD
0CCI-Hpointer to member of 0 c#ass
&0CCm-Haddress of the m member of 0 c#ass
45( 4+*- t+* .*0*$*-1i-/ o2*$ato$ -DE i& ,&*.5
It is used to access a member (hen (e use pointer to both the object and the member!
*A
27) Bhen the deferencing operator !I is usedV
It is used to access a member (hen the object itse#f is used as pointers!
2=) @efine #oca# c#asses!
C#asses can be defined and used inside a function or a b#oc>! such c#asses are ca##ed #oca# c#asses! It can use
g#oba# %ariab#es and static %ariab#es dec#ared inside the function but cannot use automatic #oca# %ariab#es!
EgD
%oid test(int a)
L
RR!
N
c#ass student
26
L
RRR
ND
student s1(a)D
N
48( 4+at a$* "$i*-. 0,-1tio-&5 4$it* t+* &6-taB
0 function that has access to the pri%ate member of the c#ass but is not itse#f a member of the c#ass is ca##ed
friend functions!
The genera# form is
21
friend dataSt$pe functionSname( )D
+riend function is preceded b$ the >e$(ord WfriendX!
4:(4$it* &om* 2$o2*$ti*& o0 0$i*-. 0,-1tio-&3
? +riend function is not in the scope of the c#ass to (hich it has been dec#ared as friend! Tence it cannot be
ca##ed using the object of that c#ass!
? ,sua##$ it has object as arguments!
? It can be dec#ared either in the pub#ic or pri%ate part of a c#ass!
? It cannot access member names direct#$! It has to use an object name and dot membership operator (ith each
member name! egC ( 0 ! ) )
2&
4?( 4+at i& 0,-1tio- o)*$loa.i-/5 Gi)* a- *Bam2l*3
+unction o%er#oading means (e can use the same function name to create functions that perform a %ariet$ of
different tas>s!
EgC 0n o%er#oaded add ( ) function hand#es different data t$pes as sho(n be#o(!
// @ec#arations
i! int add( int a- int b)D //add function (ith & arguments of same t$pe
ii! int add( int a- int b- int c)D //add function (ith * arguments of same t$pe
iii! doub#e add( int p- doub#e O)D //add function (ith & arguments of
different t$pe
//+unction ca##s
2*
add (* - 2)D //uses protot$pe ( i! )
add (*- 2- 8)D //uses protot$pe ( ii! )
add (* - 16!6)D //uses protot$pe ( iii! )
4?( D*0i-* lo1al 1la&&*&3
C#asses can be defined and used inside a function or a b#oc>! such c#asses are ca##ed
#oca# c#asses! It can use g#oba# %ariab#es and static %ariab#es dec#ared inside the function
but cannot use automatic #oca# %ariab#es!
EgD
%oid test(int a)
L
22
RR!
N
c#ass student
L
RRR
ND
student s1(a)DN
PART-B
1) @escribe the ad%antages of OOP(=)
&) Bhat are the differences bet(een pointers to constants and constant pointersV(=)
28
*) @escribe the app#ications of OOP techno#og$!
2) E)p#ain object-oriented paradigm (ith a#1its essentia# e#ements
8) State the merits and demerits of object oriented methodo#og$
<) E)p#ain the fo##o(ing concepts of object oriented programming in detai# (ith an e)amp#e!
(i) @ata abstraction
(ii) Inheritance
(iii) Po#$morphism
(i%) Objects
7)Brite short notes on access specifiers!
=)E)p#ain defau#t arguments (ith e)amp#e
A)E)p#ain about friend function (ith suitab#e e)amp#e
2<
16)Bhat is a static member and (hat are common characteristics
11)E)p#ain about pointers (ith e)amp#e
UNIT II
9
Constructors " defau#t constructor " Parameteri'ed constructors " Constructor (ith d$namic a##ocation " cop$
constructor " destructors " operator o%er#oading " o%er#oading through friend functions " o%er#oading the
assignment operator " t$pe con%ersion " e)p#icit constructor
27
'( D*0i-* 1o-&t$,1to$
0 constructor is a specia# member function (hose tas> is to initia#i'e the objects of its c#ass! It is specia#
because its name is same as c#ass name! The constructor is in%o>ed (hene%er an object of its associated c#ass
is created! It is ca##ed constructor because it constructs the %a#ues of data members of the c#ass
EgC
integer C#ass
L
RR
pub#icC
integer( )D//constructo r
2=
RRR
N
#( D*0i-* .*0a,lt 1o-&t$,1to$
The constructor (ith no arguments is ca##ed defau#t constructor
EgC
C#ass integer
L
int m-nD
Pub#icC
Integer( )D
2A
RR!
ND
integerCCinteger( )//defau#t constructor
L
mK6DnK6D
N
the statement
integer aD
in%o>es the defau#t constructor
3( D*0i-* 2a$am*t*$iA*. 1o-&t$,1to$
86
constructor (ith arguments is ca##ed parameteri'ed constructor
EgD
C#ass integer
L int m-nD
pub#icC
integer(int )-int $)
L mK)DnK$D
N
To in%o>e parameteri'ed constructor (e must pass the initia# %a#ues as arguments to the constructor function
(hen an object is dec#ared! This is done in t(o (a$s
1!3$ ca##ing the constructor e)p#icit#$
81
egC integer int1Kinteger(16-16)D
&!3$ ca##ing the constructor imp#icit#$
egC Integer int1(16-16)D
4( D*0i-* .*0a,lt a$/,m*-t 1o-&t$,1to$
The constructor (ith defau#t arguments are ca##ed defau#t argument constructor
EgC
Comp#e)(f#oat rea#-f#oat imagK6)D
The defau#t %a#ue of the argument imag is 6
The statement comp#e) a(<!6)
8&
assign rea#K<!6 and imagK6
the statement
comp#e) a(&!*-A!6)
assign rea#K&!* and imagKA!6
5( 4+at i& t+* am7i/,it6 7*t;**- .*0a,lt 1o-&t$,1to$ a-. .*0a,lt a$/,m*-t
1o-&t$,1to$ 5
The defau#t argument constructor can be ca##ed (ith either one argument or no arguments! (hen ca##ed (ith no
arguments -it becomes a defau#t constructor! Bhen both these forms are used in a c#ass -it cause ambiguit$ for a
statement such as 0 aD
The ambiguit$ is (hether to ca## 0CC0() or 0CC0(int iK6)
8*
8( D*0i-* 1o26 1o-&t$,1to$
0 cop$ constructor is used to dec#are and initia#i'e an object from another object! It ta>es a reference to an
object of the same c#ass as an argument
EgC integer i&(i1)D
(ou#d define the object i& at the same time initia#i'e it to the %a#ues of i1!
0nother form of this statement is
EgC integer i&Ki1D
The process of initia#i'ing through a cop$ constructor is >no(n as cop$ initia#i'ation !
82
:( D*0i-* .6-ami1 1o-&t$,1to$
0##ocation of memor$ to objects at time of their construction is >no(n as d$namic constructor! The memor$ is
a##ocated (ith the he#p of the 1EB operator
EgC
C#ass string
L
char InameD
int #engthD
pub#icC
string( )
L
88
#engthK6D
nameKne( charP #ength 1QD
N
%oid main( )
L
string name1(49ouis5)-name*(9agrange)D
N
?( D*0i-* 1o-&t o79*1t
Be can create constant object b$ using const >e$(ord before object dec#aration!
EgC Const matri) )(m-n)D
8<
9( D*0i-* .*&t$,1to$
It is used to destro$ the objects that ha%e been created b$ constructor! @estructor name is same as c#ass name
preceded b$ ti#de s$mbo#(Y)
EgD
Yinteger()
L
N
0 destructor ne%er ta>es an$ arguments nor it does it return an$ %a#ue! The compi#er upon e)it from the
87
program (i## in%o>e it! ne( Bhene%er operator is used to a##ocate memor$ in the constructor- (e shou#d
use de#ete to free that memor$!
'0( D*0i-* m,lti2l* 1o-&t$,1to$& @1o-&t$,1to$ o)*$loa.i-/(3
The c#ass that has different t$pes of constructor is ca##ed mu#tip#e constructors
EgC
Jinc#udeGiostream! hH
Jinc#udeGconio!hH
c#ass integer
L
int m-nD
8=
pub#icC
integer( ) //defau#t constructor
L
mK6DnK6D
N
integer(int a-int b) //parameteri'ed constructor
L
mKaD nKbD
N
integer(&i) //cop$ constructor
L
8A
mKi! mD
nKi!nD
N
%oid main()
L
integer i1D //in%o>es defau#t constructor
integer i&(28-<7)D//in%o>es parameteri'ed constructor
integer i*(i&)D //in%o>es cop$ constructor
N
''( 4$it* &om* &2*1ial 1+a$a1t*$i&ti1& o0 1o-&t$,1to$
<6
? T he$ shou#d be dec#ared in the pub#ic section
? The$ are in%o>ed automatica##$ (hen the objects are created
? The$ do not ha%e return t$pes- not e%en %oid and therefore- and the$ cannot return %a#ues
? The$ cannot be inherited- though a deri%ed c#ass can ca## the base c#ass
? The$ can ha%e defau#t arguments
? Constructors cannot be %irtua# f unction
'#( Co; t+* o79*1t& a$* i-itialiA*. .6-ami1all65
To ca## parameteri'ed constructor (e shou#d the pass %a#ues to the object ie-for the constructor integer(int a-int
b) it is in%o>ed b$ integer a(16-1=)
this %a#ue can be get during run time! i!e!- f or abo%e constructor
<1
int p-OD
cinHHpHHOD
integer a(p-O)D
'3(D*0i-* I-li-* ",-1tio-5
In#ine function is defined as a function definition such that each ca## to the function is in effect- rep#aced b$ the
statements that define the function! It is e)panded in #ine (hen it is in%o>ed! The genera# form is
in#ine function-header
L
<&
function bod$
N
'4(EB2lai- $*t,$- 76 $*0*$*-1* ;it+ a- *Bam2l*!
0 function can a#so return a reference! Consider the fo##o(ing function
int & ma)( int &) - int &$)
L if()H$)
return )D
e#se
return $D
N
<*
Since the return t$pe of ma) ( ) is int & the function returns reference to ) or $ (and not the %a#ues)! Then a
function ca## such as ma) ( a - b) (i## $ie#d a reference to either a or b depending on their %a#ues!
The statement
ma) ( a - b) K -1D
is #ega# and assigns "1 to a if it is #arger- other(ise "1 to b!
'5( 4+at i& o2*$ato$ o)*$loa.i-/5
C has the abi#it$ to pro%ide the operators (ith a specia# meaning for a data t$pe! This mechanism of gi%ing
such specia# meanings to an operator is >no(n as Operator o%er#oading! It pro%ides a f#e)ib#e option for the
creation of ne( definitions for C operators!
<2
'8( =i&t o,t t+* o2*$ato$& t+at 1a--ot 7* o)*$loa.*.3
? C#ass member access operator (! - !I)
? Scope reso#ution operator (CC)
? Si'e operator ( si'eof )
? Conditiona# operator (VC)
':( 4+at i& t+* 2,$2o&* o0 ,&i-/ o2*$ato$ 0,-1tio-5 4$it* it& &6-taB3
To define an additiona# tas> to an operator- (e must specif$ (hat it means in re#ation to the c#ass to (hich the
operator is app#ied! This is done b$ Operator function - (hich describes the tas>! Operator functions are either
member functions or friend functions! The genera# form is
<8
return t$pe c#assname CC operator (op-arg#ist )
L
function bod$
N
(here return t$pe is the t$pe of %a#ue returned b$ specified operation!
Op- operator being o%er#oaded! The op is preceded b$ a >e$(ord operator! operator op is
the function name!
'?( 4$it* at l*a&t 0o,$ $,l*& 0o$ O2*$ato$ o)*$loa.i-/3
? On#$ the e)isting operators can be o%er#oaded!
? The o%er#oaded operator must ha%e at #east one operand that is of user defined data t$pe!
<<
? The basic meaning of the operator shou#d not be changed!
? O%er#oaded operators fo##o( the s$nta) ru#es of the origina# operators! The$ cannot be o%erridden!
'9( Co; ;ill 6o, o)*$loa. U-a$6 F Bi-a$6 o2*$ato$ ,&i-/ m*m7*$ 0,-1tio-&5
Bhen unar$ operators are o%er#oaded using member functions it ta>es no e)p#icit arguments and return no
e)p#icit %a#ues! Bhen binar$ operators are o%er#oaded using member functions- it ta>es one e)p#icit argument!
0#so the #eft hand side operand must be an object of the re#e%ant c#ass!
#0( Co; ;ill 6o, o)*$loa. U-a$6 a-. Bi-a$6 o2*$ato$ ,&i-/ "$i*-. 0,-1tio-&5
Bhen unar$ operators are o%er#oaded using friend function- it ta>es one reference argument (object of the
re#e%ant c#ass) Bhen binar$ operators are o%er#oaded using friend function- it ta>es t(o e)p#icit
arguments!
<7
#'( Co; a- o)*$loa.*. o2*$ato$ 1a- 7* i-)o%*. ,&i-/ m*m7*$ 0,-1tio-&5
In case of ,nar$ operators- o%er#oaded operator can be in%o>ed as op objectSname or objectSname op
In case of binar$ operators- it (ou#d be in%o>ed as Object ! operator op($)
(here op is the o%er#oaded operator and $ is the argument!
##( Co; a- o)*$loa.*. o2*$ato$ 1a- 7* i-)o%*. ,&i-/ "$i*-. 0,-1tio-&5
In case of unar$ operators- o%er#oaded operator can be in%o>ed as Operator op ())D
In case of binar$ operators- o%er#oaded operator can be in%o>ed as Operator op () - $)
#3( =i&t o,t t+* o2*$ato$& t+at 1a--ot 7* o)*$loa.*. ,&i-/ "$i*-. 0,-1tio-3
<=
? 0ssignment operator K
? +unction ca## operator ( )
? Subscripting operator P Q
? C#ass member access operator
#4( EB2lai- 7a&i1 to 1la&& t62* 1o-)*$&io- ;it+ a- *Bam2l*3
Con%ersion from basic data t$pe to c#ass t$pe can be done in destination c#ass!
,sing constructors does it! Constructor ta>es a sing#e argument (hose t$pe is to be con%erted!
EgC Con%erting int t$pe to c#ass t$pe
c#ass time
L
<A
int hrs-minsD
pub#icC
RRRR!
Time ( int t) //constructor
L
hoursK t/<6 D //t in minutes
mins Kt Z <6D
N
ND
Constructor (i## be ca##ed automatica##$ (hi#e creating objects so that this con%ersion is done automatica##$!
76
#5( EB2lai- 1la&& to 7a&i1 t62* 1o-)*$&io- ;it+ a- *Bam2l*3
,sing T$pe Casting operator- con%ersion from c#ass to basic t$pe con%ersion can be done! It is done in the
source c#ass itse#f!
EgC %ector C C operator doub#e( )
L
doub#e sumK6D
for(int IK6DIGsi'eDI)
sumKsum%P i Q IuP i Q D
return sOrt ( sum ) D
N
This function con%erts a %ector to the corresponding sca#ar magnitude!
71
#8( EB2lai- o-* 1la&& to a-ot+*$ 1la&& 1o-)*$&io- ;it+ a- *Bam2l*3
Con%ersion from one c#ass t$pe to another is the combination of c#ass to basic and basic to c#ass t$pe
con%ersion! Tere constructor is used in destination c#ass and casting operator function is used in source c#ass!
EgC obj[ K obj\
obj[ is the object of c#ass [ and obj\ is an object of c#ass \! The c#ass \ t$pe data is con%erted into c#ass [
t$pe data and the con%erted %a#ue is assigned to the obj [! Tere c#ass \ is the source c#ass and c#ass [ is the
destination c#ass!
7&
PART-B
'(E)p#ain cop$ constructor (ith suitab#e C coding!
#(9ist out the ru#es for o%er#oading operators
3(State the ru#es to be fo##o(ed (hi#e o%er#oading an operator! Brite a program to i##ustrate an o%er#oading
4(Bhat is operator o%er#oadingV To( man$ arguments are reOuired in the definition of an o%er#oaded binar$
operator
5(Bhat is constructorV E)p#ain the t$pes of constructor (ith an suitab#e e)amp#eBrite a program to o%er#oad K
operator! 0ssign %a#ues of data members of one object to another object of the same t$pe!
<)E)p#ain defau#t constructors (ith suitab#e e)amp#e
7*
7)E)p#ain constructor (ith @$namic 0##ocation gi%e e)amp#e
=)Brite short notes onC
i)T$pe con%ersions
ii)E)p#icit constructor
72
78
7<
UNIT III 9
+unction and c#ass temp#ates - E)ception hand#ing " tr$-catch-thro( paradigm " e)ception specification "
terminate and une)pected functions " ,ncaught e)ception!
'( 4+at a$* EB1*2tio-&5
77
E)ceptions (hich occur during the program e)ecution- due to some fau#t in the input data
#( 4+at a$* t+* t;o t62*& o0 *B1*2tio-&5
E)ceptions are c#assifieds into
a)S$nchronous e)ception
b) 0s$nchronous e)ception
3( 4+at i& a &6-1+$o-o,& *B1*2tio-5
The techniOue that is not suitab#e to hand#e the current c#ass of data- (ithin the program are >no(n as
s$nchronous e)ception
7=
4( 4+at i& a- A&6-1+$o-o,& *B1*2tio-5
The e)ceptions caused b$ e%ents or fau#ts unre#ated to the program and be$ond the contro# of program
are ca##ed as$nchronous e)ceptions
5( 4+at a$* t+* 7lo1%& ,&*. i- t+* EB1*2tio- Ca-.li-/5
The e)ception-hand#ing mechanism uses three b#oc>s
1)tr$ b#oc>
&)thro( b#oc>
*)catch b#oc>
The t$6-7lo1% must be fo##o(ed immediate#$ b$ a hand#er-(hich is a 1at1+-7lo1%3
If an e)ception is thro(n in the t$6-7lo1%
7A
8( D$a; t+* EB1*2tio- +a-.li-/ mo.*l5
In%o>e function ha%ing thro( b#oc>
Tr$ b#oc>
Perform operation (hich ma$
thro( or in%o>e e)terna# function
if needed
Thro( b#oc>
If(fai#ure)
Thro( objectD
=6
e)ception
e)ception
Catch b#oc>
Catches a## e)ceptions thro(n from
(ithin tr$ b#oc> or b$ function
in%o>ed (ithin a tr$ b#oc>
=1
:( 4$it* t+* &6-taB o0 t$6 1o-&t$,1t
The tr$ >e$(ord defines a boundar$ (ithin (hich an e)ception can occur!0 b#oc> of code in (hich an
e)ception can occur must be prefi)ed b$ the >e$(ord tr$!+o##o(ing the tr$ >e$(ord is a b#oc> of code
enc#osed b$ braces!This indicates that the program is prepared to test for the e)istence of e)ceptions
=&
Fe$(ord
tr$
L
//code raising e)ception or referring to a function raising e)ception
N
catch(t$peSid1)
L
//actions for hand#ing an e)ception
N
=*
R
R
catch(t$peSidn)
L
//actions for hand#ing an e)ception
N
?( 4$it* t+* &6-taB o0 1at1+ 1o-&t$,1t
The e)ception hand#er is indicated b$ the catch >e$(ord!It must be used immediate#$ after the
statements mar>ed b$ the tr$ >e$(ord!The catch hand#er can a#so occur immediate#$ after another catch!Each
=2
hand#er (i## on#$ e%a#uate an e)ception that matches-or can be co%ered to the t$pe specified in its argument
#ist!
Fe$(ord object name or name#ess object
Catch(T)
L
// actions for hand#ing an e)ception
N
=8
9( 4$it* t+* &6-taB o0 t+$o; 1o-&t$,1t
The >e$(ord thro( is used to raise an e)ception (hen an error is generated in the computation!The
thro( e)pression initia#i'es a temporar$ object of the t$pe T(to match the t$pe of arg$ment arg)used in
thro((T arg)
1amed object- name#ess object
Fe$(ord
=<
thro( TD
'0( =i&t o,t t+* ta&%& to 7* 2*$0o$m*. 0o$ *$$o$ +a-.li-/ 1o.*
1)@etect the prob#em causing e)ception(Tit the e)ception)
&)Inform that an error has occurred(Thro( the e)ception)
*).ecei%e the error information(Catch the e)ception)
2)Ta>e correcti%e actions(Tand#e the e)ceptions)
''( 4$it* t+* &t*2& to 7* 2*$0o$m*. ;+*- a- *B1*2tio- i& $ai&*.
1)The program searches for a matching hand#er
&)If a hand#er is found- the stac> is un(ound to that point
*)Program contro# is transferred to the hand#er
=7
2)If no hand#er is found- the program (i## in%o>e the terminate()function! If no e)ceptions are thro(n-
the program e)ecutes in the norma# fashion
'#( 4$it* t+* &6-taB o0 &2*1i06i-/ a li&t o0 *B1*2tio-&
",-1tio- .*0i-itio- =i&t o0 *B1*2tio-& t+at 1a- 7* $ai&*.
+unctionSpecification thro((t$pe id1-t$pe id&-RR)
==
L
//+unction bod$ raising e)ceptions if error occurs
N
'3( 4$it* t+* &6-taB o0 1at1+ all 1o-&t$,1t
Three dotsCindicate catch a## e)ceptions
Catch(R)
=A
L
//actions for hand#ing an e)ception
N
'4( 4+at i& a &*1,$* o2*$atio-5
0## destructors in a C program shou#d ha%e an empt$ specification thro(()!This is ca##ed secure
operations
'5( 4+at i& &ta1% ,-;i-.i-/5
The process of ca##ing destructor for automatic objects constructed on the path from a tr$-b#oc> to a
thro(n e)pression is ca##ed stac> un(inding
A6
'8( 4$it* t+* 0,-1tio-& to +a-.l* ,-1a,/+t *B1*2tio-&
1)terminate()
&)setSterminate()
*)une)pected()
2)setSune)pected()
':( 4+at a$* t+* t62*& o0 0a,lt tol*$a-t .*&i/- t*1+-i<,*&
+au#t to#erant soft(are design techniOues can be c#assified into the fo##o(ing
1)1-%ersion programming
&).eco%er$ b#oc>
A1
'?( =i&t o,t &om* o0 t+* *$$o$ +a-.li-/ 0,-1tio-&
a)eof()-.eturns true if end-of-fi#e is encountered
b)fai#()-.eturns true (hen an input or output operation has fai#ed
c)bad()-.eturns true if in%a#id operation is attempted b$ or an$ unco%erab#e error has occurred
d)good()-.eturns true if no error has occurred
'9( 4+at a$* t+* t;o t62*& o0 *$$o$& o$ 7,/&5
a)9ogica# errors-errors occur due to poor understanding of the prob#em and so#ution procedure
b)S$ntatic errors-errors that arise due to poor understanding of the #anguage itse#f!
#0( 4+at i& 0,-1tio- t*m2lat*5
A&
0 function temp#ate specifies ho( an indi%idua# function can be constructed! The #imitation of
such functions is that the$ operate on#$ on a particu#ar data t$pe! It can be o%ercome b$ defining that function
as a function temp#ate or generic function!
#'(4$it* t+* &6-taB o0 0,-1tio- t*m2lat*
temp#ateGc#ass T-RH
.eturnT$pe +unc1ame(arguments)
L
R!!//bod$ of the function temp#ate
R!!
A*
N
##(4+at i& 1la&& t*m2lat*5
C#asses can a#so be dec#ared to operate on different data t$pes! Such c#asses are ca##ed c#ass temp#ates!
0 c#ass temp#ate specifies ho( indi%idua# c#asses can be constructed simi#ar to norma# c#ass specification
#3(4$it* t+* &6-taB o0 1la&& t*m2lat*
temp#ate Gc#ass T1-c#ass T&-RH
c#ass c#assname
L
A2
T1 data1D
R!
//functions of temp#ate arguments T1-T&-R!
%oid func1(T1 a-T& &b)D
R
T func&(T& I)-T& I$)D
ND
PART-B
A8
1) @iscuss the need for e)ception (ith tr$- catch and thro( >e$(ords
&) Bhat is +unction Temp#ateV Brite the s$nta) for function temp#ate! Brite an e)amp#e program for function
temp#ate!
*) Bhat is c#ass temp#ateV Brite the s$nta) for c#ass temp#ate! Brite an e)amp#e program for c#ass temp#ate!
2) Bhat are E)ceptionsV Bhat are the t(o t$pes of e)ceptionsV @ra( the E)ception hand#ing mode#V
8) Bhat are uncaught e)ceptionsV Brite the functions to hand#e uncaught e)ceptions (ith a suitab#e e)amp#e
<) Bhat are E)ceptionsV Bhat are the t(o t$pes of e)ceptionsV Brite the s$nta) of tr$ construct! Brite the
s$nta) of catch construct! Brite the s$nta) of thro( construct (ith an e)amp#e
A<
UNIT IV 9
Inheritance " pub#ic- pri%ate- and protected deri%ations " mu#tip#e inheritance - %irtua# base c#ass " abstract
c#ass " composite objects .untime po#$morphism " %irtua# functions " pure %irtua# functions " .TTI " t$peid "
d$namic casting " .TTI and temp#ates " cross casting " do(n casting !
A7
'( 4+at i& m*a-t 76 i-+*$ita-1*5
Inheritance is the process b$ (hich objects of one c#ass acOuire the properties of another c#ass! It supports the
concept of hierarchica# c#assification! It pro%ides the idea of reusabi#it$! Be can add additiona# features to an
e)isting c#ass (ithout modif$ing it b$ deri%ing a ne( c#ass from it!
#(=i&t t+* $,l*& 0o$ i-+*$ita-1*
1)The defau#t mode is pri%ate
&)@ifficu#t to inherit the base c#ass pri%ate members
*)The base c#ass pub#ic and protected member functions are considered as pub#ic and protected
member functions in deri%ed c#ass-Bhen those c#ass is pub#ic#$ inherited from base c#ass
A=
2)The base c#ass pub#ic and protected member functions are considered as pri%ate member function in
deri%ed c#ass-(hen those c#ass is pri%ate#$ inherited from base c#ass
3(=i&t t+* 1la&& m*m7*$H& )i&i7ilit6
There are three %isibi#ities of c#ass members!The$ are
i) Pub#ic %isibi#it$
ii)Pri%ate %isibi#it$
iii)Protected %isibi#it$
4(EB2lai- t+* 2,7li1 )i&i7ilit6
AA
The c#ass members are %isib#e to the base c#ass-deri%ed c#asses and outside the c#ass through the
objects
5(EB2lai- t+* 2$i)at* )i&i7ilit6
The c#ass members are %isib#e on#$ to the base c#ass itse#f but not to the deri%ed c#ass
8(EB2lai- t+* 2$ot*1t*. )i&i7ilit6
The c#ass members are %isib#e to the base and deri%ed c#asses
:( 4+at i& m*a-t 76 &i-/l* i-+*$ita-1*5
If a sing#e c#ass is deri%ed from a sing#e base c#ass is ca##ed sing#e inheritance!
166
EgC
3ase c#ass
@eri%ed c#ass
Tere c#ass 0 is the base c#ass from (hich the c#ass @ is deri%ed! C#ass @ is the pub#ic
deri%ation of c#ass 3 hence it inherits a## the pub#ic members of 3! 3ut @ cannot access
pri%ate members of 3!
?( 4+at i& m,lti2l* i-+*$ita-1*5
If a c#ass is deri%ed from more than one base c#ass- it is ca##ed mu#tip#e inheritance!
EgC 3ase c#asses
161
@eri%ed c#ass
Tere c#ass C is deri%ed from t(o base c#asses 0 & 3!
9( 4+at i& +i*$a$1+i1al i-+*$ita-1*5
If a number of c#asses are deri%ed from a sing#e base c#ass then it is ca##ed
hierarchica# inheritance!
Eg C Tierarchica# c#assification of students in ,ni%ersit$
0
3
16&
0
C
3
'0( 4+at i& m,ltil*)*l i-+*$ita-1*5
If a c#ass is deri%ed from a c#ass- (hich in turn is deri%ed from another c#ass- is ca##ed mu#ti#e%e# inheritance!
This process can be e)tended to an$ number of #e%e#s!
EgC
3ase c#ass Urand father
Intermediate
16*
3ase c#ass +ather
@eri%ed c#ass Chi#d
''( 4+at i& +67$i. i-+*$ita-1*5
It is the combination of one or more t$pes of inheritance!
;u#ti#e%e#
inheritance
;u#tip#e
inheritance
The c#ass resu#t (i## ha%e both the mu#ti#e%e# and mu#tip#e inheritances!
162
'#( 4+at i& m*a-t 76 A7&t$a1t 7a&* 1la&&5
0 c#ass that ser%es on#$ as a base c#ass from (hich deri%ed c#asses are deri%ed! 1o objects of an abstract base
c#ass are created! 0 base c#ass that contains pure %irtua# function is an abstract base c#ass!
'3( 4$it* &+o$t -ot*& o- )i$t,al 7a&* 1la&&3
0 base c#ass that is Oua#ified as %irtua# in the inheritance definition! In case of mu#tip#e inheritance- if the base
c#ass is not %irtua# the deri%ed c#ass (i## inherit more than one cop$ of members of the base c#ass! +or a %irtua#
base c#ass on#$ one cop$ of members (i## be inherited regard#ess of number of inheritance paths bet(een base
c#ass and deri%ed c#ass!
EgC Processing of studentsX resu#ts! 0ssume that c#ass sports deri%e the ro##
168
number from c#ass student! C#ass test is deri%ed from c#ass Student! C#ass resu#t is
deri%ed from c#ass Test and sports! 0s a %irtua# base c#ass
'4(4+at a$* )i$t,al 0,-1tio-&5
0 function Oua#ified b$ the W%irtua#X >e$(ord is ca##ed %irtua# function! Bhen a %irtua# function is ca##ed
through a pointer- c#ass of the object pointed to determine (hich function definition (i## be used!
'5(4$it* &om* o0 t+* 7a&i1 $,l*& 0o$ )i$t,al 0,-1tio-&
? Eirtua# f unctions must be member of some c#ass!
? The$ cannot be static members and the$ are accessed b$ using object pointers
? Eirtua# f unction in a base c#ass must be defined!
16<
? Protot$pes of base c#ass %ersion of a %irtua# function and a## the deri%ed c#ass %ersions must be identica#!
? If a %irtua# function is defined in the base c#ass- it need not be redefined in the deri%ed c#ass!
'8( 4+at a$* 2,$* )i$t,al 0,-1tio-&5 4$it* t+* &6-taB3
0 pure %irtua# function is a function dec#ared in a base c#ass that has no definition re#ati%e to the base c#ass! In
such cases- the compi#er reOuires each deri%ed c#ass to either define the function or redec#are it as a pure %irtua#
function! 0 c#ass containing pure %irtua# functions cannot be used to dec#are an$ object of its o(n! It is a#so
>no(n as 4donothing5 function!
The 4do-nothing5 function is defined as fo##o(sC
%irtua# %oid disp#a$ ( ) K6D
':( 4+at i& 2ol6mo$2+i&m5
167
Po#$morphism is the abi#it$ to ta>e more than one form! 0n operation ma$ e)hibit different beha%iors in
different! The beha%ior depends upon the t$pe of data used!
'?(4+at a$* t+* t62*& o0 2ol6mo$2+i&m5
Ba&i1 T62*&
i).untime po#$morphism
ii)Compi#e time po#$morphism
Ma9o$ T62*&
a!.un-time po#$morphism
b!Compi#e time po#$morphism
16=
c!ad-hoc po#$morphism
d!Parametric po#$morphism
e!Eirtua# functions
f!+unction name o%er#oading
g!Operator o%er#oading
'9(4+at i& 1om2il* tim* 2ol6mo$2+i&m5
The o%er#oaded member functions are se#ected for in%o>ing b$ matching arguments both t$pe and
number!This information is >no(n to the compi#er at the compi#e time and therefore compi#er is ab#e to se#ect
the appropriate function for a particu#ar ca## at the compi#e time itse#f!This is ca##ed ear#$ binding or static
binding or static #in>ing!0#so >no(n as compi#e time po#$morphism
16A
#0(4+at t+* t62*& i- 1om2il* tim* 2ol6mo$2+i&m5
a)+unction o%er#oading
b)Operator o%er#oading
#'( 4+at i& RTTI5
.un-time t$pe information (.TTI) is a mechanism that a##o(s the t$pe of an object to be determined
during program e)ecution
##(=i&t O,t RTTI5
a)3ase T$pe
116
b)Compound T$pe
c)Container T$pe
#3(EB2lai- t+* ot+*$ 0,-1tio-& o0 RTTI
i)insert e#ements into the container
ii)de#ete e#ements of the container
iii)iterate through the e#ements of the container
#4(EB2lai- t+* t+$** 2a$t& o0 RTTI &6&t*m
1).TTI description of 3ase T$pes
&)Propert$ @escription of compound and container t$pes
111
*)Propert$ iterators
#5(4$it* t+* 2,$2o&* o0 t62*i. o2*$ato$
The t$peid operator pro%ides a program (ith the abi#it$ to retrie%e the actua# deri%ed t$pe of the object
referred to b$ a pointer or a reference!This operator is used a#ong (ith the d$namicScast operator
#8(4+at i& .6-ami1 1a&ti-/5
@$namic cast a##o(s us to do(ncast a data t$pe from one to a more specific one in the same hierarch$
#:(D*0i-* Do;-1a&ti-/
11&
@o(ncasting is used to cast a pointer or reference to a base c#ass to a deri%ed c#ass!@o(ncasting is the
opposite of the basic object-oriented ru#e-(hich states objects of a deri%ed c#ass-can a#(a$s be assigned to
%ariab#es of a base c#ass!Since base c#ass %ariab#es can on#$ sometimes be assigned to %ariab#es of a deri%ed
c#ass do(ncasting doesnXt a#(a$s (or>
#?(D*0i-* U21a&ti-/
,pcasting means c#asses can easi#$ con%erge to a genera# c#ass
PART-B
11*
1) @iscuss about po#$morphism and its ad%antages!
&) Brite a C program that (i## gi%e the conditions of en%ironment reOuired-
food habits and uniOue characteristics of pet anima#s fish and dog! @efine a
base ca##ed pet that describe an$ common househo#d petD t(o deri%ed c#asses ca##ed fish and dog (ith
items specific to that t$pe of anima#s! Brite pure %irtua# functions in the base c#ass for operations that are
common to both t$pes of anima#s! Brite a program to test the usage of c#asses!
*) E)p#ain h$brid inheritance (ith suitab#e C coding
2) @efine po#$morphism! E)p#ain the different t$pes of po#$morphism!
8) Brite a C program using inheritance to imp#ement the fo##o(ing! 0 ne(spaper agent pa$s t(o %ariant
rates for the de#i%er$ of ne(spapers! 0 de#i%er$ bo$ can earn .S! & on a morning de#i%er$ but on#$ .s!
1!86 on an e%ening de#i%er$! 3o$s are sa#aried either for a morning de#i%er$ or for an e%ening paper round
112
but not for both! .ead(Input) the code for round (6 for morning and 1 for e%ening) and the number of tota#
paper rounds in one (ee> done b$ a bo$ and compute his earning and disp#a$ it!
<) Bhat are the different >inds of inheritanceV E)p#ain them (ith suitab#e e)amp#es
7) 3rief#$ e)p#ain .TTI
=) @escribe %irtua# function (ith suitab#e e)amp#e
A) @efine pure %irtua# function
118
11<
UNIT V 9
Streams and formatted I/O " I/O manipu#ators - fi#e hand#ing " random access " object seria#i'ation "
namespaces - std namespace " 01SI String Objects " standard temp#ate #ibrar$!
'3 4+at a$* &t$*am&5
0 Stream is a seOuence of b$tes! It can either as a source from (hich the input data can be obtained or
as a destination to (hich the output data can be sent
The stream source that pro%ides data to the program is ca##ed the input stream and the destination
stream that recei%es output from the program is ca##ed output stream
117
#3 4+at a$* t+* &t$*am 1la&&*& 0o$ 1o-&ol* o2*$atio-&5
a)ios
b)istrean
c)ostream
d)iostream
e)streambuf
3( D$a; t+* 1o-&ol* &t$*am 1la&&*& 0o$ +i*$a$1+6
11=
istream
ios
streambuf
ostream
iostream
ostreamS(ithassign iostreamS(ithassign istreamS(ithassign
Streambuf
IbpK&strstreambaseCC
bug
11A
4( =i&t o,t &om* o0 t+* ,-0o$matt*. IIO o2*$ato$&3
a)put()
b)get()
c)get#ine()
d)(rite()
5( =i&t o,t &om* o0 t+* 0o$matt*. IIO o2*$atio-&
C supports a number of feature that cou#d be used for formatting the output!These features inc#ude
a)ios stream c#ass member functions and f#ags
b)Standard manipu#ators
c),ser "defined manipu#ators
1&6
8( =i&t &om* o0 t+* io& 0,-1tio-&
a)(idth()
b)precision()
c)fi##()
d)setf()
e)unsetf()
:( =i&t &om* o0 t+* -o--2a$am*t*$iA*. ma-i2,lato$&
a)dec
1&1
b)he)
c)oct
d)es
e)end#
f)ends
g)f#ush
?( 4+at i& 1,&tomI,&*$-.*0i-*. ma-i2,lato$&
The users can design their o(n manipu#ators to contro# the appearance of the output
9(4$it* t+* &6-taB o0 1$*ati-/ a 1,&tom ma-i2,lato$
1&&
Ostream & manipu#ator(ostream & output- argumentsSifSan$)
L
RR
RR(manipu#ator code)
RR
.eturn outputD
N
'0( =i&t o,t &om* o0 t+* ma-i2,lato$&
a)set((int (idth)
b)setprecision(int prec)
1&*
c)setfi##(int fchar)
d)setbase(int base)
e)setiosf#ags(#ong f#ags)
f)resetiof#ags(#ong f#ags)
''( 4+at a$* t+* t;o t62*& o0 .ata 1omm,-i1atio-5
a) @ata transfer bet(een the conso#e unit and the program
b) @ata transfer bet(een the program and dis> fi#e
'#( D$a; t+* &t$*am 1la&&*& 0o$ +i*$a$1+6
1&2
istream
ios
streambuf ostream
iostream fi#ebuf
ofstream fstream ifstream
1&8
'3( 4+at a$* t+* 0la/& t+at .o -ot +a)* 7it 0i*l.&5
a)iosCCsho(base-,se base indicator on output
b)iosCCsho(pos-Print before positi%e integers
c)iosCCsho(point-Sho( trai#ing decima# point and 'eros
d)iosCCuppercase-,se uppercase #etters for he) output
e)iosCCunitbuf-+#ush a## streams after insertion
g)iosCCstdio-+#ush stdout and stderr after insertion
'4(4+at a$* t+* 0,-1tio-& t+at t+* 0il* &t$*am 1la&& 2$o)i.*&5
a)see>g()-;o%es get pointer to a specified #ocation
fstreambase
1&<
b)see>p()-;o%es put pointer to a specified #ocation
c)te##g()-Ui%es the current position of the get pointer
d)te##p()-Ui%es the current position of the put pointer
'5( 4+at i& a 0il*5
0 fi#e is a co##ection of re#ated information defined b$ its creator! +i#es represent programs (both
source and object forms) and data!@ata ma$ be numeric-a#phabetic- or a#phanumeric!+i#es ma$ be free-
form-such as te)t fi#es-or ma$ be rigid#$ formatted!
'8( =i&t &om* o0 t+* 0il* mo.*&
1)iosCCin
1&7
&)iosCCout
*)iosCCate
2)iosCCapp
8)iosCCtrun>
<)iosCCnocreate
7)iosCCnorep#ace
=)iosCCbinar$
':( 4+at i& m*a-t 76 -am*&2a1*5
1&=
01SI C Standard has added a ne( >e$(ord namespace to define a scope that cou#d ho#d g#oba#
identifiers!The best e)amp#e of namespace scope is the C Standard 9ibrar$!0## c#asses-functions and
temp#ates are dec#ared (ithin the namespace named &t.3
U&i-/ -am*&2a1* &t.J
The using namespace statement specifies that the members defined in &t. namespace (i## be used freOuent#$
throughout the program
'?( 4$it* t+* &6-taB 0o$ .*0i-i-/ a -am*&2a1*
-am*&2a1* namespaceSname
1&A
L
//@ec#aration of
//%ariab#es-functions-c#asses-etc!
N
'9( 4+at i& ,--am*. -am*&2a1*&5
0n unnamed namespace is one that does not ha%e a name! ,nnamed namespace members occup$
g#oba# scope and are accessib#e in a## scopes fo##o(ing the dec#aration in the fi#e
#0( 4+at i& !ta-.a$. T*m2lat* =i7$a$65
The co##ection of these generic c#asses and functions is ca##ed the Standard Temp#ate 9ibrar$
1*6
#'( =i&t t+* 1om2o-*-t& o0 !T=
The ST9 contains se%era# components! The$ are
1)Containers
&)0#gorithms
*)Iterators
##( 4+at i& 1o-tai-*$5
1*1
0 container is an object that actua##$ stores data!It is a (a$ data is organi'ed in memor$! The ST9
containers are imp#emented b$ temp#ate c#asses and therefore can be easi#$ customi'ed to ho#d different t$pes
of data
#3( 4+at i& a- al/o$it+m5
0n a#gorithm is a procedure that is used to process the data contained in the containers! The ST9
inc#udes man$ different >inds of a#gorithms to pro%ide support to tas>s such as initia#i'ing- searching- cop$ing-
and sorting and merging
#4( 4+at i& a- it*$ato$5
1*&
0n iterator is an object that points to an e#ement in a container! Be can use iterators to mo%e through
the contents of containers!Iterators are hand#ed just #i>e pointers
#5(4+at a$* t+* t+$** t62*& o0 1o-tai-*$&5
The ST9 contains three t$pes of containers
1)SeOuence containers
&)0ssociati%e containers
*)@eri%ed containers
#8( 4+at i& m*a-t 76 &*<,*-1* 1o-tai-*$&5
1**
SeOuence containers store e#ements in a #inear seOuence- #i>e a #ine! Each e#ement is re#ated to other
e#ements b$ its position a#ong the #ine! The$ a## e)pand themse#%es to a##o( insertion of e#ements and a## of
them support a number of operations on them
El*m*-t0 El*m*-t' El*m*-t# K33 =a&t El*m*-t
#:(4+at a$* t+* t62*& o0 &*<,*-1* 1o-tai-*$&
The ST9 pro%ides three t$pes of seOuence containers
1)Eector
&)#ist
1*2
*)deOue
#?( 4+at i& m*a-t 76 A&&o1iati)* Co-tai-*$&5
0ssociati%e containers are designed to support direct access to e#ements using >e$s!
#9( 4+at a$* t+* t62*& o0 A&&o1iati)* 1o-tai-*$&5
The ST9 pro%ides three t$pes of associati%e containers
1)Set
&);u#tiset
*);ap
2);u#timap
1*8
30( 4+at i& m*a-t 76 D*$i)*. Co-tai-*$&5
The deri%ed containers do not support iterators and therefore (e cannot use them for data
manipu#ation! The$ support t(o member functions 2o2@( a-. 2,&+@( for imp#ementing de#eting and inserting
operations!
3'( 4+at a$* t+* t62*& o0 .*$i)*. 1o-tai-*$&
The ST9 pro%ides three t$pes of deri%ed containers
1)Stac>
&)]ueue
*)Priorit$SOueue
1*<
3#(4+at i& m*a-t 76 )*1to$5
The %ector stores e#ements in contiguous memor$ #ocations and enab#es direct access to an$ e#ement
using the subscript operatorPQ!0 %ector can change its si'e d$namica##$ and therefore a##ocates memor$ as
needed at run time
33(4+at i& m*a-t 76 li&t&5
The #ist is another container that is popu#ar#$ used! It supports a bidirectiona#- #inear #ist and pro%ides
an efficient imp#ementation for de#etion and insertion operations!
34( 4+at i& m*a-t 76 ma2&5
1*7
0 map is a seOuence of pairs (here a sing#e %a#ue is associated (ith each uniOue >e$! .etrie%a# of
%a#ues is based on the >e$ and is %er$ fast! Be shou#d specif$ the >e$ to obtain the associated %a#ue!
Ea#ue1
Fe$1
Fe$&
1*=
Ea#ue&
3 3
3 3
3 3
3 3
3 3
3
3 Ea#ue1
Fe$ 1
1*A
35( 4$it* t+* m*m7*$ 0,-1tio-& o0 t+* ma2 1la&&
1) begin ()
&) c#ear()
*) empt$()
2) end()
8) erase()
<) find()
7) insert()
126
=) si'e()
A) s(ap()
PART-B
'( Brite a program to (rite and read data in a fi#e using object I/O functions (rite( ) and read( )!
@ec#are c#ass (ith data members name P&Q- m int bi## no- int Samount debited and int recei%ed S
amount and in ba#ance! 0dd 16 records and disp#a$ the #ist of persons (ith ba#ances! The user shou#d
ha%e a faci#it$ to modif$ the e)isting records @escribe the %arious fi#e modes and its s$nta)
121
#( Brite a C program to read from & fi#es simu#taneous#$!
3( Bhat is fi#eV Brite the t$pes of +i#e accessV Brite an e)amp#e program for seOuentia# access
4( Bhat is fi#eV Brite the t$pes of +i#e accessV Brite an e)amp#e program for random access
5( Bhat is standard temp#ate #ibrar$V Brite the t$pes of ST9VBrite an e)amp#e program for each ST9
t$pes.