You are on page 1of 33

CS 2203

OBJECT-ORIENTED PROGRAMMING (Common to CSE & IT)

3 0 0 3

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 ! -.SI String Objects ! standard temp"ate "ibrar# Total: 4 TE!T BOO"S: / 0 Tri$edi* 1Programming &ith -.SI C++2* O'ford )ni$ersit# Press* 3445 RE#ERENCES: / 3 ; Ira Poh"* 1Object Oriented Programming using C++2* Pearson Education* Second Edition +eprint 3446 S 0 7ippman* 8osee 7ajoie* 0arbara E 9oo* 1C++ Primer2* (ourth Edition* Pearson Education* 344: 0 Stroustrup* 1The C++ Programming "anguage2* Third edition* Pearson Education* 3446

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 2 Ma$%& '( Gi)* t+* *)ol,tio- .ia/$am o0 OOPS 1o-1*2t3 9achine "anguage Procedure "anguage -ssemb"# "anguage OOPS 2( 4+at i& P$o1*.,$* o$i*-t*. la-/,a/*5 Con$entiona" programming* using high-"e$e" "anguage such as CO0O7* (O+T+-. 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) = 7arger programs are di$ided into sma""er programs <no&n as functions = 9ost 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 (unction-/ (unction-3 (unction-; (unction-6 (unction-: (unction-? (unction-5 (unction-@ 9ain program 4( 4$it* a-6 0o,$ 0*at,$*& o0 OOPS3 = 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 ( 4+at a$* t+* 7a&i1 1o-1*2t& o0 OOS5 = Objects = C"asses = >ata abstraction and Encapsu"ation = Inheritance = Po"#morphism = >#namic binding = 9essage 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 = 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 Arapping 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 '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 EgB int a*bC ,, a*b are data members Doid getdata ( ) C ,, member function ''(4+at i& .6-ami1 7i-.i-/ o$ lat* 7i-.i-/5 0inding refers to the "in<ing of a procedure to the code to be e'ecuted in response to the ca"" >#namic binding 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 '2(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 OOPS3 = 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 = 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 = -utomatic initia"i%ation and c"ear up of objects = Operator o$er"oading ' (4+at a$* t+* 0*at,$*& $*<,i$*. 0o$ o79*1t o$i*-t*. la-/,a/*5 = >ata encapsu"ation = >ata hiding and access mechanisms

= -utomatic initia"i%ation and c"ear up of objects = Operator o$er"oading = Inheritance = >#namic binding '8(Gi)* a-6 0o,$ a22li1atio-& o0 OOPS = +ea"-time s#stems = Simu"ation and mode"ing = Object-oriented databases = -I 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 '?( 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* = Ee#&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 EgB go to* If* struct * e"se *union etc 20( 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 = - dec"ared <e#&ord cannot be used as a $ariab"e name 2'(4+at a$* t+* o2*$ato$& a)aila7l* i- C>>5 -"" operators in C are a"so used in C++ In addition to insertion operator FF and e'traction operator GG the other ne& operators in C++ are* B Scope reso"ution operator B B H Pointer-to-member dec"arator -GH Pointer-to-member operator H Pointer-to-member operator de"ete 9emor# re"ease operator end" 7ine feed operator ne& 9emor# a""ocation operator set& (ie"d &idth operator 22(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 EgB Iinc"udeFiostream hG

int mJ/4C ,, g"oba" $ariab"e m $oid main ( ) K int mJ34C ,, "oca" $ariab"e m coutFF2mJ2FFmFF2Ln2C coutFF2B B mJ2FFB B mFF2Ln2C M outputB 34 /4 (B B m access g"oba" m) Scope reso"ution operator is used to define the function outside the c"ass S#nta'B +eturn t#pe Fc"ass nameG B B Ffunction nameG EgB Doid ' B B getdata() 23( 4+at a$* 0$** &to$* o2*$ato$& @o$( M*mo$6 ma-a/*m*-t o2*$ato$&5 .e& and >e"ete operators are ca""ed as free store operators since the# a""ocate the memor# d#namica""# .e& operator can be used to create objects of an# data t#pe Pointer-$ariab"e J ne& data t#peC Initia"i%ation of the memor# using ne& operator can be done This can be done as* Pointer-$ariab"e J 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"eC 24( 4+at a$* ma-i2,lato$&5 Set&* end" are <no&n as manipu"ators 9anipu"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 character2Ln2 EgBCoutFFset&(:)FFsumFFend"C 2 ( 4+at .o 6o, m*a- 76 *-,m*$at*. .atat62*5 -n 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 EgB enum shapeK circ"e* sNuare* triang"eM enum co"orK red* b"ue* green* #e""o&M 28( 4+at a$* &6m7oli1 1o-&ta-t&5 There are t&o &a#s for creating s#mbo"ic constants in C++B = )sing the Nua"ifier constant = >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++ EgB Const int si%e J/4C Char name Osi%ePC 2:(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* QQ Q int n Jstr"en(string)C QQ f"oat areaJ; /6HradHradC 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 2?( 4+at a$* $*0*$*-1* )a$ia7l*5 - 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' B >ata-t#pe &reference-name J $ariab"e-name EgB f"oat tota" J /44C f"oat sum J tota"C 29(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* B BH To dec"are a pointer to a member of a c"ass H To access a member using object name and a pointer to the member -GH To access a member using a pointer to the object and a pointer to that member 30(;+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 functionRname(argument "ist)C Eg f"oat $o"ume(int '*f"oat #)C 3'(;+at i& a- i-li-* 0,-1tio- 5 -n 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 K function bod# M 32( 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 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 B f"oat amount(f"oat princip"e*int period*f"oat rateJ4 /:) (unction ca"" is Da"ueJamount(:444*5)C

Sere it ta<es princip"eJ:444& periodJ5 -nd defau"t $a"ue for rateJ4 /: Da"ueJamount(:444*5*4 ;6) Passes an e'p"icit $a"ue 4f 4 ;6 to rate Ae must add defau"t $a"ue from right to "eft 34( 4+at a$* 1o-&ta-t a$/,m*-t& 5 <e#&ord is const The Nua"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 egB int str"en( const char Hp)C 3 ( Co; t+* 1la&& i& &2*1i0i*. 5 Tenera""# c"ass specification has t&o parts = c"ass dec"aration It describes the t#pe and scope of its member = c"ass function definition It describes ho& the c"ass functions are imp"emented The genera" form is C"ass c"assRname K pri$ateB $ariab"e dec"arationsC function dec"arationC pub"icB $ariab"e dec"arationC function dec"arationC MC 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 EgBc"assname 'C ,,memor# for ' is created 3:( Co; to a11*&& a 1la&& m*m7*$ 5 object-name function-name(actua" arguments) egB' getdata(/44*5: :)C 3?( Co; t+* m*m7*$ 0,-1tio-& a$* .*0i-*. 5 9ember functions can be defined in t&o &a#s = outside the c"ass definition 9ember function can be defined b# using scope reso"ution operatorBB Tenera" format is +eturn t#pe c"assR nameBBfunction-name(argument dec"aration) K M = Inside the c"ass definition This method of defining member function is to rep"ace the function dec"aration b# the actua" function definition inside the c"ass It is treated as in"ine function EgBc"ass item K int a*b C $oid getdata(int '*int #) K

aJ'C bJ#C MC 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 (eatureB = It is initia"i%ed to %ero &hen the first object is created .o 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 EgB static int count,,count is initia"i%ed to %ero &hen an object is created int c"assnameBBcountC,,definition of static data member 40( 4+at i& &tati1 m*m7*$ 0,-1tio-5 - member function that is dec"ared as static has the fo""o&ing properties = - static function can ha$e access to on"# other static member dec"ared in the same c"ass = - static member function can be ca""ed using the c"assname as fo""o&s c"assname BBfunctionRnameC 4'( Co; t+* o79*1t& a$* ,&*. a& 0,-1tio- a$/,m*-t5 This can be done in t&o &a#s = - cop# of the entire object is passed to the argument = On"# address of the objects is transferred to the f unction 42( 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 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 Doid mu"(int *int)constC 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 1fu""# Nua"ified2 c"ass member name - c"ass member pointer can be dec"ared using the operatorBBH&ith the c"ass name EgB c"ass K int mC pub"icB $oid sho&( )C MC pointer to member m is defined as int -BBHipJ&-BBmC -BBH-Gpointer to member of - c"ass &-BBm-Gaddress of the m member of - c"ass 4 ( 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 65) Ahen the deferencing operator H is usedU It is used to access a member &hen the object itse"f is used as pointers

6@) >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 EgC $oid test(int a) K QQ M c"ass student K QQQ MC student s/(a)C M 48( 4+at a$* #$i*-. 0,-1tio-&5 4$it* t+* &6-taB - 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 friend dataRt#pe functionRname( )C (riend function is preceded b# the <e#&ord VfriendW 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 Sence 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 egB ( - ' ) 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 EgB -n 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)C ,,add function &ith 3 arguments of same t#pe ii int add( int a* int b* int c)C ,,add function &ith ; arguments of same t#pe iii doub"e add( int p* doub"e N)C ,,add function &ith 3 arguments of different t#pe ,,(unction ca""s add (; * 6)C ,,uses protot#pe ( i ) add (;* 6* :)C ,,uses protot#pe ( ii ) add (; * /4 4)C ,,uses protot#pe ( iii ) 6@) >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 EgC $oid test(int a) K QQ

M c"ass student K QQQ MC student s/(a)CM PART-B /) >escribe the ad$antages of OOP(@) 3) Ahat are the differences bet&een pointers to constants and constant pointersU(@) ;) >escribe the app"ications of OOP techno"og# 6) E'p"ain object-oriented paradigm &ith a"/its essentia" e"ements :) 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 5)Arite short notes on access specifiers @)E'p"ain defau"t arguments &ith e'amp"e X)E'p"ain about friend function &ith suitab"e e'amp"e /4)Ahat is a static member and &hat are common characteristics //)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 '( D*0i-* 1o-&t$,1to$ - 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 EgB integer C"ass K QQ pub"icB integer( )C,,constructo r QQQ M 2( D*0i-* .*0a,lt 1o-&t$,1to$ The constructor &ith no arguments is ca""ed defau"t constructor EgB C"ass integer K int m*nC Pub"icB Integer( )C QQ MC integerBBinteger( ),,defau"t constructor K mJ4CnJ4C M the statement integer aC in$o<es the defau"t constructor 3( D*0i-* 2a$am*t*$iA*. 1o-&t$,1to$ constructor &ith arguments is ca""ed parameteri%ed constructor EgC C"ass integer K int m*nC pub"icB integer(int '*int #) K mJ'CnJ#C M 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

/ 0# ca""ing the constructor e'p"icit"# egB integer int/Jinteger(/4*/4)C 3 0# ca""ing the constructor imp"icit"# egB Integer int/(/4*/4)C 4( D*0i-* .*0a,lt a$/,m*-t 1o-&t$,1to$ The constructor &ith defau"t arguments are ca""ed defau"t argument constructor EgB Comp"e'(f"oat rea"*f"oat imagJ4)C The defau"t $a"ue of the argument imag is 4 The statement comp"e' a(? 4) assign rea"J? 4 and imagJ4 the statement comp"e' a(3 ;*X 4) assign rea"J3 ; and imagJX 4 ( 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 Ahen both these forms are used in a c"ass *it cause ambiguit# for a statement such as - aC The ambiguit# is &hether to ca"" -BB-() or -BB-(int iJ4) 8( D*0i-* 1o26 1o-&t$,1to$ - 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 EgB integer i3(i/)C &ou"d define the object i3 at the same time initia"i%e it to the $a"ues of i/ -nother form of this statement is EgB integer i3Ji/C The process of initia"i%ing through a cop# constructor is <no&n as cop# initia"i%ation :( D*0i-* .6-ami1 1o-&t$,1to$ -""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 .EA operator EgB C"ass string K char HnameC int "engthC pub"icB string( ) K "engthJ4C nameJne& charO "ength +/PC M $oid main( ) K string name/(17ouis2)*name;(7agrange)C M ?( D*0i-* 1o-&t o79*1t

Ae can create constant object b# using const <e#&ord before object dec"aration EgB Const matri' '(m*n)C

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) EgC Yinteger() K M - destructor ne$er ta<es an# arguments nor it does it return an# $a"ue The compi"er upon e'it from the program &i"" in$o<e it ne& Ahene$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 EgB Iinc"udeFiostream hG Iinc"udeFconio hG c"ass integer K int m*nC pub"icB integer( ) ,,defau"t constructor K mJ4CnJ4C M integer(int a*int b) ,,parameteri%ed constructor K mJaC nJbC M integer(&i) ,,cop# constructor K mJi mC nJi nC M $oid main() K integer i/C ,,in$o<es defau"t constructor integer i3(6:*?5)C,,in$o<es parameteri%ed constructor integer i;(i3)C ,,in$o<es cop# constructor M ''( 4$it* &om* &2*1ial 1+a$a1t*$i&ti1& o0 1o-&t$,1to$ = 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

'2( 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(/4*/@) this $a"ue can be get during run time i e * f or abo$e constructor int p*NC cinGGpGGNC integer a(p*N)C '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 K function bod# M '4(EB2lai- $*t,$- 76 $*0*$*-1* ;it+ a- *Bam2l* - function can a"so return a reference Consider the fo""o&ing function int & ma'( int &' * int &#) K if('G#) return 'C e"se return #C M 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) J -/C is "ega" and assigns !/ to a if it is "arger* other&ise !/ to b ' ( 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 '8( =i&t o,t t+* o2*$ato$& t+at 1a--ot 7* o)*$loa.*.3 = C"ass member access operator ( * H) = Scope reso"ution operator (BB) = Si%e operator ( si%eof ) = Conditiona" operator (UB) ':( 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 return t#pe c"assname BB operator (op-arg"ist ) K function bod# M &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 Ahen 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 Ahen binar# operators are o$er"oaded using member functions* it ta<es one e'p"icit argument -"so the "eft hand side operand must be an object of the re"e$ant c"ass 20( Co; ;ill 6o, o)*$loa. U-a$6 a-. Bi-a$6 o2*$ato$ ,&i-/ #$i*-. 0,-1tio-&5 Ahen unar# operators are o$er"oaded using friend function* it ta<es one reference argument (object of the re"e$ant c"ass) Ahen binar# operators are o$er"oaded using friend function* it ta<es t&o e'p"icit arguments 2'( 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 objectRname or objectRname 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 22( 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 (')C In case of binar# operators* o$er"oaded operator can be in$o<ed as Operator op (' * #) 23( =i&t o,t t+* o2*$ato$& t+at 1a--ot 7* o)*$loa.*. ,&i-/ #$i*-. 0,-1tio-3 = -ssignment operator J = (unction ca"" operator ( ) = Subscripting operator O P = C"ass member access operator 24( 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 EgB Con$erting int t#pe to c"ass t#pe c"ass time K int hrs*minsC pub"icB QQQQ Time ( int t) ,,constructor K hoursJ t,?4 C ,,t in minutes mins Jt Z ?4C M MC Constructor &i"" be ca""ed automatica""# &hi"e creating objects so that this con$ersion is done automatica""#

2 ( 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 EgB $ector B B operator doub"e( ) K doub"e sumJ4C for(int IJ4CIFsi%eCI++) sumJsum+$O i P HuO i P C return sNrt ( sum ) C M This function con$erts a $ector to the corresponding sca"ar magnitude 28( 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 Sere constructor is used in destination c"ass and casting operator function is used in source c"ass EgB obj[ J 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 [ Sere c"ass \ is the source c"ass and c"ass [ is the destination c"ass PART-B '(E'p"ain cop# constructor &ith suitab"e C++ coding 2(7ist 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 Arite a program to i""ustrate an o$er"oading 4(Ahat is operator o$er"oadingU So& man# arguments are reNuired in the definition of an o$er"oaded binar# operator (Ahat is constructorU E'p"ain the t#pes of constructor &ith an suitab"e e'amp"eArite a program to o$er"oad J operator -ssign $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 5)E'p"ain constructor &ith >#namic -""ocation gi$e e'amp"e @)Arite short notes onB i)T#pe con$ersions ii)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 '( 4+at a$* EB1*2tio-&5 E'ceptions &hich occur during the program e'ecution* due to some fau"t in the input data 2( 4+at a$* t+* t;o t62*& o0 *B1*2tio-&5 E'ceptions are c"assifieds into a)S#nchronous e'ception b) -s#nchronous e'ception 3( 4+at i& a &6-1+$o-o,& *B1*2tio-5 The techniNue 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 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 ( 4+at a$* t+* 7lo1%& ,&*. i- t+* EB1*2tio- Ca-.li-/5 The e'ception-hand"ing mechanism uses three b"oc<s /)tr# b"oc< 3)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% 8( D$a; t+* EB1*2tio- +a-.li-/ mo.*l5 Tr# b"oc< Perform operation &hich ma# thro& or in$o<e e'terna" function if needed In$o<e function ha$ing thro& b"oc< e'ception Thro& b"oc< If(fai"ure) Thro& objectC 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<

:( 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 - 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 Ee#&ord tr# K ,,code raising e'ception or referring to a function raising e'ception M catch(t#peRid/) K ,,actions for hand"ing an e'ception M Q Q catch(t#peRidn) K ,,actions for hand"ing an e'ception M ?( 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 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 Ee#&ord object name or name"ess object

Catch(T) K ,, actions for hand"ing an e'ception M 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) .amed object* name"ess object Ee#&ord

thro& TC

'0( =i&t o,t t+* ta&%& to 7* 2*$0o$m*. 0o$ *$$o$ +a-.li-/ 1o.* /)>etect the prob"em causing e'ception(Sit the e'ception) 3)Inform that an error has occurred(Thro& the e'ception) ;)+ecei$e the error information(Catch the e'ception) 6)Ta<e correcti$e actions(Sand"e the e'ceptions) ''( 4$it* t+* &t*2& to 7* 2*$0o$m*. ;+*- a- *B1*2tio- i& $ai&*. /)The program searches for a matching hand"er 3)If a hand"er is found* the stac< is un&ound to that point ;)Program contro" is transferred to the hand"er 6)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 '2( 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 id/*t#pe id3*QQ) K ,,(unction bod# raising e'ceptions if error occurs M '3( 4$it* t+* &6-taB o0 1at1+ all 1o-&t$,1t
Three dotsBindicate catch a"" e'ceptions

Catch(Q) K ,,actions for hand"ing an e'ception M '4( 4+at i& a &*1,$* o2*$atio-5 -"" destructors in a C++ program shou"d ha$e an empt# specification thro&() This is ca""ed secure operations ' ( 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 '8( 4$it* t+* 0,-1tio-& to +a-.l* ,-1a,/+t *B1*2tio-& /)terminate() 3)setRterminate()

;)une'pected() 6)setRune'pected() ':( 4+at a$* t+* t62*& o0 0a,lt tol*$a-t .*&i/- t*1+-i<,*& (au"t to"erant soft&are design techniNues can be c"assified into the fo""o&ing /).-$ersion programming 3)+eco$er# b"oc< '?( =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 occurred d)good()-+eturns true if no error has occurred

has

'9( 4+at a$* t+* t;o t62*& o0 *$$o$& o$ 7,/&5 a)7ogica" 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 20( 4+at i& 0,-1tio- t*m2lat*5 - 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 2'(4$it* t+* &6-taB o0 0,-1tio- t*m2lat* temp"ateGc"ass T*QG +eturnT#pe (unc.ame(arguments) K Q ,,bod# of the function temp"ate Q M 22(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 - c"ass temp"ate specifies ho& indi$idua" c"asses can be constructed simi"ar to norma" c"ass specification 23(4$it* t+* &6-taB o0 1la&& t*m2lat* temp"ate Fc"ass T/*c"ass T3*QG c"ass c"assname K T/ data/C Q ,,functions of temp"ate arguments T/*T3*Q $oid func/(T/ a*T3 &b)C Q T func3(T3 H'*T3 H#)C

MC

PART-B /) >iscuss the need for e'ception &ith tr#* catch and thro& <e#&ords 3) Ahat is (unction Temp"ateU Arite the s#nta' for function temp"ate Arite an e'amp"e program for function temp"ate ;) Ahat is c"ass temp"ateU Arite the s#nta' for c"ass temp"ate Arite an e'amp"e program for c"ass temp"ate 6) Ahat are E'ceptionsU Ahat are the t&o t#pes of e'ceptionsU >ra& the E'ception hand"ing mode"U :) Ahat are uncaught e'ceptionsU Arite the functions to hand"e uncaught e'ceptions &ith a suitab"e e'amp"e ?) Ahat are E'ceptionsU Ahat are the t&o t#pes of e'ceptionsU Arite the s#nta' of tr# construct Arite the s#nta' of catch construct Arite the s#nta' of thro& construct &ith an e'amp"e

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 '( 4+at i& m*a-t 76 i-+*$ita-1*5 Inheritance is the process b# &hich objects of one c"ass acNuire the properties of another c"ass It supports the concept of hierarchica" c"assification It pro$ides the idea of reusabi"it# Ae can add additiona" features to an e'isting c"ass &ithout modif#ing it b# deri$ing a ne& c"ass from it 2(=i&t t+* $,l*& 0o$ i-+*$ita-1* /)The defau"t mode is pri$ate 3)>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*Ahen those c"ass is pub"ic"# inherited from base c"ass 6)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 The c"ass members are $isib"e to the base c"ass*deri$ed c"asses and outside the c"ass through the objects (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 EgB 0ase c"ass >eri$ed c"ass Sere c"ass - is the base c"ass from &hich the c"ass > is deri$ed C"ass > is the pub"ic deri$ation of c"ass 0 hence it inherits a"" the pub"ic members of 0 0ut > cannot access pri$ate members of 0 ?( 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 EgB 0ase c"asses >eri$ed c"ass Sere c"ass C is deri$ed from t&o base c"asses - & 0

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 B Sierarchica" c"assification of students in )ni$ersit# 0 C 0 '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 EgB 0ase c"ass Trand father Intermediate 0ase 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 9u"ti"e$e" inheritance 9u"tip"e inheritance The c"ass resu"t &i"" ha$e both the mu"ti"e$e" and mu"tip"e inheritances Student -rts Engineering 9 e d i c a " CSE ECE Ci$i" 0 C Student Test +esu"t Sports '2( 4+at i& m*a-t 76 A7&t$a1t 7a&* 1la&&5 - c"ass that ser$es on"# as a base c"ass from &hich deri$ed c"asses are deri$ed .o objects of an abstract base c"ass are created - 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 - base c"ass that is Nua"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 EgB Processing of studentsW resu"ts -ssume that c"ass sports deri$e the ro"" 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 -s a $irtua" base c"ass

