C++ Sample Paper (NewSyllabus)

August 23, 2022 | Author: Anonymous | Category: N/A
Share Embed Donate


Short Description

Download C++ Sample Paper (NewSyllabus)...

Description

 

Roll No.: -------------------------------

MCA – SECOND SEMESTER  MODEL TEST PAPER-II Object Oriented Programming in C++ MCA-1!

Time: " #r$

Total Marks: %

In$tr&ction$' 1. Write Write your class class roll roll no at the top, top, immediatel immediately y on receipt receipt of this this Q/aper. Q/aper. !. "ttempt (i)e  #uestions in all includin$ Q1 %hich is compulsory. "ttempt one #uestion from each unit.

*1:

&hort ans%er type:

1,./

a/ 0at 0at do 2o& 3no4 abo&t abo&t r&ntim r&ntimee 5o62mor5 5o62mor5i$ i$m7 m7

"ns: - "t runtim "ns: runtime, e, %hen %hen it is kno%n kno%n %hat %hat class class o'(ect o'(ectss are under consid considera erati tion, on, the appropriate )ersion of the function is in)oked. &ince the functions are linked %ith a  particular class much later after the compilation, this process is termed as late binding. *t binding. *t is also kno%n as dynami dynamicc binding binding  'ecause the selection of the appropriate function is done automatically at runtime. *f the appropriate mem'er function could 'e selected %hile the pro$ram is runnin$. This is kno%n as run time polymorphism. + polymorphism. + supports a mechanism kno%n as virtual function to function to achie)e runtime polymorphism. b/ Can ineri ineritanc tancee be 5ri)ate7 5ri)ate7 Commen Comment8 t8

 

   

"ns: - The normal %ay of epressin$ inheritance in + is +lass eri)ed: pu'lic ase 0 // stuff   2 When this is done, eri)ed is a su'type of ase, and any%here one encounters a ase 3 or ase 4 one can su'stitute a eri)ed 3/eri)ed 4. To $et pri)ate inheritance, one does: +lass eri)ed: pri)ate ase 0// pri)ate key%ord is actually optional //stuff  2

The pri)ate inheritance is the default type of o f inheritance for classes 'ut not for structs5i.e. if you omit the 6pu'lic6 key%ord in the inheritance specification, you $et pri)ate inheritance. 1

 

+onse#uences of pri)ate inheritance: "ll pu'lic and protected mem'ers of ase 'ecome private 'ecome  private mem'ers  mem'ers of eri)ed--i.e. the class can use them, 'ut eternal clients cannot. 78o%e)er, the definition for eri)ed can re-declare these to 'e pu'lic or protected2 mem'ers %hich %ere ori$inally protected can only 'e re-declared as protected, not pu'lic9. eri)ed is not  a   a su'type of ase, tryin$ to $tatic9ca$t a pointer from deri)ed to 'ase 7or  cast one implicitly9 %ill result in a compiler error. •





*f pri)ate inheritance 'roke the su'type link 'ut didnt make all the mem'ers pri)ate, it mi$ht 'e more useful 'ut since it does make mem'ers pri)ate, it isnt )ery $ood. c/ #o4 i$ e:ce5tion e:ce5tion and6ing and6ing i$ bene(icia6 bene(icia6 (or object oriented oriented 5rogrammin 5rogramming7 g7 An$' - Ad)antage$ o( ;$ing E:ce5tion #and6ing

;ception handlin$ pro)ides the follo%in$ ad)anta$es o)er $-33

*"'- E:56ain te conce5t o( Name$5ace$ in C++ 4it e:am56e7

1/

An$' - Name$5ace$

 Namespaces allo% to $roup entities like classes, o'(ects and functions under a name. This %ay the $lo'al scope can 'e di)ided in 6su'-scopes6, each one %ith its o%n name. The format of namespaces is: namespace identifier  0 entities  Where identifier is any )alid identifier and entities is the set of classes, o'(ects and functions that are included %ithin the namespace. Bor eample: namespace myNamespace 0   int a, '2  *n this case, the )aria'les a and ' are normal )aria'les declared %ithin a namespace called myNamespace. *n order to access these )aria'les from outside ou tside the myNamespace namespace %e ha)e to use the scope operator '' Bor eample, to access the pre)ious )aria'les from outside myNamespace %e can %rite: myNamespace::a myNamespace::' 9

 

