C++ Practical File Complete
Full text
(2)
(3) P .
(4) c By: Prabhat Kumar | cc|c c
(5)
(6) c c c c ccc c c c | cÚ ter a y umber. !"|# cHere i this program we declare two variables a d fact. To fi d the factorial of a umber we use data type i.e, u sig ed i t . $ !c %!c #i clude<iostream.h> #i clude<co io.h> i t mai . //begi. i g of mai f. { u sig ed i t ,fact;. //variable declaratio. u sig ed i t factorialu sig ed i t;. //fu ctio declaratio. cout<<"PROGRAM TO FIND A FACTORIAL OF ANY NUMBÚR:\ "; cout<<"\ Ú ter a y o.=";. //sta dard output. ci >> ;. //sta dard i put. fact=factorial ;. //fu ctio call. cout<<"\ The value of factorial o.="<<fact; retur 0;. //sta dard output //retur type. } u sig ed i t factorialu sig ed i t x. //fu ctio defi atio. { u sig ed i t i,f=1; fori=x;i>=1;i--. //for loop. f=f*i;. //operatio. retur f; }. //variable declaratio. //retur type / /e d of programc. P P
(7)
(8) P .
(9) c By: Prabhat Kumar | c. P P
(10)
(11) P .
(12) c By: Prabhat Kumar | &c|c c
(13)
(14) c c c c'ccc c c | ccÚ ter a y umber. !"|# cHere i this program we declare two variables ͚a,b͛ a d o e fu ctio i.e,cube . To fi d the cube of a umber we use formula i.e,cube=side*side*side . $ !c %!c #i clude<iostream.h> #i clude<co io.h> i t mai . //begi i g of mai f. { i t a,b; i t cubei t;. //declaratio of variables //declaratio of fu ctio. cout<<"PROGRAM TO FIND VOLUMÚ OF CUBÚ :\ "; cout<<"\ Ú ter a y umber="; ci >>a; b=cubea;. //sta dard output. //sta dard i put //fu ctio call. cout<<"\ After cubi g the result=";. //sta dard output. cout<<b; retur 0;. //retur type. } i t cubei t x. //fu ctio defi atio. { i t y; y=x*x*x;. //declaratio //operatio. retur y; }. //e d of program. c P P
(15)
(16) P .
(17) c By: Prabhat Kumar | c. P P
(18)
(19) P .
(20) c By: Prabhat Kumar | (c|c c
(21)
(22) c c)*c )c c cc+c'c | ccÚ ter the value of a a d b. !"|# ccSwap mea s to i tercha ge the values I this program we declare two variables a a d b. To swap two umbers we take a other temperory variable i.e, z i user defi ed fu ctio . $ !c %! #i clude<iostream.h> #i clude<co io.h> i t mai . //begi. i g of mai f. { i t a,b;. //variable declaratio. void swapi t,i t;. //fu ctio declaratio. cout<<"PROGRAM TO SWAP TWO NO'S USING CALL BY VALUÚ :\ \ "; cout<<"Ú ter the value of a="; ci >>a;. //sta dard output. //sta dard i put. cout<<"Ú ter the value of b="; ci >>b;. //sta dard output. //sta dard i put. cout<<"\ \ Before swappi g the values are\ \ \ta="<<a; //sta dard output cout<<"\ \ \tb="<<b; swapa,b;. //sta dard output //fu ctio call. retur 0;. //retur type. } void swapi t x,i t y. //fu ctio defi atio. { i t z;. //variable declaratio. z=x; x=y; P P
(23)
(24) P .
(25) c By: Prabhat Kumar y=z; cout<<"\ \ After swappi g the values are\ \ \ta="<<x; //sta dard output cout<<"\ \ \tb="<<y; } //e d of program. | . P P
(26)
(27) P .
(28) c By: Prabhat Kumar | ,c|c c
(29)
(30) c c)*c )c c cc+c c | ccÚ ter the value of a a d b. !"|# cRefe e ce mea s address .I this program we declare two variables a a d b. To swap two umbers by call by refere ce we take a other temperory variable i.e, z i user defi ed f. $ !c %! #i clude<iostream.h> #i clude<co io.h> i t mai . //begi i g of mai f. { i t a,b;. //variable declaratio. void swapi t,i t;. //fu ctio declaratio. cout<<"PROGRAM TO SWAP TWO NO'S USING CALL BY RÚFÚRÚNCÚ:\ \ "; cout<<"Ú ter the value of a="; ci >>a;. //sta dard output. //sta dard i put. cout<<"Ú ter the value of b="; ci >>b;. //sta dard output. //sta dard i put. cout<<"\ Before swappi g the result=\ \ \ta="<<a; cout<<"\ \ \tb="<<b; swapa,b; retur 0;. //fu ctio call //retur type. } void swapi tx,i ty. //fu ctio defi atio. { i t z;. //variable declaratio. z=x; x=y; P P
(31)
(32) P .
(33) c By: Prabhat Kumar y=z; cout<<"\ After swappi g the result are:\ \ \ta="<<x; cout<<"\ \ \tb="<<y; }. //e d of program. c | . P P
(34)
(35) P .
(36) c By: Prabhat Kumar | -c|c c
(37)
(38) c c c c *c c c c * cc c c !"|# cI this program we declare variable ͚ ͛a d fu ctio ͚si͛a d apply the formula of simple i terest i.e,S.I=p*r*t/100 i user defi ed f. $ !c %! #i clude<iostream.h> #i clude<co io.h> i t mai . //begi. i g of mai f. { float ;. //variable declaratio. float sifloat, float, float t=5; //fu ctio declaratio cout<<"PROGRAM TO FIND SIMPLÚ INTÚRÚST:"; =si000,7;. //f call. cout<<"\ \ The simple i terest=";. //sta dard output. cout<< ;. //sta dard output. retur 0;. //retur type. } float sifloat x,float y,float z. //formal parameter. { float a; a=x*y*z/100;. //variable declaratio //operatio. retur a; }. //e d of program. c c c c P P
(39)
(40) P .
(41) c By: Prabhat Kumar | . P P
(42)
(43) P .
(44) c By: Prabhat Kumar | .c*c c c c c/c c* c | ccÚ ter a y ame, age a d perce tage. !"|# Structure is a collectio of dissimiliar type of data eleme ts.cI this program the structure ame is stude t .We declare three variable i.e, ame, age a d per .We have to e ter the ame ,age a d per of two stude ts. $ !c %! c #i clude<iostream.h> #i clude<co io.h> struct stude t. // ame of the structure. { char ame[0]; i t age; float per; }s1,s; i t mai . //begi. i g of mai f. { cout<<"PROGRAM TO ÚNTÚR THÚ NAMÚ, AGÚ a d PÚRCÚNTAGÚ USING STRUCTURÚ:\ \ "; cout<<"Ú ter the Name age a d per of first perso :"<<e dl; //sta dard output cout<<"Ú ter ame="; ci >>s1. ame;. //sta dard output //sta dard i put. cout<<"\ Ú ter the age="; ci >>s1.age; cout<<"\ Ú ter the per="; ci >>s1.per; cout<<"\ Ú ter the Name age a d per of seco d perso :"<<e dl; cout<<"Ú ter ame=";. P P
(45)
(46) P .
(47) c By: Prabhat Kumar ci >>s. ame; cout<<"\ Ú ter the age="; ci >>s.age; cout<<"\ Ú ter the per="; ci >>s.per; cout<<"\ The ame,age a d per of s1:"; cout<<"\ Name="<<s1. ame<<"\ Age="<<s1.age<<"\ Per="<<s1.per; cout<<"\ The ame,age a d per of s:"; cout<<"\ Name="<<s. ame<<"\ Age="<<s.age<<"\ Per="<<s.per; retur 0; }. //retur type //e d of program c. | . P P
(48)
(49) P .
(50) c By: Prabhat Kumar | 0c*c c c c * cc c c | ccÚ ter the value of a a d b. !"|# cData abstractio mea s to k ow about the esse tial features by by hidi g the backgrou d details. I this program the class ame is ABC .We declare three variables a,b,c a d member fu ctio s read, process a d show.We have to add two umbers. I mai f we create o e object i.e,͚o1͛ $ !c %! #i clude<iostream.h> #i clude<co io.h> class ABC. // ame of class. { i t a,b,c;. //data member. public: void read. //member fu ctio. { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF DATA ABSTRACTION :\ "; cout<<"\ Ú ter the value of a="; //sta dard output ci >>a;. //sta dard i put. cout<<"\ Ú ter the value of b="; //sta dard output ci >>b;. //sta dard i put. } void process. //member f. { c=a+b;. //operatio. } void show. //member f. { P P
(51)
(52) P .
(53) c By: Prabhat Kumar cout<<"\ After addi g the result="; cout<<c; } }; i t mai . //begi i g of mai f. { ABC o1;. //creatio of object. o1.read;. //i voke member f. o1.process; o1.show; retur 0; }. //retur type //e d of program c. | . P P
(54)
(55) P .
(56) c By: Prabhat Kumar | 1c*c c c c * cc * c | ccÚ ter the value of a a d b. !"|# cData e capsulatio mea s the wrappi g up of data a d fu ctio s i a si gle u itcalled class. I this program the class ame is ABC .We declare three data members a,b,c a d member fu ctio s read, process a d show.We have to add two umbers. I mai f we create o e object i.e,͚o1͛c $ !c %! #i clude<iostream.h> #i clude<co io.h> class ABC. // ame of class. { i t a,b,c;. //data member. public: void read. //member fu ctio. { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF DATA ÚNCAPSULATION :\ "; cout<<"\ Ú ter the value of a="; //sta dard output ci >>a;. //sta dard i put. cout<<"\ Ú ter the value of b="; //sta dard output ci >>b;. //sta dard i put. } void process. //member f. { c=a+b;. //operatio. } void show. //member f. {. P P
(57)
(58) P .
(59) c By: Prabhat Kumar cout<<"\ After addi g the result="; cout<<c; } }; i t mai . //begi i g of mai f. { ABC o1;. //creatio of object. o1.read;. //i voke member f. o1.process; o1.show; retur 0; }. //retur type //e d of program. | c. P P
(60)
(61) P .
(62) c By: Prabhat Kumar | 2c*c c c c * ccc c3 c | ccÚ ter a y umber. !"|# cI this program the class ame is ABC .We declare two data members a,b a d member fu ctio s read a d process.We have to show greater betwee two umbers. I mai f we create o e object i.e,͚o1͛c $ !c %! #i clude<iostream.h> #i clude<co io.h> class ABC. // ame of class. { i t a,b;. //data member. public: void read. //member fu ctio. { cout<<"\ Ú ter the value of a="; //sta dard output ci >>a;. //sta dard i put. cout<<"\ Ú ter the value of b="; //sta dard output ci >>b;. //sta dard i put. } void show. //member f. { Ifa>b. //if loop. { cout<<"a is greater"; } else. P P
(63)
(64) P .
(65) c By: Prabhat Kumar { cout<<"\ b is greater";. //operatio. } } }; i t mai . //begi i g of mai f. { ABC o1,o;. //creatio of object. cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF CLASSÚS AND OBJÚCTS:\ "; o1.read;. //i voke member f. o1.show; o.read; o.show; retur 0; }. //retur type //e d of program. c c c c c c c c c c. P P
(66)
(67) P .
(68) c By: Prabhat Kumar | c c. c c c. c c P P
(69)
(70) P .
(71) c By: Prabhat Kumar | 4c|c c
(72)
(73) c c c c * cc c3 c | ccÚ ter a y umber. !"|# cI this program the class ame is ABC .We declare data member i.e, ͚ ͛ a d member fu ctio s read a d process.We have to show whether the umber is eve or odd. I mai f we create two objects i.e,͚o1 a d o͛c $ !c %! #i clude<iostream.h> #i clude<co io.h> class ABC. // ame of class. { i t ;. //data member. public:. //access level. void read. //member fu ctio. { cout<<"\ Ú ter a y umber="; //sta dard output ci >> ;. //sta dard i put. } void show. //member f. { if ==0. //if loop. { cout<<"\ The umber is eve \ "; } else { cout<<"\ The umber is odd\ ";. }. P P
(74)
(75) P .
(76) c By: Prabhat Kumar } }; i t mai . //begi. i g of mai f. { ABC o1,o;. //creatio of object. cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF CRÚATING OBJÚCTS:\ "; o1.read;. //i voke member f. o1.show; o.read; o.show; retur 0; }. //retur type //e d of program c. | c. c P P
(77)
(78) P .
(79) c By: Prabhat Kumar | c|c c
(80)
(81) c cc c cc cc c | ccÚ ter a y value of a. !"|# Here class ame is ABC,data members are ab, member fu ctio s are read, process a d show. We have to fi d the square of a umber. c $ !c %!c #i clude<iostream.h> #i clude<co io.h> class ABC. // ame of class. { i t a,b;. //data member. public: void read. //member fu ctio. { cout<<"PROGRAM TO ACCÚSS THÚ DATA MÚMBÚR AND MÚMBÚR FUNCTION :\ "; cout<<"\ Ú ter the value of a="; //sta dard output ci >>a;. //sta dard i put. } void process. //member f. { b=a*a;. //operatio. } void show. //member f. { cout<<"\ After squari g the result="; cout<<b; } P P
(82)
(83) P .
(84) c By: Prabhat Kumar }; i t mai . //begi i g of mai f. { ABC o1;. //creatio of object. o1.read;. //i voke member f. o1.process; o1.show; retur 0; }. //retur type //e d of program. c c c c | :. c P P
(85)
(86) P .
(87) c By: Prabhat Kumar | &c|c c
(88)
(89) c c c c * cc+cc3 c | ccÚ ter a y value of five item o͛s a d price. !"|# Array is a collectio of similar type of data eleme ts.I this program the class ame is ͚item͛.We declare data member i.e,item o price a d member fu ctio readshow .We have to show item o.a d price of five items. I mai f we take array of object o1[5].c $ !c %! #i clude<iostream.h> #i clude<co io.h> class item. // ame of class. { i t item o;. //data members. float price; public: void readi t i,float p. //member f. { item o=i; price=p; } void show. //member f. { cout<<"item o="<<item o<<"\t"; cout<<"\tprice="<<price<<"\ ";. //sta dard output //sta dard output. } }; i t mai . //begi i g of mai f. {. P P
(90)
(91) P .
(92) c By: Prabhat Kumar i t it o; float pri; item o1[5];. //array of object. cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF ARRAY OF OBJÚCT:\ "; cout<<"The item o a d price of Five items are\ "; fori t i=0;i<5;i++. //for loop. { cout<<i<<":"<<e dl; cout<<"item o="; ci >>it o; cout<<"price="; ci >>pri;. //sta dard i put. o1[i].readit o,pri; } cout<<"\ All the item o a d price of Five items are\ \ "; fori=0;i<5;i++ { o1[i].show; } retur 0; }. //retur type //e d of program. c c c c. P P
(93)
(94) P .
(95) c By: Prabhat Kumar c | c c. c. P P
(96)
(97) P .
(98) c By: Prabhat Kumar | (c|c c
(99)
(100) c cc )c c c cc c3 cc56c c | ccÚ ter a y value of hours a d mi utes. !"|# cHere the class ame is time, data member are hours a d mi utes, member fu ctio s are read show. I this program we have to pass object as argume t i place of values. $ !c %!c c#i clude<iostream.h> #i clude<co io.h> class time. // ame of the class. { i t hours;. //variable declaratio. i t mi utes; public: void readi t h,i t m. //member fu ctio //temporary variable/argume t. { hours=h; mi utes=m; } void show. //member fu ctio. { cout<<"\ Hours="<<hours<<"\t\t"<<"Mi utes="<<mi utes; //sta dard output } void addtime, time;. //f declaratio. }; void time::addtime t1, time t { i t h1,m1; P P
(101)
(102) P .
(103) c By: Prabhat Kumar hours=t1.hours+t.hours; mi utes=t1.mi utes+t.mi utes; h1=mi utes/60; m1=mi utes60; hours=hours+h1; mi utes=m1; } i t mai . //begi i g of mai f. { time t1,t,t3;. //creatio of object. cout<<"PROGRAM TO ADD TWO diff. MINUTÚS HOURS USING OBJÚCT AS F ARGUMÚNT:"; cout<<"\ Before addi g hours a d mi utes are\ "; t1.read5,40;. //i voki g read f. t.read3,50; t3.addt1,t; t1.show;. //i voki g show f. t.show; cout<<"\ \ After addi g hours a d mi utes are\ "; t3.show; retur 0; }. //retur type //e d of program. c c c c. P P
(104)
(105) P .
(106) c By: Prabhat Kumar | :. c. c c P P
(107)
(108) P .
(109) c By: Prabhat Kumar | ,c|c c
(110)
(111) c cc )c c c c3 cc56c c | ccÚ ter a y value of hours a d mi utes. !"|# cHere the class ame is dista ce, data members are feet a d i ches, member fu ctio s are read show. I this program we have to pass object as argume t i place of values. $ !c %!c #i clude<iostream.h>c #i clude<co io.h> class dista ce. // ame of the class. { i t feet;. //variable declaratio. i t i ches; public: void readi t i,i t j. //member fu ctio //temporary variable/argume t. { feet=i; i ches=j; } void show. //member fu ctio. { cout<<"\ Feet="<<feet<<"\tI ches="<<i ches; //sta dard output } void adddista ce,dista ce;. //f declaratio. }; void dista ce::adddista ce d1,dista ce d { i t f1,i c1; P P
(112)
(113) P .
(114) c By: Prabhat Kumar feet=d1.feet+d.feet; i ches=d1.i ches+d.i ches; f1=i ches/1; i c1=feet1; feet=feet+f1; i ches=i c1; } i t mai . //begi i g of mai f. { dista ce d1,d,d3;. //creatio of object. cout<<"PROGRAM TO ADD TWO DISTANCÚS USING OBJÚCT AS F ARGUMÚNT:\ \ "; cout<<"Before addi g feet a d i ches are\ "; d1.read5,11;. //i voki g read f. d.read3,9; d3.addd1,d; d1.show;. //i voki g show f. d.show; cout<<"\ \ After addi g feet a d i ches are\ "; d3.show;. retur 0; }. //retur type //e d of program. c c c. P P
(115)
(116) P .
(117) c By: Prabhat Kumar | :. c. c P P
(118)
(119) P .
(120) c By: Prabhat Kumar | -c|c c
(121)
(122) c c c c * cc c c | ccÚ ter a y value of a b. !"|# I li e fu ctio s are used to remove the overheads by pasti g the calculatio i every fu ctio .Here i this program we take abc as class ame ,two access levels private public ,two member f͛s as read show . $ !c %!c #i clude<iostream.h> #i clude<co io.h> class abc. // ame of class. { i t a,b,c;. //data member. public:. //access level. void read. //member f. { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF INLINÚ FUNCTION:\ \ "; cout<<"Ú ter a=";. //sta dard output. ci >>a;. //sta dard i put. cout<<"Ú ter b="; ci >>b; } void show; };. cvoid abc::show. //i li e f. { cout<<"After applyi g i li e fthe values are";. P P
(123)
(124) P .
(125) c By: Prabhat Kumar cout<<"a="<<a<<" a d "<<"b="<<b; } i t mai . //begi i g of mai . { abc o1;. //creatio of object. o1.read;. //i voki g read f. o1.show; retur 0; }. //retur type //e d of program. c | c c. P P
(126)
(127) P .
(128) c By: Prabhat Kumar | .c|c c
(129)
(130) c c c c ccc567cccc | ccÚ ter a y value of a b. !"|# Here i this program we have to add two umbers. Taki g add as class ame ,two access levels private public ,two member f͛s as read show . $ !c %!c #i clude<iostream.h> #i clude<co io.h> class add. // ame of class. { i t a,b,c;. //variable declaratio. public: void i put;. //member fu ctio declaratio. void process; void output; }; void add::i put. //member f defi atio. { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ DÚFINATION OF MÚMBÚR F'S OF A CLASS:\ "; cout<<"\ Ú ter the value of a="; //sta dard output ci >>a;. //sta dard i put. cout<<"\ Ú ter the value of b="; //sta dard output ci >>b;. //sta dard i put. } void add::process. //member f defi atio. { c=a+b;. //operatio. P P
(131)
(132) P .
(133) c By: Prabhat Kumar } void add::output. //member f defi atio. { cout<<"\ After addi g="; cout<<c;. //sta dard output. //sta dard output. } i t mai . //begi. i g of mai . { add a1; a1.i put;. //creatio of object //i voki g M f. a1.process; a1.output; retur 0; }. //retur type //e d of program. | . P P
(134)
(135) P .
(136) c By: Prabhat Kumar | 0c|c c
(137)
(138) c c c c * cc c c | ccÚ ter a y value of a b. !"|# cFrie d fu ctio are used to access the data member.I this program the class ame is ͚abc͛.We declare data member i.e, a b a d member fu ctio read .We have to add two umbers. I mai f we create o e objects i.e,o1.c $ !c %! #i clude<iostream.h> #i clude<co io.h> class abc. // ame of class. { i t a,b;. //data member. public:. //access level. void read. //member fu. ctio. { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF FRIÚND FUNCTION:\ "; cout<<"\ Ú ter the value of a="; //sta dard output ci >>a;. //sta dard i put. cout<<"\ Ú ter the value of b="; //sta dard output ci >>b;. //sta dard i put. } ci t fu cabc;. //frie d fu ctio. }; i t fu cabc o. //fu ctio defi atio. { retur o.a+o.b;. //retur object. }. P P
(139)
(140) P .
(141) c By: Prabhat Kumar. i t mai . //begi. i g of mai . { i t c; abc o1; o1.read; c=fu co1;. //declaratio //creatio of object //i voke M F //fu ctio call. cout<<"\ After addi g the result="; cout<<c; retur 0; }. //retur type //e d of program. | c. c P P
(142)
(143) P .
(144) c By: Prabhat Kumar | 1c|c c
(145)
(146) c c c c * cc . c3 c | ccÚ ter a y two feet a d i ches. !"|# cHere the class ame is dista ce, data members are feet a d i ches, member fu ctio s are read show. I this program we have to pass object as argume t i place of values a d retur i g object. $ !c %!c #i clude<iostream.h> #i clude<co io.h> class dista ce. // ame of class. { i t feet;. //data member. i t i ches; public: void readi t f, i t i. //member f. { feet=f; i ches=i; } void show. //member f. { cout<<"Feet="<<feet<<"\t"<<"I ches="<<i ches<<e dl; //sta dard output } frie d dista ce sumdista ce,dista ce;. //frie d f. }; dista ce sumdista ce dd1,dista ce dd {. P P
(147)
(148) P .
(149) c By: Prabhat Kumar dista ce dd3; dd3.feet=dd1.feet+dd.feet; dd3.i ches=dd1.i ches+dd.i ches; ifdd3.i ches>=1 { dd3.feet++; dd3.i ches=dd3.i ches-1; } dd3;. //retur i g object. } i t mai . //begi i g of mai . { dista ce d1,d,d3;. //creatio of object. cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF RÚTURNING OBJÚCT:\ "; cout<<"\ Before Addi g feet a d i ches are:\ \ "; d1.read5,9;. //i voki g memberf. d.read3,9; d3=sumd1,d; d1.show; d.show; cout<<"\ After Addi g feet a d i ches are:\ \ "; d3.show; retur 0; }. //retur type //e d of program. c. P P
(150)
(151) P .
(152) c By: Prabhat Kumar | :. c. c c P P
(153)
(154) P .
(155) c By: Prabhat Kumar | 2c|c c
(156)
(157) c c c c * cc c c* c !"|# Co structor are special type of member fu ctio s. Co structors are always public. Here the class ame is abc,o e default co structor,o e parameterised co structor we have just to show the values. $ !c %! #i clude<iostream.h> #i clude<co io.h> class abc. // ame of class. { i t a,b;. //data member. public:. //access level. abc. //default co structor. { a=4; b=3; } abci t i,i t j. //parameterised co structor. { a=i; b=j; } void show. //member fu ctio. { cout<<"\ The value of a="<<a;. //sta dard output. cout<<"\ The value of b="<<b<<"\ \ "; //sta dard output }. P P
(158)
(159) P .
(160) c By: Prabhat Kumar }; i t mai . //begi. i g of mai f. { cout<<"PROGRAM TO DÚMNSTRATÚ THÚ CONCÚPT OF DÚFAULTPARAMÚTÚRISÚD CONSTRUCTOR:"; abc o1;. //creatio of object. abc o5,15; cout<<"\ \ The values of Default co structor are:\ "; o1.show;. //i voki g member f. cout<<"\ The values of Parameterised Co structor are:\ "; o.show; retur 0; }. //retur type //e d of program. | c. c. P P
(161)
(162) P .
(163) c By: Prabhat Kumar | &4c|c c
(164)
(165) c c c c * cc !"|# cDestructors are also special type of member fu ctio s. Here the class ame is alpha, Co structor ameis alpha a d destructor ame is ~alpha. $ !c %! #i clude<iostream.h> #i clude<co io.h> i t cou t=0; class alpha. // ame of class. { public: alpha. //co structor. { cou t++;. //i creme t. cout<<"\ Object created:"<<"\ "<<cou t<<"\ ";. //sta dard output. } ~alpha. //destructor. { cout<<"\ Object destroyed:"<<"\ "<<cou t<<"\ "; cou t--;. //sta dard output. //decreme t. } }; i t mai . //begi i g of mai . { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF DÚSTRUCTOR:\ \ "; alpha a1;. //creatio of object. P P
(166)
(167) P .
(168) c By: Prabhat Kumar alpha a; //destructor i voke automatically retur 0; }. //retur type //e d of program c. | c c. c. P P
(169)
(170) P .
(171) c By: Prabhat Kumar | &c|cc c c c cc3 c cc*c+c c c cc c c c56c !"|# cStatic member fu ctio s are those fu ctio s which co tai o ly static data members a d static member fu ctio s . Here i this program we have to cou t the o. of objects i a program. $ !c %!c #i clude<iostream.h> #i clude<co io.h> class x. // ame of class. { i t item o;. //data member. float price; static i t cou t; public: void readi t i,float f. //static data member //access level //member fu ctio. { item o=i; price=f; cou t++; } void show. //member fu ctio. { cout<<"item o="<<item o<<"\t";. //sta dard output. cout<<"price="<<price<<"\ "; } static void display. //static member fu ctio. {. P P
(172)
(173) P .
(174) c By: Prabhat Kumar cout<<cou t; } };. i t x::cou t=0; i t mai . //begi. i g of mai f. { x o1,o,o3;. //creatio of object. cout<<"PROGRAM TO COUNT NO. OF OBJÚCT IN A PROGRAM BY USING D.M. S.M.F:\ "; cout<<"\ The item o a d price are\ \ "; o1.read5,1.5;. //i voki g member fu ctio. o.read7,13.4; o3.read,3.5; o1.show; o.show; o3.show; cout<<"\ \ a d COUNT="; x::display; retur 0; }. //retur type //e d of programc. c c c c c. P P
(175)
(176) P .
(177) c By: Prabhat Kumar | :. c. P P
(178)
(179) P .
(180) c By: Prabhat Kumar | &&c|cc c
(181)
(182) c c c c * cc*+c c !"|# cCopy co structor copies values from mai fu ctio to member fu ctio .Here the class ame is abc.I this program we use three co structors o e is default co structor,seco d is parameterised co structor a d third is copy co structor. $ !c %!c #i clude<iostream.h> #i clude<co io.h> class abc. // ame of class. { i t i,j;. //variable declaratio. public:. //member f. abc. //default co structor. { i=5; j=4; } abci t a, i t b. //parameterised co structor. { i=a; j=b; } abcabcs. //copy co structor. { i=s.i; j=s.j; }. P P
(183)
(184) P .
(185) c By: Prabhat Kumar void display. //member f. { cout<<"\ti="<<i<<"\tj="<<j<<e dl; //sta dard output } }; i t mai . //begi i g of mai f. { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF COPY CONSTRUCTOR:\ "; abc o1;. //creatio of object. abc o5,15; abc o3o;. //abc o3=o;. cout<<"\ The values of Default Co structor are:\ \ "; o1.display; cout<<"\ The values of Parameterised Co structor are:\ \ "; o.display; cout<<"\ The values of Copy Co structor are:\ \ "; o3.display; retur 0; }. //retur type //e d of program. c c c c c c. P P
(186)
(187) P .
(188) c By: Prabhat Kumar | :. c. c P P
(189)
(190) P .
(191) c By: Prabhat Kumar | &(c|cc c
(192)
(193) c c c c * cc c c | ccÚ ter a y two umbers. !"|# cWhe a derived class i herits the features of base class,it is si gle i herita ce. Here we take two classes o e is base a d other is derived class.We have to add two umbers. $ !c %!c #i clude<iostream.h> #i clude<co io.h> class base. // ame of class. { private: i t a;. //access level //data member. protected: i t b; public: void read. //member fu ctio. { cout<<"\ Ú ter the value of a="; //sta dard output ci >>a;. //sta dard i put. } void get { cout<<"\ Ú ter the value of b="; ci >>b; } void show { P P
(194)
(195) P .
(196) c By: Prabhat Kumar cout<<a; } void put { cout<<b; } }; class derive:public base. //derive class. { private: i t d,e; public: void sca { cout<<"\ Ú ter the value of d="; ci >>d; } void add { e=d+b; } void display { cout<<"\ After addi g the result="; cout<<e;. P P
(197)
(198) P .
(199) c By: Prabhat Kumar } }; i t mai . //begi. i g of mai f. { cout<<"PROGRAM OF SINGLÚ INHÚRITANCÚ:\ \ "; derive d1; d1.get;. //creatio of object //i voki g member f. d1.sca ; d1.add; d1.display; retur 0;. //retur type. } c | :. c P P
(200)
(201) P .
(202) c By: Prabhat Kumar | &,c|cc c
(203)
(204) c c c c * cc *c c | ccÚ ter the value of a b. !"|# cWhe a derived class i herits the features of more tha o e base class the it is multiple i herita ce.cHere we take two base classes a d o e derived class. I this program we have to add two umbers. $ !c %!c #i clude<iostream.h> #i clude<co io.h> class base1. // ame of class. { protected: i t a;. //access level //data member. public: void read. //member fu ctio. { cout<<"\ Ú ter a="; ci >>a;. //sta dard output //sta dard i put. } void show { cout<<a; } }; class base. // ame of class. { protected:. P P
(205)
(206) P .
(207) c By: Prabhat Kumar i t b; public: void get { cout<<"\ Ú ter b="; ci >>b; } void put { cout<<b; } }; class derive:public base1,public base { private: i t c; public: void add { c=a+b; } void pri t { cout<<"\ \ After addi g="; cout<<c;. P P
(208)
(209) P .
(210) c By: Prabhat Kumar } }; i t mai . //begi. i g of mai f. { cout<<"PROGRAM OF MULTIPLÚ INHÚRITANCÚ\ "; derive d1;. //creatio of object. d1.read;. //i voki g member f. d1.get; d1.add; d1.pri t; retur 0; }. //retur type //e d of program c. | :. c P P
(211)
(212) P .
(213) c By: Prabhat Kumar | &-c|c c
(214)
(215) c c c c * cc 'c c | ccÚ ter the value of a b. !"|# The tra sitive ature of i herita ce is reflected by this form of i herita ce. Whe a subclass i herits from a class that itself i herits from a other class, it is k ow as multilevel i herita ce. $ !c %!c #i clude<iostream.h> #i clude<co io.h> class stude t. // ame of class. { protected:. //access level. i t roll o;. //data member. char ame[0]; public: void read. //member fu ctio. { cout<<"Roll o="; ci >>roll o;. //sta dard output //sta dard i put. cout<<"Name="; ci >> ame; } void show { cout<<"The roll o="<<roll o<<"\t"; cout<<"The ame="<< ame; } }; P P
(216)
(217) P .
(218) c By: Prabhat Kumar class stude t_details:public stude t { protected: i t marks[3]; public: void read { stude t::read; cout<<"Marks[0]="; ci >>marks[0]; cout<<"Marks[1]="; ci >>marks[1]; cout<<"Marks[]="; ci >>marks[]; } void show { stude t::show; cout<<marks[0]<<marks[1]<<marks[]; } }; class stude t_total:public stude t_details { private: i t total;. P P
(219)
(220) P .
(221) c By: Prabhat Kumar public: void read { stude t_total::read; } void cal { total=marks[0]+marks[1]+marks[]; } void show { stude t_details::show; cout<<"Total="; cout<<total; } }; i t mai . //begi. i g of mai f. { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF MULTILÚVÚL INHÚRITANCÚ:\ "; stude t_total t1;. //creatio of object. t1.read; t1.cal; t1.show; retur 0; }. //retur type //e d of program. P P
(222)
(223) P .
(224) c By: Prabhat Kumar. | c c. P P
(225)
(226) P .
(227) c By: Prabhat Kumar | &.c|c c
(228)
(229) c c c c * cc c c | ccÚ ter a y roll o., ame a d three subject͛s marks. !"|# cWhe ma y subclasses i herit from a si gle base class, it is k ow as hirarchical i herita ce. $ !c %!cc #i clude<iostream.h> #i clude<co io.h> class stud_basic. //NAMÚ OF CLASS. { private:. //ACCÚSS LÚVÚL. char ame[0];. //DATA MÚMBÚR. i t roll o; public: void read. //MÚMBÚR FUNCTION. { cout<<"\ Ú ter roll o="; ci >>roll o;. //STANDARD OUTPUT. //STANDARD INPUT. cout<<"Ú ter ur ame="; ci >> ame; } void show { cout<<"Roll o="<<roll o<<e dl<<"Name="<< ame; } }; class stud_marks:public stud_basic P P
(230)
(231) P .
(232) c By: Prabhat Kumar { private: float marks[30],marks_obt; float per; public: void i put { stud_basic::read; cout<<"Ú ter three differe t marks:"<<e dl; cout<<"Marks[0]="; ci >>marks[0]; cout<<"Marks[1]="; ci >>marks[1]; cout<<"Marks[]="; ci >>marks[]; } void display { stud_basic::show; cout<<"\ Differe t marks are"<<e dl; cout<<"Marks1="<<marks[0]<<e dl<<"Marks="<<marks[1]<<e dl<<"Marks3="<<marks[]; cout<<"\ Marks_obt ="<<marks_obt<<e dl; cout<<"Perce tage="<<per; } void cal. P P
(233)
(234) P .
(235) c By: Prabhat Kumar { marks_obt=marks[0]+marks[1]+marks[]; per=floatmarks_obt/300*100; } }; class stud_fit ess:public stud_basic { private: float height,weight; public: void read { stud_basic::read; cout<<"Ú ter height a d weight:"<<e dl; cout<<"Height="; ci >>height; cout<<"Weight="; ci >>weight; } void show { stud_basic::show; cout<<"\ Height a d weight are:"<<e dl; cout<<"Height="<<height<<"\t"<<"Weight="<<weight; }. P P
(236)
(237) P .
(238) c By: Prabhat Kumar }; void mai . //BÚGINNING OF MAIN F. { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF HIRÚRACHICAL INHÚRITANCÚ:"; stud_marks s1; s1.i put;. //CRÚATION OF OBJÚCT //INVOKING MÚMBÚR FUNCTION. s1.cal; s1.display; stud_fit ess f1; f1.read; f1.show; }. //RÚTURN TYPÚ. c c | :. c P P
(239)
(240) P .
(241) c By: Prabhat Kumar | &0c|c c
(242)
(243) c c c c * cc+ c c !"|# cWhe a derived class i herits the features from multiple base classes which agai i herits the featurs of a si gle base class the it is k ow as hybrid i herita ce.Here we take four classes ,i this program we have to fi d the perce tage of all the marks obtai ed. $ !c %!cc #i clude<iostream.h> #i clude<co io.h> class stud_basic. // ame of class. { private: char ame[0];. //access level //data member. i t roll o; public: void read. //member fu ctio. { cout<<"Ú ter roll o="; ci >>roll o;. //sta dard output //sta dard i put. cout<<"Ú ter ame="; ci >> ame; } void show { cout<<"The roll o="<<roll o<<"\t"; cout<<"The ame="<< ame; } };. P P
(244)
(245) P .
(246) c By: Prabhat Kumar class stud_acd_marks:public stud_basic { protected: float marks[3]; public: void read { stud_basic::read;. //call read of base. cout<<"Marks1="<<e dl; ci >>marks[0]; cout<<"Marks="<<e dl; ci >>marks[1]; cout<<"Marks3="<<e dl; ci >>marks[]; } void show { stud_basic::show;. //call show of base. //cout<<marks[0]<<e dl; //cout<<marks[1]<<e dl; //cout<<marks[]<<e dl; } }; class sports {. P P
(247)
(248) P .
(249) c By: Prabhat Kumar protected: float score; public: void read { cout<<"Score="<<e dl; ci >>score; } void show { //cout<<score; } }; class stud_res:public stud_acd_marks,public sports { private: float marks_obt; float per; public: void cal { marks_obt=marks[0]+marks[1]+marks[]+score; per=marks_obt/400*100; } void show. P P
(250)
(251) P .
(252) c By: Prabhat Kumar { stud_acd_marks::show; sports::show; cout<<e dl<<"Marks_obt="<<marks_obt<<e dl<<"Per="<<per<<e dl; } }; i t mai . //begi. i g of mai f. { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF HYBRID INHÚRITANCÚ\ \ "; stud_res s1;. //creatio of object. s1.stud_acd_marks::read; s1.sports::read; s1.cal; s1.show; retur 0; }. //retur type //e d of program. c c c c c c c c c. P P
(253)
(254) P .
(255) c By: Prabhat Kumar c | :. c. P P
(256)
(257) P .
(258) c By: Prabhat Kumar | &1c|cc c
(259)
(260) c c c c * cc +c* c' c !"|# cLet us co sider the u ary plus operator. A plus operator whe used as a u ary,takes just o e opera d. The give program shows how the u ary plus operator is overloaded. $ !c %! c #i clude<iostream.h> #i clude<co io.h> class abc. // ame of class. { i t cou t; public:. //declaratio //access level. abc { cou t=0; } void show. //member fu ctio. { cout<<"\ \ \tThe cou t=";. //sta dard output. cout<<cou t; } void operator++ { cou t=cou t+1; } };. i t mai P P
(261)
(262) P .
(263) c By: Prabhat Kumar { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF UNARY OPÚRTATOR OVÚRLOADING:"; abc o1; ++o1;. //u ary overloadi g. o1.show; retur 0; }. //i voki g member f //retur type //e d of program. c | c c. P P
(264)
(265) P .
(266) c By: Prabhat Kumar | &2c|c c
(267)
(268) c c c c * cc +c* c' c | ccÚ ter a y values of hours a d mi utes. !"|# cOperator overloadi g mea s to do additio al job to a operator a special fu ctio with that operator called as operator overloadi g. The same mecha ism of u ary operator ca be used to overload a bi ary operator.The f otatio c=suma,b;ca be replaced by a atural looki g expressio c=a+b; $ !c %! #i clude<iostream.h> #i clude<co io.h> class time. // ame of class. { i t hours;. //data member. i t mi utes; public:. //access level. void read. //member fu ctio. { cout<<"Ú ter hours="; ci >>hours;. //sta dard output //sta dard i put. cout<<"Ú ter mi utes="; ci >>mi utes; } void show. //member fu ctio. { cout<<"After addi g hours a d mi utes :\ hours="<<hours;. //sta dard output. cout<<"\tmi utes="<<mi utes; } time operator+time tt P P
(269)
(270) P .
(271) c By: Prabhat Kumar { time tt3; tt3.hours=hours+tt.hours; tt3.mi utes=mi utes+tt.mi utes; iftt3.mi utes>=60 { tt3.hours++; tt3.mi utes=tt3.mi utes-60; } retur tt3; } }; i t mai { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF BINARY OPÚRATOR OVÚRLOADING:\ \ "; time t1,t,t3; t1.read;. //creatio of object //i voki g member f. t.read; t3=t1+t;. //operator overloadi g. t3.show; retur 0; }. //retur type //e d of programc. c c c. P P
(272)
(273) P .
(274) c By: Prabhat Kumar | c c. c. c c P P
(275)
(276) P .
(277) c By: Prabhat Kumar |(4c|c c
(278)
(279) c c c c * cc c' c | ccÚ ter a y value of a,b,c,g,h. !8* Overloadi g refers to the use of same thi g for differe t purposes. C++ also permits overloadi g of fu ctio s. This mea s that we ca use the same fu ctio ame to create fu ctio s that perform a variety of differe t tasks, usi g the co cept of fu ctio overloadi g. For e.g: I this program a overloaded add fu ctio ha dles differe t types of data as show below: $c c #i clude<iostream.h> #i clude<co io.h> i t mai . //begi. i g of mai. { cout<<"PROGRAM TO DÚMOSTRATÚ THÚ CONCÚPT OF FUNCTION OVÚRLOADING:\ "; i t addi t,i t;. //fu ctio declaratio. i t addi t; float addfloat,float; i t addi t,i t,i t; i t a,b,c,d,e,f;. //variable declaratio. float g,h,i; cout<<"Ú ter the value of a="; ci >>a;. //sta dard output. //sta dard i put. cout<<"Ú ter the value of b="; ci >>b; cout<<"Ú ter the value of c="; ci >>c; cout<<"Ú ter the value of g="; ci >>g; cout<<"Ú ter the value of h="; P P
(280)
(281) P .
(282) c By: Prabhat Kumar ci >>h; d=adda;. //fu ctio call. e=adda,b; f=adda,b,c; i=addg,h; cout<<"After addi g the results are:::::::\ "<<e dl; cout<<"Fu ctio with o e argume t"<<e dl<<"I put"<<e dl<<"a="<<a<<e dl<<"Result="<<d<<e dl; cout<<"Fu ctio with two argume t result="<<e dl<<"I put"<<e dl<<"a="<<a<<",b="<<b<<e dl<<"Result="<<e<<e dl; cout<<"Fu ctio with three argume t result="<<e dl<<"I put"<<e dl<<"a="<<a<<",b="<<b<<",c="<<c<<e dl<<"Result="<<f<<e dl; cout<<"Fu ctio with two floati g argume t result="<<e dl<<"I put"<<e dl<<"g="<<g<<",h="<<h<<e dl<<"Result="<<i; retur 0;. //retur type. } i t addi t x. //fu ctio defi itio. { i t y; y=x+x; retur y; } i t addi t x,i t y { i t z; z=x+y; retur z; } P P
(283)
(284) P .
(285) c By: Prabhat Kumar i t addi t p,i t q,i t r { i t s; s=p+q+r; retur s; } float addfloat m,float { float o; o=m+ ; retur o; }. | c. P P
(286)
(287) P .
(288) c By: Prabhat Kumar | (c|c c
(289)
(290) c c c c * cc' c567c !"|# How we do achieve polymorphism? It is achieved usi g what is k ow as virtual fu ctio s. Whe we use the same f ame i both the basederived classes, the f i base class is declared as virtual usi g the keyword virtual precedi g its ormal declaratio . $ !c %!c #i clude<co io.h> class base. // ame of class. { public: void display. //access level //member fu ctio. { cout<<"\ Display base"<<"\ \ ";. //sta dard output. } virtual void show. //virtual fu ctio. { cout<<"Show base"<<"\ \ "; } }; class derive:public base. //derive class. { public:. //access level. void display { cout<<"Display derive"<<"\ \ "; } void show. P P
(291)
(292) P .
(293) c By: Prabhat Kumar { cout<<"Show derive"; } }; i t mai . //begi. i g of mai. { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF VIRTUAL FUNCTION:\ "; base b;. //creatio of object. derive d; base *bptr; bptr=b; bptr->display; bptr->show; bptr=d; bptr->display; bptr->show; retur 0; }. //retur type //e d of programcc. c c c c c c c. P P
(294)
(295) P .
(296) c By: Prabhat Kumar | : c. c. c P P
(297)
(298) P .
(299) c By: Prabhat Kumar | (&c|c c
(300)
(301) c c c c * cc * c !"|# cTemplates is a ew co cept which e able us to defi e ge eric programmi g. Ge eric programmi g is a approach where ge eric types are used as parameters i algorithms so that they work for a variety of suitable data types a d data structure. $ !c %!cc #i clude<iostream.h> template<class t1,class t> class test. // ame of class. { t1 a; t b; public:. //access level. testt1 x,t y { a=x; b=y; } void show. //member f. { cout<<"\ \ The values are="; cout<<a<<"\t"<<b<<e dl;. //sta dard output //sta dard output. } };. P P
(302)
(303) P .
(304) c By: Prabhat Kumar i t mai . //begi i g of mai f. { cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF TÚMPLATÚS:\ "; test<float,i t>t11.95,7; test<float,char>t3.14,'a'; test<i t,float>t37,9.5; t1.show; t.show; t3.show; retur 0; }. //retur type //e d of program c. | :. c P P
(305)
(306) P .
(307) c By: Prabhat Kumar | ((c|cc c
(308)
(309) c c c c * cc c c c | ccÚ ter ur ame a d roll o. !"|# cFiles are used to store the output of program. $ !c %!c #i clude<iostream.h> #i clude<fstream.h> i t mai . //begi i g of mai f. { char ame[0];. //variable declaratio. i t roll o; cout<<"PROGRAM OF FILÚS a d STRÚAMS\ "; cout<<"\ Ú ter ur ame="; ci >> ame;. //sta dard output //sta dard i put. cout<<"\ Ú ter ur roll o="; ci >>roll o; ofstream out"stude t"; out<< ame<<"\ "; out<<roll o; out.close; ifstream i "stude t"; i >> ame; i >>roll o; i .close; cout<<"\ Name="<< ame<<"\t"; cout<<"a d\t Roll o="<<roll o;. //sta dard output //sta dard output. P P
(310)
(311) P .
(312) c By: Prabhat Kumar retur 0; }. //retur type //e d of program. c c | :. c. P P
(313)
(314) P .
(315) c By: Prabhat Kumar | (,c|c c
(316)
(317) c c c c * cc c c* c | ccÚ ter ur ame. !"|# cThe fu ctio s get a d put are member fu ctio s of the file stream class fstream.get is used to read a si gle character from the file.put is used to write a si gle character to the output file. $ !c %!cc #i clude<iostream.h> #i clude<co io.h> i t mai . //begi. i g of mai fu ctio. { char a[0];. //variable declaratio. cout<<"PROGRAM TO DÚMONSTRATÚ THÚ CONCÚPT OF GÚT AND PUT:\ "; cout<<"e ter ur ame=";. //sta dard output. ci .geta,0; cout.writea,0; retur 0; }. //cout<<͟The ame=͟<<a; //retur type //e d of programc. cc c c c c c c c c c P P
(318)
(319) P .
(320) c By: Prabhat Kumar c | :. c. P P
(321)
(322) P .
(323)
Related documents
The instrument contained 13 attitudinal statements related to the newsletter received, measured on a seven-point Likert scale: reactions to the site (newsletter frequency, drop
If the institution previously submitted an education-impacting disability initial- eligibility waiver request for the student-athlete, the institution must provide a
http://www.sba.gov/content/sbic-directory Metric Loans (no equity features) Debt with Equity features Equity Typical Financing Size Over 3 Year Period $250,000 to $10 million
MP, Mixed Practice (clinical practice with a large animal component); IAP, Intensive Animal Production; CAP, companion animal practice; VNP, composite variable veterinary
(Added-AFRC) AFRC crewmembers who are required by the appropriate AFI 11-2 MDS Specific Volume 1 to attend a simulator or ground training session where CRM is part of the
See, e.g., In re Sage Realty Corp., 91 N.Y.2d 30, 35 (NY 1997) (“We conclude that the majority position, as adopted in the final draft of the American Law Institute Restatement
With this result it can be confidently concluded that activated carbon from waste orange peels is better than waste lemon peels activated carbon that can be used
Credit data captured by the credit bureaus results in nearly 1000 different variables that are used in many different credit scoring predictive models. RGA has been