'4(4+at a$* )i$t,al 0,-1tio-&5 - function Nua"ified b# the V$irtua"W <e#&ord is ca""ed $irtua" function Ahen a $irtua" function is ca""ed through a pointer* c"ass of the object pointed to determine &hich function definition &i"" be used ' (4$it* &om* o0 t+* 7a&i1 $,l*& 0o$ )i$t,al 0,-1tio-& = Dirtua" f unctions must be member of some c"ass = The# cannot be static members and the# are accessed b# using object pointers = Dirtua" f unction in a base c"ass must be defined = 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 - 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 reNuires each deri$ed c"ass to either define the function or redec"are it as a pure $irtua" function - 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 1donothing2 function The 1do-nothing2 function is defined as fo""o&sB $irtua" $oid disp"a# ( ) J4C ':( 4+at i& 2ol6mo$2+i&m5 Po"#morphism is the abi"it# to ta<e more than one form -n 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 c ad-hoc po"#morphism d Parametric po"#morphism e Dirtua" 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 -"so <no&n as compi"e time po"#morphism 20(4+at t+* t62*& i- 1om2il* tim* 2ol6mo$2+i&m5 a)(unction o$er"oading b)Operator o$er"oading 2'( 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

22(=i&t O,t RTTI5 a)0ase T#pe b)Compound T#pe c)Container T#pe 23(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 24(EB2lai- t+* t+$** 2a$t& o0 RTTI &6&t*m /)+TTI description of 0ase T#pes 3)Propert# >escription of compound and container t#pes ;)Propert# iterators 2 (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#namicRcast operator 28(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 i- the same hierarch# 2:(D*0i-* Do;-1a&ti-/ >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 doesnWt a"&a#s &or< 2?(D*0i-* U21a&ti-/ )pcasting means c"asses can easi"# con$erge to a genera" c"ass