The functionality of namespaces is especially useful in the case that there is a possi'ility that a $lo'al o'(ect or function uses the same identifier as another one, causin$ redefinition errors. Bor eample: // namespaces Zinclude iostream usin$ namespace std2 namespace first 0   int )ar G S2  namespace second 0   dou'le )ar G .112  int main 79 0 cout  first::)ar   endl2    cout  second::)ar endl2   return Y2  *n this case, there are t%o $lo'al )aria'les )a ria'les %ith the same name: )ar. Dne is defined %ithin the namespace first and the other one in second. No redefinition errors happen thanks to namespaces. &$ing

The key%ord usin$ is used to introduce a name from a namespace into the current declarati)e re$ion. Bor eample: // usin$ Zinclude iostream usin$ namespace std2 namespace first 0   int  G S2   int y G 1Y2  namespace second 0   dou'le  G .112  dou'le y G !.V12 10

 

int main 79 0   usin$ first::2   usin$ second::y2   cout    endl2   cout  y  endl2   cout  first::y  endl2   cout  second::  endl2   return Y2  The key%ord usin$ can also 'e used as a directi)e to introduce an entire namespace: // usin$ Zinclude iostream usin$ namespace std2 namespace first 0    int int  yG G S2 1Y2  namespace second 0   dou'le  G .112   dou'le y G !.V12  int main 79 0   usin$ namespace first2 first2   cout    endl2   cout  y  endl2   cout  second::  endl2   cout  second::y  endl2   return Y2  *n this case, since %e ha)e declared that %e %ere usin$ namespace first, all direct uses of  and y %ithout name #ualifiers %as referrin$ to their declarations in namespace first. ;$ing namespace ha)e )alidity only in the same 'lock in %hich they are stated or in the entire code if they are used directly in the $lo'al scope. Bor ea eample, mple, if %e had the intention to first use the o'(ects of one namespace and then those of another one, %e could do somethin$ like:

// usin$ usin$ namespace namespace eample eample S 11

 

Zinclude iostream usin$ namespace std2 namespace first 0   int  G S2  namespace second 0   dou'le  G .112 

.11

int main 79 0  0   usin$ namespace first2   cout    endl2    0 usin$ namespace    cout   endl2second2     return Y2  Name$5ace a6ia$

We can declare alternate names for eistin$ namespaces ac accordin$ cordin$ to the follo%in$ format: namespace ne%>name G current>name2 Name$5ace $td

"ll the files in the + standard li'rary declare all of its entities %ithin the std namespace. That is %hy %e ha)e $enerally included the usin$ namespace std2 statement in all pro$rams that used any entity defined in iostream. ;nit -II *!'- E:56ain te conce5t o( Ineritance and @riend$i5 in C++ 4it e:am56e$7 1/

12

 

An$' - @rien d$i5 and iner inerit itance ance'' - *n principle, pri)ate and protected mem'ers of a class cannot 'e accessed from outside the same class in %hich they are declared. 8o%e)er, this rule does not apply to "friends" . Friends are functions or classes declared %ith the  (riend key%ord.

" non-mem'er function can access the pri)ate and protected mem'ers of a class if it is declared a friend   friend  of that class. That is done 'y includin$ a declaration of this eternal function %ithin the class, and precedin$ it %ith the key%ord friend   friend : @riend (&nction$

*n principle, pri)ate and protected mem'ers of a class cannot 'e accessed from outside the same class in %hich they are declared. 8o%e)er, 8 o%e)er, this rule does not affect affect friends  friends.. Briends are functions or classes declared as such. *f %e %ant to declare an eternal function as friend of a class, thus allo%in$ this function to ha)e access to the pri)ate and protected mem'ers of this class, %e do it 'y declarin$ a prototype of  this eternal function %ithin the class, and precedin$ it %ith the key%ord friend: // friend function 6include 7iotrea8 uin2 naepace td; cla "?ectan2le #   int width4 hei2ht;   public$   void etDvalue (int4 int);   int area () #return (width * hei2ht);+   friend "?ectan2le duplicate ("?ectan2le); +; void "?ectan2le$$etDvalue (int a4 int b) #   width = a;   hei2ht = b;

