III Sem BCA
C++ Lab Manual
SECTION-A /* 1. Program to swap two values values using pointers pointers and reeren!e varia"les */ #in!lude$iostream.%& #in!lude$!onio.%& void swappoint'int *(int *) void swapre'int +(int +) void main') , int a(" !lrs!r') !out$$Enter two num"ers$$endl num"ers$$endl !in&&a&&" !out$$nalues !out$$nalues o A and 0 "eore !alling un!tion$$endl !out$$A$$a$$ !out$$A$$a$$ 0$$" swappoint'+a(+") !out$$nalues !out$$nalues o A and 0 ater !alling swappoint')$$endl swappoint')$$endl !out$$A$$a$$ !out$$A$$a$$ 0$$" swapre'a(") !out$$nalues !out$$nalues o A and 0 ater !alling swapre')$$endl !out$$A$$a$$ !out$$A$$a$$ 0$$" get!%') 2 void swappoint'int *l(int *m) , int 3 3*l *l*m *m3 2 void swapre'int +l(int +m) , int 3 3l lm m3 2 /* 4.Program to ind 5argest and smallest o our num"ers 6sing inline un!tions. */ #in!lude$iostream.%& #in!lude$!onio.%& inline int largest'int 7( int 8) , return'7&897:8) 2 Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
inline int smallest'int 7( int 8) , return'7$897:8) 2 void main') , !lrs!r') int a("(!(d !out$$Enter an8 our num"ers: !in&&a&&"&&!&&d int larlargest'largest'a(")(lar larlargest'largest'a(")(largest'!(d)) gest'!(d)) !out$$n 5argest o t%e given our num"ers is:$$lar int smallsmallest'smallest'a(") smallsmallest'smallest'a(")(smallest'!(d) (smallest'!(d)) ) !out$$n Smallest o t%e given our num"ers is:$$small get!%') 2 /* ;. Program to !%e!3 w%et%er t%e given num"er is prime or not using un!tion overloading and also use deault arguments.*/ #in!lude$iostream.%& #in!lude$!onio.%& #in!lude$stdli".%& void prime')( prime'int(int1) prime'int(int1) void main') , int !%oi!e( n w%ile'1) , !lrs!r') !out$$n1.Prime !out$$n1.Prime num"er !%e!3ing wit%out parameters !out$$n4.Prime !out$$n4.Prime num"er !3e!3ing wit% parameters !out$$n;.E7it !out$$endl$$nEnter !out$$endl$$nEnter 8our !%oi!e: !in&&!%oi!e swit!%'!%oi!e) , !ase 1:prime') get!%') "rea3 !ase 4:!out$$Enter 4:!out$$Enter an8 num"er: !in&&n prime'n) get!%') "rea3 !ase ;:e7it'<) "rea3 Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
deault: !out$$nInvalid C%oi!e get!%') 2 2 2 void prime') , int n( lag1 !out$$Enter an8 num"er: !in&&n or'int i4i$n/4i==) i'n>i<) , lag< "rea3 2 i'lag1) !out$$endl$$T%e ?iven Num"er $$n$$ is a prime num"er else !out$$endl$$T%e ?iven Num"er $$n$$ is not a prime num"er 2 void prime'int n(int lag) , or'int i4i$n/4i==) i'n>i<) , lag< "rea3 2 i'lag1) !out$$endl$$T%e ?iven Num"er $$n$$ is a prime num"er else !out$$endl$$T%e ?iven Num"er $$n$$ is not a prime num"er 2
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
/* @.Program to ind a!torial o a num"er using un!tion overloading 'use "ot% dire!t and re!ursive met%ods to ind a!torial) */ #in!lude$iostream.%& #in!lude$!onio.%& #in!lude$stdli".%& void a!t') long int a!t'int) void main') , int !%oi!e( n or') , !lrs!r') !out$$1.a!torial o a num"er wit%out parameters$$endl !out$$4.a!torial o a num"er wit% parameters$$endl !out$$;.E7it$$endl !out$$endl$$Enter 8our !%oi!e: !in&&!%oi!e swit!%'!%oi!e) , !ase 1:a!t') get!%') "rea3 !ase 4:!out$$Enter t%e num"er: !in&&n !out$$n T%e a!torial o $$n$$ is $$a!t'n) get!%') "rea3 !ase ;:e7it'<) deault:!out$$n Invalid !%oi!e get!%') 2 2 2 void a!t') , int n(i !out$$endl$$Enter t%e num"er: !in&&n long int a!t1 or'i1i$ni==) a!ta!t*i !out$$endl$$T%e a!torial o $$n$$ is $$a!t 2
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
long int a!t' int n) ,
i'n<) return 1 else return n*a!t'n-1)
2 /* B.Program to a!!ept !ri!3et pla8ers name( total runs( and total mat!%es and print t%ese details wit% "atting average'6se arra8 o o"e!ts). */ #in!lude$stdio.%& #in!lude$!onio.%& #in!lude$iostream.%& #in!lude$iomanip.%& !lass !ri!3et , !%ar nameD;< loat truns loat tmat loat avg pu"li!: void getFdata') void putFdata') 2 void !ri!3et::getFdata') , !out$$Enter t%e pla8er name:$$endl !in&&name !out$$Enter total runs o t%e pla8er $$name$$:$$endl !in&&truns !out$$Enter total mat!%es pla8ed "8 $$name$$:$$endl !in&&tmat avgtruns/tmat 2 void !ri!3et::putFdata') , !out.set'ios::let(ios::adustield) !out$$setw'1<)$$name$$t$$setw'1<)$$truns$$t $$setw'1<)$$tmat$$t$$setw'1<)$$setpre!ision'4)$$avg$$endl 2 void main') , !lrs!r') int n Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
!ri!3et eDG G !out$$Enter num"er o pla8ers: !in&&n or'int i1i$ni==) , !out$$Enter t%e details o pla8er $$i$$endl eDi.getFdata') 2 !lrs!r') !out$$-------------------------------------------------------------- $$endl !out$$Pla8er-NametTotal-HunstTotal-at!%est0atting Averagen !out$$------------------------------------------------------ --------$$endl or'i1i$ni==) , eDi.putFdata') 2 !out$$------------------------------------------------------ -------- get!%') 2 /* J. Program in w%i!% Create a !lass to %old inormation o a %us"and and anot%er or t%e wie. 6sing riend un!tion ind t%e total salar8 o t%e amil8. */ #in!lude$iostream.%& #in!lude$!onio.%& !lass wie !lass %us"and , !%ar nameD1< loat sal pu"li!: void read') , !out$$nEnter Name and Salar8 o Kus"and: !in&&name&&sal 2 void print') , !out$$nName:$$name$$nSalar8:$$sal$$endl 2 riend loat total'%us"and(wie) Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
2 !lass wie , !%ar nameD1< loat sal pu"li!: void read') , !out$$nEnter Name and Salar8 o Lie: !in&&name&&sal 2 void print') , !out$$nName:$$name$$nSalar8:$$sal$$endl 2 riend loat total'%us"and(wie) 2 loat total'%us"and %(wie w) , return'%.sal=w.sal) 2 void main') , %us"and % wie w !lrs!r') %.read') w.read') !lrs!r') !out$$nKus"and Metails: %.print') !out$$nLie Metails: w.print') !out$$nTotal ont%l8 In!ome o t%e amil8:$$total'%(w) get!%') 2
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
/* . Program to demonstrate stati! mem"ers */ #in!lude$iostream.%& #in!lude$!onio.%& !lass !ount , private : stati! int num"er int idno pu"li!: void assignidno') , idno==num"er 2 void displa8') , !out$$o"e!t num"er :$$idno$$endl 2 stati! void displa8!ount') , !out$$Num"er o o"e!ts !reated so ar :$$num"er$$endl 2 2 int !ount::num"er void main') , !ount o"1 !lrs!r') !out$$n Program to demonstrate stati! mem"ers nn o"1.assignidno') !ount::displa8!ount') !ount o"4(o"; o"4.assignidno') o";.assignidno') !ount::displa8!ount') o"1.displa8') o"4.displa8') o";.displa8') get!%') 2
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
/* . Program to !reate a data"ase or a "an3 a!!ount !ontains Name( A!!ount no( A!!ount t8pe( 0alan!e( In!luding t%e ollowing a) Constru!tors ") destru!tors !) deault !onstru!tors d) Input and output un!tions. */
#in!lude$iostream.%& #in!lude$!onio.%& #in!lude$string.%& #in!lude$iomanip.%& !lass "an3 , private: !%ar nameD4< int ano !%ar at8peD4 loat "alan!e pu"li!: "an3'),2 "an3'!%ar naD( int no(!%ar t8peD(loat "al) , str!p8'name( na) anono str!p8'at8pe(t8pe) "alan!e"al 2 void readdata') , !out$$endl$$Name : !in&&name !out$$endl$$A!!ount Num"er : !in&&ano !out$$endl$$A!!ount t8pe : !in&&at8pe !out$$endl$$0alan!e : !in&&"alan!e 2 void writedata') , !out$$endl$$setw'1B)$$name$$setw')$$ano$$setw')$$at8pe $$setw'1B)$$"alan!e 2 Q"an3') , 2 2 void main') , int n "an3 !ustDG G(!ust1'As%o3(144<(S0(1<<<<.<<) !lrs!r')
Department of Computer Science NCB
2
III Sem BCA
2
C++ Lab Manual
!out$$Enter num"er o !ustomers: !in&&n or'int i1i$ni==) !ustDi.readdata') !lrs!r') !out.set'ios::let(ios::adustield) !out$$n n Mata"ase or 0an3 A!!ount n n !out$$Name Num"er T8pe 0alan!e n !ust1.writedata') or'i1i$ni==) !ustDi.writedata') get!%')
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
PAHT-0 /* Program to in!rement t%e given date using == operator 'unar8 operator)*/ #in!lude$iostream.%& #in!lude$!onio.%& !lass udate , int da8(mont%(8ear pu"li!: void read') , !in&&da8&&mont%&&8ear 2 void write') , !out$$da8$$/$$mont%$$/$$8ear 2 void operator ==') 2 void udate :: operator ==') , da8== i'da8&;1) , i'mont%14) , da81mont%18ear== 2 else , da81 mont%== 2 2 i''mont%@RRmont%JRRmont%RRmont%11)++'da8&;<)) , da81mont%== 2 i'mont%4) , i'8ear>@< ++ da8&4) , da81 mont%== Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
2 else i '8ear>@< ++ da8 &4) , da81 mont%== 2
2 2 void main') , udate d1 !lrs!r') !out$$Enter t%e date: d1.read') !out$$n T%e given date is: d1.write') ==d1 !out$$endl$$n T%e in!remented date is : d1.write') get!%') 2 /* 1<. program to overload t%e ollowing operators a) 0inar8 operator G=G to !on!atenate 4 strings ") !ompare to strings using operator */ #in!lude$iostream.%& #in!lude$string.%& #in!lude$!onio.%& !lass string , private: !%ar strD< pu"li!:
void getstring') , !in.getline'str(<) 2 void displa8') , !out$$str 2 string operator ='string) int operator 'string)
2 Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
string string::operator ='string ss) , string temp str!p8'temp.str(str) str!at'temp.str(ss.str) return temp 2 int string::operator 'string ss) , return'str!mp'str(ss.str)) 2 void main') , string s1(s4(s; !lrs!r') !out$$Enter t%e irst string : s1.getstring') !out$$endl$$Enter t%e se!ond string : s4.getstring') s;s1=s4 !lrs!r') !out$$endl$$n T%e irst string is: s1.displa8') !out$$endl$$n T%e se!ond string is : s4.displa8') !out$$endl$$n T%e !on!atenation o two string : s;.displa8') i's1s4) !out$$n T%e given two strings are not eual else !out$$n T%e given two strings are eual get!%') 2
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
/* 11.Program in w%i!% Create a "ase !lass or a sta!3 and implement pus% and pop operation. In!lude a derived !lass to !%e!3 or sta!3 !riteria su!% as a) sta!3 empt8 ") sta!3 ull !) sta!3 overlow d) sta!3 underlow. */ #in!lude$iostream.%& #in!lude$!onio.%& #in!lude$stdli".%& #in!lude$stdio.%& #deine ma7 B !lass sta!3 , prote!ted: int stDma7 int top pu"li!: sta!3') , top -1 2
void pus%') , int item !out$$enter t%e item to pus%: !in&&item top== stDtopitem 2 int pop') ,
int item itemstDtop top-- return'item) 2 void displa8') , !out$$n t%e sta!3 !ontains n$$endl or'int itopi&<i--) !out$$stDi$$endl 2 2
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
!lass sta!3F!ond:pu"li! sta!3 , pu"li!: void pus%') , i'topma7-1) !out$$n sta!3 over low$$endl else , sta!3::pus%') i'top ma7-1) !out$$endl$$ ater pus% sta!3 is ull$$endl 2 2 int pop') , int item i'top -1) , !out$$endl$$sta!3 underlow$$endl return'<) 2 else , itemsta!3::pop') i'top -1) !out$$endl$$ater pop sta!3 is empt8$$endl return'item) 2 2 void displa8') , i'top -1) !out$$sta!3 is empt8$$endl else sta!3::displa8') 2 2 void main') , sta!3F!ond s int !%oi!e !out$$n program or sta!3 using in%ertan!e nn do , !lrs!r') !out$$1. pus% n Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
!out$$4. pop n !out$$;. displa8 n !out$$@. e7it n !out$$ n enter 8our !%oi!e: !in&&!%oi!e swit!%'!%oi!e) , !ase 1: s.pus%') s.displa8') "rea3 !ase 4: s.displa8') !%oi!es.pop') i'!%oi!e<) !out$$endl$$t%e items $$!%oi!e$$ is poped rom t%e top o sta!3$$endl "rea3 !ase ;: s.displa8') "rea3 !ase @: e7it'<) 2 !out$$press an8 3e8 to !ontinue.....$$endl get!%') 2 w%ile'!%oi!e@) 2
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
/* 14. Program to illustrate %8"rid in%eritan!e */
#in!lude$iostream.%& #in!lude$!onio.%& !lass student , int regno !%ar nameD4< pu"li!: void input') , !out$$Enter Hegister Num"er + Name:n !in&®no&&name 2 void output') , !out$$nName:$$name !out$$nHegno:$$regno 2 2 !lass test: pu"li! student , prote!ted: int m1(m4(m; pu"li!: void input') , !out$$nEnter ; su"e!t mar3sn !in&&m1&&m4&&m; 2 void output') , !out$$nSu"e!t1 ar3s:$$m1 !out$$nSu"e!t4 ar3s:$$m4 !out$$nSu"e!t; ar3s:$$m; 2 2 !lass sports , prote!ted: int wt pu"li!: void input') , !out$$Enter Sports Leig%tagen !in&&wt 2
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
void output') , !out$$nSports Leig%tage:$$wt 2 2 !lass result: pu"li! test(sports , int total pu"li!: void !altotal') , student::input') test::input') sports::input') totalm1=m4=m;=wt !lrs!r') student::output') test::output') sports::output') !out$$nTotal mar3s:$$total 2 2 void main') , !lrs!r') result r r.!altotal') get!%') 2
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
/* 1;. Program to sort n names using pointer sort */ #in!lude$iostream.%& #in!lude$!onio.%& #in!lude$string.%& !lass student , private: !%ar nameD1B pu"li!: void getdata') , !in&&name 2 void putdata') , !out$$name$$endl 2 !%ar* getname') , return name 2 2 void main') , int n void sort'student**(int) student *stuD1< !lrs!r') !out$$Enter t%e total num"er o students: !in&&n or'int i<i$ni==) , !out$$n Enter t%e name o t%e student:$$i$$endl stuDinew student stuDi-&getdata') 2 !lrs!r') !out$$n 0eore sorting : n !out$$n T%e names are :n or'i<i$ni==) stuDi-&putdata') sort'stu( n) !out$$n Ater sorting:n !out$$n T%e names are:n Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
or'i<i$ni==) stuDi-&putdata') get!%') 2 void sort'student** s(int n) , void swap'student**( student**) or'int i<i$ni==) or'int i=1$n==) swap's=i( s=) 2 void swap'student** s1( student** s4) , i'str!mp''*s1)-&getname')('*s4)-&getname'))&<) , student *temp *s1 *s1 *s4 *s4 temp 2 2
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
/* 1@. Program to demonstrate irtual un!tion.*/ #in!lude$iostream.%& #in!lude$stdio.%& #in!lude$!onio.%& #in!lude$string.%& !lass media , prote!ted: !%ar titleD4< dou"le pri!e pu"li!: media'!%ar *s(dou"le p) , str!p8'title(s) pri!ep 2 virtual void displa8')< 2 !lass "oo3: pu"li! media , int noopages pu"li!: "oo3'!%ar *s(dou"le a(int p):media's(a) , noopagesp 2 void displa8') , !out$$nTitle:$$title !out$$nPages:$$noopages !out$$nPri!e:$$pri!e 2 2 !lass tape: pu"li! media , int pla8time pu"li!: tape'!%ar *s(dou"le a(int pt): media's(a) , pla8timept 2 void displa8') , !out$$nTitle:$$title !out$$nPla8time:$$pla8time$$ inutes !out$$nPri!e:$$pri!e 2 Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
2 void main') , !lrs!r') !%ar tD1<< dou"le pr int pt(nop media *m !out$$Enter Title(NooPages + pri!e o a 0oo3:n gets't) !in&&nop&&pr "oo3 "'t(pr(nop) !out$$Enter Title(Pri!e + Pla8time'in minutes) o a Tape:n gets't) !in&&pr&&pt tape T't(pr(pt) m+" !lrs!r') !out$$n0oo3 Inormation m-&displa8') m+T !out$$nnTape Inormation m-&displa8') get!%') 2
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
/* 1B. Program to !reate a data"ase using !on!epts o iles or a student in!luding t%e ields: Student-name( StudentGs Hegister No( StudentGs Attendan!e 'overall > o attendan!e) and enter data or n students and output t%e same in proper ormat*/ #in!lude$iostream.%& #in!lude$!onio.%& #in!lude$stream.%& #in!lude$iomanip.%& #in!lude$stdio.%& !lass student , pu"li!: int regno !%ar nameD1B loat attendan!e 2
void line'int n) , or'int i<i$ni==) !out$$F 2 void main') , student stu int n ostream inputile inputile.open'stud.dat(ios::out/ios::"inar8) !lrs!r') !out$$Enter num"er o students: !in&&n !out$$Enter t%e student details or $$n$$ students$$endl or'int i1i$ni==) , !lrs!r') lus%'stdin) !out$$He!ord no:$$i$$endl !out$$endl$$Hegno: !in&&stu.regno !out$$endl$$Name: !in&&stu.name !out$$endl$$Attendan!e: !in&&stu.attendan!e inputile.write''!%ar*)+stu(siUeo'stu)) 2 Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
inputile.!lose') istream outputile outputile.open'stud.dat(ios::in/ios::"inar8) !lrs!r') int row B goto78'( row==) line'B<) goto78'(row==) !out$$setw'@<)$$Student Attendan!e Heport$$endl goto78'(row==) !out$$setw')$$Heg.No$$setw'1B)$$Name$$setw'1B)$$Attendan !e goto78'(row==) line'@B) lus%'stdout) w%ile'outputile.read''!%ar*)+stu(siUeo'stu))) , goto78'(row==) !out$$setw'B)$$stu.regno !out$$setw'4<)$$stu.name !out$$setw'1<)$$stu.attendan!e 2 outputile.!lose') goto78'(row==) line'@B) get!%') 2
Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
/* 1J.Program to a!!essing a parti!ular re!ord in an emplo8ee ile */ #in!lude$iostream.%& #in!lude$stream.%& #in!lude$!onio.%& #in!lude$iomanip.%& #in!lude$stdio.%& !lass emplo8ee , private: int empno !%ar nameD1B long int salar8 pu"li!: void getdata') , lus%'stdin) !out$$Enter emplo8ee num"er(name and salar8:n !in&&empno&&name&&salar8 2 void putdata') , !out$$setw'B)$$empno$$setw'1<)$$name$$setw'1<) $$salar8$$endl 2 2 void main') , stream 1 emplo8ee emp !%ar !% int n !lrs!r') 1.open'emp.dat(ios::out) do , emp.getdata') 1.write''!%ar*)+emp(siUeo'emp)) !out$$n Mo 8ou want to !ontinue'8/n) 9 !in&&!% 2 w%ile'!%G8GRR!%GVG) 1.!lose') 1.open'emp.dat(ios::in) !lrs!r') !out$$n Emplo8ee details nn Department of Computer Science NCB
2
III Sem BCA
C++ Lab Manual
!out$$setw'B)$$Empno$$setw'1<)$$Name$$setw'1<)$$Salar8$$ endl w%ile'1.read''!%ar*)+emp(siUeo'emp))) emp.putdata') !out$$n Enter t%e re!ord num"er 8ou want to view: !in&&n 1.!lose') 1.open'emp.dat(ios::in) int p 'n-1)*siUeo'emp) 1.see3g'p) 1.read''!%ar*)+emp(siUeo'emp)) emp.putdata') 1.!lose') get!%') 2
Department of Computer Science NCB
2