PART-B /) >iscuss about po"#morphism and its ad$antages 3) Arite a C++ program that &i"" gi$e the conditions of en$ironment reNuired* food habits and uniNue characteristics of pet anima"s fish and dog >efine a base ca""ed pet that describe an# common househo"d petC t&o deri$ed c"asses ca""ed fish and dog &ith items specific to that t#pe of anima"s Arite pure $irtua" functions in the base c"ass for operations that are common to both t#pes of anima"s Arite a program to test the usage of c"asses ;) E'p"ain h#brid inheritance &ith suitab"e C++ coding 6) >efine po"#morphism E'p"ain the different t#pes of po"#morphism :) Arite a C++ program using inheritance to imp"ement the fo""o&ing - ne&spaper agent pa#s t&o $ariant rates for the de"i$er# of ne&spapers de"i$er# bo# can earn +S 3 on a morning de"i$er# but on"# +s / :4 on an e$ening de"i$er# 0o#s are sa"aried either for a morning de"i$er# or for an e$ening paper round but not for both +ead(Input) the code for round (4 for morning and / 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 ?) 5) @) X) Ahat are the different <inds of inheritanceU E'p"ain them &ith suitab"e e'amp"es 0rief"# e'p"ain +TTI >escribe $irtua" function &ith suitab"e e'amp"e >efine pure $irtua" function

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 ! -.SI String Objects ! standard temp"ate "ibrar# '3 4+at a$* &t$*am&5 - Stream is a seNuence 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 23 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

ios

Streambuf HbpJ&strstreambaseBB bug ostream

istream

streambuf

iostream

istreamR&ithassign

iostreamR&ithassign

ostreamR&ithassign

4( =i&t o,t &om* o0 t+* ,-0o$matt*. IIO o2*$ato$&3 a)put() b)get() c)get"ine() d)&rite() ( =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 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 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$ Ostream & manipu"ator(ostream & output* argumentsRifRan#) K QQ QQ(manipu"ator code) QQ +eturn outputC M '0( =i&t o,t &om* o0 t+* ma-i2,lato$& a)set&(int &idth) b)setprecision(int prec) c)setfi""(int fchar) d)setbase(int base) e)setiosf"ags("ong f"ags) f)resetiof"ags("ong f"ags) ''( 4+at a$* t+* 0la/& t+at .o -ot +a)* 7it 0i*l.&5 a)iosBBsho&base-)se base indicator on output b)iosBBsho&pos-Print + before positi$e integers c)iosBBsho&point-Sho& trai"ing decima" point and %eros d)iosBBuppercase-)se uppercase "etters for he' output e)iosBBunitbuf-("ush a"" streams after insertion g)iosBBstdio-("ush stdout and stderr after insertion '2( 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

'3(D$a; t+* &t$*am 1la&&*& 0o$ +i*$a$1+6 ios

istream

streambuf

ostream

iostream

fi"ebuf

ifstream

fstream

ofstream

fstreambase

'4(4+at a$* t+* 0,-1tio-& t+at t+* 0il* &t$*am 1la&& 2$o)i.*&5 a)see<g()-9o$es get pointer to a specified "ocation b)see<p()-9o$es put pointer to a specified "ocation c)te""g()-Ti$es the current position of the get pointer d)te""p()-Ti$es the current position of the put pointer ' ( 4+at i& a 0il*5 - 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.*& /)iosBBin 3)iosBBout ;)iosBBate 6)iosBBapp :)iosBBtrun< ?)iosBBnocreate 5)iosBBnorep"ace @)iosBBbinar# ':( 4+at i& m*a-t 76 -am*&2a1*5 -.SI 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 7ibrar# -"" 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 freNuent"# throughout the program '?( 4$it* t+* &6-taB 0o$ .*0i-i-/ a -am*&2a1* -am*&2a1* namespaceRname K ,,>ec"aration of ,,$ariab"es*functions*c"asses*etc M '9( 4+at i& ,--am*. -am*&2a1*&5 -n 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 20( 4+at i& Sta-.a$. T*m2lat* =i7$a$65 The co""ection of these generic c"asses and functions is ca""ed the Standard Temp"ate 7ibrar# 2'( =i&t t+* 1om2o-*-t& o0 ST= The ST7 contains se$era" components The# are /)Containers 3)-"gorithms ;)Iterators 22( 4+at i& 1o-tai-*$5 - container is an object that actua""# stores data It is a &a# data is organi%ed in memor# The ST7 containers are imp"emented b# temp"ate c"asses and therefore can be easi"# customi%ed to ho"d different t#pes of data 23( 4+at i& a- al/o$it+m5 -n a"gorithm is a procedure that is used to process the data contained in the containers The ST7 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 24( 4+at i& a- it*$ato$5 -n iterator is an object that points to an e"ement in a container Ae can use iterators to mo$e through the contents of containers Iterators are hand"ed just "i<e pointers 2 (4+at a$* t+* t+$** t62*& o0 1o-tai-*$&5 The ST7 contains three t#pes of containers /)SeNuence containers 3)-ssociati$e containers ;)>eri$ed containers 28( 4+at i& m*a-t 76 &*<,*-1* 1o-tai-*$&5 SeNuence containers store e"ements in a "inear seNuence* "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*-t2 K33 =a&t El*m*-t