+ "?ectan2le duplicate ("?ectan2le rectpara) #   "?ectan2le rectre;   rectre.width = rectpara.width*;   rectre.hei2ht = rectpara.hei2ht*;   return (rectre); + int ain () #   rect4(4B); rectb;   "?ectan2le rect.etDvalue

13

 

  rectb = duplicate (rect);   cout 77 rectb.area();   return ; +

The duplicate function is a friend of +Rectan$le. Brom %ithin that function %e ha)e 'een a'le to access the mem'ers %idth and hei$ht +Rectan$le, %hich are pri)ate mem'ers. Notice that nei neithe therr ina the declarat declaof ration ion of duplica duplicate7 te799&imply nor inhas itsaccess later to useitsinpri)ate mai main79 n79 %e consid con sidere ered d duplicate mem'er class +Rectan$le. andha)e protected mem'ers %ithout 'ein$ a mem'er of different o'(ects of type The friend functions can ser)e, for eample, to conduct operations 'et%een t%o different classes. [enerally, the use of friend functions is out of an o'(ect-oriented pro$rammin$ methodolo$y, so %hene)er possi'le it is 'etter to use mem'ers of the same class to perform operations %ith them. Friend classes

Eust as %e ha)e the possi'ility to define a friend function, %e can also define a class as friend of  another one, $rantin$ that first class access to the protected and pri)ate mem'ers of the second one. // friend cla 6include 7iotrea8 uin2 naepace td; cla "ECuare; cla "?ectan2le #   int width4 hei2ht;   public$   int area ()   #return (width * hei2ht);+   void convert ("ECuare a); +; cla "ECuare #   private$   int ide;   public$   void etDide (int a)   #ide=a;+   friend cla "?ectan2le; +; void "?ectan2le$$convert ("ECuare a) #   width = a.ide;   hei2ht = a.ide; +   int ain () #   "ECuare Cr;   rect;   "?ectan2le Cr.etDide(A);

14

 

  rect.convert(Cr);   cout 77 rect.area();   return ; +

*n this eample, %e ha)e declared +Rectan$le as a friend of +&#uare so that +Rectan$le mem'er functions could ha)e access to the protected and pri)ate mem'ers of +&#uare, more concretely to +&#uare::side, %hich descri'es the side %idth of the s#uare. +onsider that friendships are not corresponded if %e do not eplicitly specify so. *n our eample, +Recta +Re ctan$l n$lee is conside considered red as a friend friend class class 'y +&#uar +&#uare, e, 'ut +Recta +Rectan$l n$lee does does not consider  consider  +&#uare to 'e a friend, so +Rectan$le can access the protected and pri)ate mem'ers of +&#uare  'ut not the re)erse %ay. Df course, %e could ha)e declared also +&#uare as friend of +Rectan$le if %e %anted to. "notherr proper "nothe property ty of friend friendshi ships ps is that that they they are not transitive: transitive: The friend of a friend is not considered to 'e a friend unless eplicitly specified *'- 0at i$ te mecani$m o( Late binding7 E:56ain te 5roce$$ and a6$o e:56ain te r&6e$ (or te $ame8

1/

An$' - Mecani$m o( Late Binding'-

15

 

"t runtime, %hen it is kno%n %hat class o'(ects are under consideration, the appropriate )ersion of the function is in)oked. &ince the functions are linked %ith a particular class much later after  the compilation, compilation, this process process is termed as late binding.  binding.  *t is al also so kno%n kno%n as dynamic binding   'ecause the selection of the appropriate function is done automatically at runtime. *f the appropriate mem'er function could 'e selected %hile the pro$ram is runnin$. This is kno% kno%n n as run time polymorphism.  polymorphism.  + support supportss a mechani mechanism sm kno%n as virtual function  function  to achie)e runtime polymorphism.

;nit -III

*%'- 0at are 5&re )irt&a6 (&nction$ in C++7 E:56ain 4it te e65 o( a 5rogram8 A6$o e:56ain te r&6e$ o( )irt&a6 (&nction$7 1/ An$'- P&re irt&a6 @&nction - C++