2:(4+at a$* t+* t62*& o0 &*<,*-1* 1o-tai-*$& The ST7 pro$ides three t#pes of seNuence containers /)Dector 3)"ist ;)deNue 2?( 4+at i& m*a-t 76 A&&o1iati)* Co-tai-*$&5 -ssociati$e containers are designed to support direct access to e"ements using <e#s 29( 4+at a$* t+* t62*& o0 A&&o1iati)* 1o-tai-*$&5 The ST7 pro$ides three t#pes of associati$e containers /)Set 3)9u"tiset ;)9ap 6)9u"timap 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 ST7 pro$ides three t#pes of deri$ed containers /)Stac< 3)]ueue ;)Priorit#RNueue 32(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 operatorOP - $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 - map is a seNuence of pairs &here a sing"e $a"ue is associated &ith each uniNue <e# +etrie$a" of $a"ues is based on the <e# and is $er# fast Ae shou"d specif# the <e# to obtain the associated $a"ue

Ee#/

Da"ue/

Ee#3

Da"ue3 3 3 3 3 3 Da"ue.

3 3 3 3 3 3 Ee# . 3

3 ( 4$it* t+* m*m7*$ 0,-1tio-& o0 t+* ma2 1la&& /) begin () 3) c"ear() ;) empt#() 6) end() :) erase() ?) find() 5) insert() @) si%e() X) s&ap() PART-B Arite 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 O3P m int bi"" no* int Ramount debited and int recei$ed R amount and in ba"ance -dd /4 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' Arite a C++ program to read from 3 fi"es simu"taneous"# Ahat is fi"eU Arite the t#pes of (i"e accessU Arite an e'amp"e program for seNuentia" access

'(

2( 3( 4(

( Ahat is fi"eU Arite the t#pes of (i"e accessU Arite an e'amp"e program for random access 8( Ahat is standard temp"ate "ibrar#U Arite the t#pes of ST7UArite an e'amp"e program for each ST7 t#pes.

You might also like