" )irtual )irtual function 'ody is kno%n as ure \irtual \irtual Bunction. *n a'o)e eample %e can see that the function is 'ase class ne)er $ets in)oked. *n such type of situations %e can use pure )irtual functions ;ample : same eample can re-%ritten class 'ase 0 16

 

 pu'lic: )irtual )oid sho%79GY2 //pure )irtual function 2 class deri)ed1 : pu'lic 'ase 0  pu'lic: )oid sho%79 0 cout6]n eri)ed 162  2 class deri)ed! : pu'lic 'ase 0  pu'lic: )oid sho%79 0 cout6]n eri)ed !62  2 )oid main79 0  'ase 3'2 deri)ed1 d12 deri)ed! d!2  ' G 4d12  '-sho%792  ' G 4d!2  '-sho%792 

R&6e$ (or irt&a6 @&nction$

1. The )irtual function must 'e mem'er of class !. They cannot 'e static mem'ers . They are accessed 'y usin$ o'(ect pointers . rototype of 'ase class function 4 deri)ed class must 'e same S. \irtual \irtual function in 'ase class must 'e defined d efined e)en thou$h it is not used . " )irtual )irtual function can 'e friend function o off another class V. We could not ha)e )irtual constructor  . *f a )irtual function is deri)ed in 'ase ' ase class, it need not 'e n necessarily ecessarily redefined in the deri)ed class X. ointer o'(ect of 'ase class can point to any o'(ect of deri)ed class 'ut re)erse is not true 1Y. When a 'ase pointer points to deri)ed class, incrementin$ 4 decrementin$ it %ill not make it  point to the net o'(ect of deri)ed class 17

 

*'- De$cribe di((erent metod$ o( o5ening a (i6e8 0rite an interacti)e 5rogram tat a c c e 5 t $ r ec o r d $ a n d $ t o r e$ t  e m i n a ( i 6 e 8

 1 /

An$' - ifferent file openin$ modes in + are as follo%s: Mode$

De$cri5tion

ios::in

Dpens a file fo for rreeadin$. N Nee% co contents ca cannot ' 'ee %r %ritten in in th the fi file.

ios::ou outt

Dpe pen ns a file for %ritin$ n$.. **ff file e eist already then its contents ar are de deleted  'ut if the file does not eist it %ill create create a ne% file.

ios::'inary

*t opens a file in 'inary form.

ios: io s::t :tru runca ncate te

Dpen Dpenss a file file for for %ri %riti tin$. n$. *f file file al alre ready ady e eis ists ts th then en it itss pre pre)i )ious ous co cont nten ents ts are replaced %ith the ne% contents.

ios::app

Dpens a file in in ap append mo mode. *t *t adds ne ne% co content aatt tth he en end of of tth he fi file.

ios::ate

*t is used to %rite ne% contents at the end of the file and %e can also modify the pre)ious contents.

ios: ios::n :noc ocre reat atee ios: ios::n :nor orep epla lace ce

*t does does no nott all allo% o% to cr crea eate te a ne% ne% file file if it do does es not not ei eist st.. *t does does re repl plac acee the the old old fil filee %it %ith h ne% ne% fi file le..

" header file fstream.h is used in order to %ork %ith the files in + %hich include functions for openin$ and closin$ files. For opening a file:

*n order to open a file open79 mem'er function of fstream^s class is used. S2nta:' open 7filename, mode9

8ere filename is the name of the file to open and mode can 'e mode a)aila'le in +. For closing a file:

*n order to close a file close79 mem'er function of fstream^s fstream^s class is used. This function takes no ar$uments. Di((erent c6a$$e$ &$ed (or reading and 4riting to and (rom (i6e$ are a$ (o66o4$' C6a$$e$

De$cri5tion

ofstream

This class is used for %ritin$ onto files

ifstream

*t is used for readin$ from files

fstream

*t is used for 'oth readin$ and %ritin$ onto files

Program' -  This Menu Driven Program explains how to take get data from user, how to append data, how to modify record and how to display records 6include 7iotrea.h8 6include 7ftrea.h8

18

 

6include 7conio.h8 taticint totrec=; //totrec variable Feep tracF for total variable entered//>nitially ?ecord canned are Gerovoid ain() # int choice; while(3) # clrcr(); cout779"hooe your choice
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF