Semester: II Hours/week: 2 Code: 08UCS2403:P Credits: 2
Semester: II Hours/week: 2 Code: 08UCS2403:P Credits: 2
Semester: II Hours/week: 2 Code: 08UCS2403:P Credits: 2
Hours/week : 2 Credits : 2
2. a) Define a class to represent a bank account. Include the following members: Data members : Name of the depositor !ccount number "#pe of account $alance amount in the account %ember functions : "o assign initial values "o deposit an amount "o withdraw an amount after checking the balance "o displa# the name and balance. &rite a main program to invoke the member functions. b) 'onsider a shopping list of items for which orders are placed with a dealer. "he list should include the code number and price of each item. (perations such as adding an item to the list deleting an item from the list and printing the total value of the order are to be provided for. &rite a program to implement the above using a class with arra#s as data members. ). a) Program to find the largest of three numbers using inline function. b) Program to find mean of *N+ numbers using friend function. ,. a) Program to find volume of cube c#linder and rectangular bo- using function overloading. b) Program to add two times in hours and minutes format using ob.ects as function arguments. /. Program to illustrate the use of arra#s of ob.ects. 0. Program to add two comple- numbers using a) overloaded constructors b) operator overloading 1. Program to check whether the given string is a palindrome or not using pointer method. 2. Program to read the derived class data members such as name roll number seheight and weight from the ke#board and displa# the contents of a class on the screen. &rite a program to demonstrate a single inheritance.
Pro.r)m: 8include9iostream.h: 8include9conio.h: class fact ; int num< public: void set5int)< int factorial5 )< void print5 )< =< void fact :: set 5int -) ; num > -< = void fact :: print5 )
; cout99?@n "he given number is?:99num< cout99? "he factorial?99factorial5)< = int fact :: factorial5) ; int f>1< for5int i>2< i9>num<i33) f>fAi< return f< = void main5 ) ; clrscr5)< cout99?@n?< cout99?@n@t 4I%PBC PD(7D!% E4IN7 'B!44C4 !ND ($FC'"4@n?< cout99?@n@t AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA@n?< cout99?@n?< int n< fact a< cout99?@n Cnter the value of n:?< cin:: n< cout99n< a.set5n)< a.print5 )< getch5)< = ut2ut: 4I%PBC PD(7D!% E4IN7 'B!44C4 !ND ($FC'"4 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Cnter the value of n : / "he given number is / "he factorial is 126 Desult: "hus the factorial of a given number using class is calculated.
*)+tori), wit1out +,)ss "im : "o write a c33 program to calculate factorial of a number without using class. ",.orit1m: 4tep 1: Define function unsigned long fact5). 4tep 2 : In main get the *n+ value. 4tep ) : 'all the function fact5) 4tep , : Print the factorial value. 4tep / : 4top Pro.r)m: 8include9iostream.h: 8include9conio.h: unsigned long fact 5 int n) ; int f>1< for5int ->2< -9>n< -33) fA>-< return f< = void main5) ; int n< clrscr5)< cout99?@t@t?99?AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA?99?@n?< cout99?@t@t?99?G!'"(DI!B &I"H(E" 'B!44C4?99?@n?< cout99?@t@t?99?AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA?99@n?< cout99?@n Cnter n value:?< cin ::n< cout99n< cout99?@n "he given number is >?99n99?@n?< cout99?@n "he factorial is >?99fact5n)< getch5)< = (utput: AAAAAA G!'"(DI!B &I"H(E" 'B!44C4 Cnter n value : , "he given number is > , "he factorial is > 2, Resu,t : "hus the factorial of a given number is calculated without using class.
"im "o write a c33 program to convert dollars to rupees. ",.orit1m: 4tep 1: start 4tep 2 : Define a function dollar to rupee 5int dol) to convert dollar to rupee value. 4tep ): In main enter dollar value. 4tep ,: call the function dollar to rupee. 4tep / : print the rupee value. 4tep 0: 4top Pro.r)m : 8include 9iostream.h: 8include9 conio.h: float con5int float)< void main5 ) ; int dol< float rate< clrscr5)< cout99?@n@t@t PD(7D!% G(D '(NICD" D(BB!D "( DEPCC4?99?@n?< cout99?@n@t@t AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA?99?@n?< cout99?@n Cnter the current dollar rate:?< cin::rate< cout99rate< cout99?@n "he current rate is :?99rate99?@n?< cout99?@n Cnter the dollar to convert:?< cin::dol< cout99dol< cout99?@n "he degree value of dollar is :?99con5dol rate)99?@n?< getch5)< = float con5int dol float rate) ; float temp< temp > dol A rate< return temp< =
ut2ut Program for convert dollar to rupees Cnter the current dollar rate:,/ "he current rate is : ,/ Cnter the dollar to convert: 0/ "he degree value of dollar is: 2J2/ Resu,t: "hus the dollar is converted into rupees using this program.
Pro.r)m: 8include9iostream.h: 8include9conio.h: class account ; int accno< char nameK26L< float amount< char acct#peK26L< public: void getdata5void)< void putdata5void)< void deposit5int)< void withdraw5int)< =< 1
void account::getdata5void) ; clrscr5 )< cout99?@n@t Cnter account no:?< cin::accno< cout99accno< cout99?@n@t@t Cnter name:?< cin::name< cout99name< cout99?@n@t Cnter Deposit:?< cin::amount< cout99amount< cout99?@n@t Cnter !ccount t#pe: M< cin::acct#pe< cout99acct#pe< = void account::putdata5void) ;cout99?@n@t !ccount details?< cout99?@n@t AAAAAAAAAAAAAA?< cout99?@n@t !ccount no:?99accno< cout99?@n@t Name:?99name< cout99?@n@t $alance:?99amount< = void account::deposit5int -) ; amount>amount3-< = void account::withdraw5int -) ; if5-:amount) ; cout99?@n@t sorr# #ou can+t withdraw?< = else amount> amount N -< = void main5 ) ; account cus< int ch< float amt< do ; clrscr5 )< cout99?@n@t@t $!NO !''(EN"?< cout99?@n@t@t@t AAAAAAAAAAAAAAAA?< cout99?@n@t@t 1. (pen account< cout99?@n@t@t 2. Deposit?< cout99?@n@t@t ). &ithdraw?< 2
cout99?@n@t@t ,. Displa#?< cout99?@n@t@t /. C-it?< cout99?@n@t@t Cnter #our choice:?< cin::ch< cout99ch< switch5ch) ; case 1: cus.getdata5)< break< case 2: cout99?@n@t Cnter the amount:?< cin::amt< cout99amt< cus.deposit5amt)< cus.putdata5)< break< case ): cout99?@n@t Cnter the amount to withdraw:?< cin ::amt< cout99amt< cus.withdraw5amt)< cus.putdata5 )< break< case ,: cus.putdata5 )< break< = getch5)< =while5ch:6PPch9/)< = ut2ut: $!NO !''(EN" 1. open account 2. Deposit ). &ithdraw ,. Displa# /. C-it Cnter #our choice : 1 Cnter account no : 1661 Cnter name: sa.una Cnter Deposit : /6666 Cnter !ccount t#pe : saving $!NO !''(EN" 1. open account 2. Deposit
). &ithdraw ,. Displa# /. C-it Cnter #our choice : 2 Cnter the amount : 16666 !ccount details AAAAAAAAAAAA !ccount no :1661 Name : sa.una $alance : 06666 $!NO !''(EN" 1. open account 2. Deposit ). &ithdraw ,. Displa# /. C-it Cnter #our choice : ) Cnter the amount to withdraw: /666 !ccount details AAAAAAAAAAAA !ccount no :1661 Name : sa.una $alance : //666 $!NO !''(EN" 1. open account 2. Deposit ). &ithdraw ,. Displa# /. C-it Cnter #our choice : , !ccount details AAAAAAAAAAAA !ccount no :1661 Name : sa.una $alance : //666 $!NO !''(EN" 1. (pen account 2. Deposit ). &ithdraw ,. Displa# /. C-it Cnter #our choice : /
Resu,t: 16
"hus a c33 program to represent a bank account using classes and ob.ects is implemented.
2( b) S1o22i0. ,ist
"im: "o develop a c33 program to implement the concept of classes and ob.ects.. ",.orit1m: 4tep 1: start 4tep 2: get the initial value for item code name price. 4tep ): process the function. 4tep ,: displa# the result. 4tep /: stop. S1o22i0. ,ist 2ro.r)m 8include9iostream.h: 8include9conio.h: 8include9iomanip.h: class item ; int codeK16L< char nameK16L K26L< int price K16L< int count< public: void 'N"5void)< void getitem5void)< void removeitem5void)< void displist5void)< =< void item:: 'N"5void) ; count>6< = void item::getitem5void) ; cout99?@n Cnter the item code:?< cin::codeKcountL< cout99codeKcountL< cout99?@n@t Cnter the item name:?< cin::nameKcountL< cout99nameKcountL< cout99?@n@t Cnter the price:?<
11
cin::priceKcountL< cout99priceKcountL< count33< = void item::removeitem5void) ; int a< cout99?@n@t@t@t Cnter the item code:?< cin::a< cout99a< for5int i>6<i9count<i33) if5codeKiL>>a) ; codeKiL > 6< priceKiL > 6< = = void item:: displist5void) ; clrscr5)< cout99?@n@t@t item @t name @t price @n?< cout99?@n@t@t AAAA @t AAAA @t AAAA @n?< for5int i>6< i9count< i33) ; if5priceKiLQ>6) ; cout99?@t@t?99codeKiL99?@t?99nameKiL99?@t?99setw51)99priceKiL< cout99?@n@n?< = = float sum>6< for5i>6<i9count<i33) sum>sum3priceKiL< cout99?@t@t "otal value@t?99setw51)99sum< = void main5) ; item order< order.'N"5)< int ch< do ; clrscr5 )< cout99?@n@t@t 4H(PPIN7 BI4" ?< cout99?@n@t@t@t AAAAAAAAAAAAAAAA?< cout99?@n@t@t 1. 'reate list?< cout99?@n@t@t 2. !dd item?< cout99?@n@t@t ). Demove item?< cout99?@n@t@t ,. Displa# item?< 12
cout99?@n@t@t /. C-it?< cout99?@n@t@t Cnter #our choice:?< cin::ch< cout99ch< switch5ch) ; case 1: int n< cout99?@n@t Cnter the no. of items?< cin::n< cout99n< for5int i>6<i9n<i33) order.getitem5)< break< case 2: order.getitem5 )< break< case ): order.removeitem5 )< break< case ,: order.displist5 )< break< = getch5)< =while5ch:6PPch9/)< = ut2ut 4H(PPIN7 BI4" 1. create list 2. add item ). remove item ,. displa# item /. e-it Cnter #our choice:1 Cnter the item code : 1666 Cnter the item name : dove Cnter the price :,6 Cnter the item code: 1661 Cnter the item name : luCnter the price: /6 4H(PPIN7 BI4" 1. create list 2. add item ). remove item ,. displa# item /. e-it
1)
Cnter #our choice:2 Cnter the item code : 1662 Cnter the item name: pears Cnter the price : )/ 4H(PPIN7 BI4" 1. create list 2. add item ). remove item ,. displa# item /. e-it Cnter #our choice : , Item name AAAA AAAAA 1666 dove 1661 lu1662 pears "otal value 4H(PPIN7 BI4" 1. create list 2. add item ). remove item ,. displa# item /. e-it Cnter #our choice : / Resu,t:
"hus a c33 program for the classes and ob.ects have been successfull# implemented.
1,
45( $o: 3()) %"R!4S6 !im: "o write a '33 program to find the largest of three numbers using inline function. !lgorithm: 4tep 1: 4tep 2: 4tep ): 4tep ,: 4tep /: 4tart Define big 5- # S) function in a single line as line function. In main program get the value of a b c. 'all the function big 5a b c) to print the largest numbers. 4top. * 6HR44 $U#&4RS USI$! I$%I$4 *U$C6I $:
Program: 8include9iostream.h: 8include9conio.h: inline big5int - int # int S) ; return55-:#PP-:S)T-:5#:S)T#:S)< = void main5) ; clrscr5)< int a b c< cout99R$I77C4" (G "HDCC NE%$CD4 E4IN7 INBINC GEN'"I(N @n@nR< cout99R@n@t Cnter the value for a @nR< cin::a< cout99a< cout99R@n@t Cnter the value for b @nR< cin::b< cout99b<
cout99R@n@t Cnter the value for c @nR< cin::c< cout99c< cout99R@n@n@t "he biggest number is:R99big5a b c)<
1/
getch5)< = Input: Cnter the value for a ) ) Cnter the value for b , , Cnter the value for c / / (utput: "he biggest number is: / Desult: "hus the largest of three numbers is printed using the inline function.
10
45( $o: 3(b) #4"$ !im: "o write a '33 program to find the mean of *n+ numbers using friend function. !lgorithm: 4tep 1: 4tart. 4tep 2: "he value 5) function is used to get the *n+ numbers and the sum is calculated. 4tep ): "he float mean5) function is outside the scope of the class sample. 4tep ,: $ut it is declared as friend in sample class. 4tep /: 'alculate sum>sum3aK.L. 4tep 0: Deturn float5s.sumUs.n) to find the mean value. 4tep 1: 4top. 'lass Diagram: 4ample int aKL float sum getvalue5) mean5sample s) Program: 8include9iostream.h: 8include9conio.h: class sample ; int n aK16L< float sum<
11
public: void getvalue5void)< friend float mean5sample s)< =< void sample::getvalue5) ; cout99R@n GDICND GEN'"I(N @n@nR< cout99RCnter the value for n:@nR< cin::n< cout99n< sum>6.6< for5int .>1<.9>n<.33) ; cout99R@n Cnter the R99.99Rnumber:R< cin::aK.L< sum>sum3aK.L< = = float mean5sample s) ; return float5s.sumUs.n)< = int main5) ; clrscr5)< sample s1< s1.getvalue5)< cout99R@n@n "he mean value is:R99mean5s1)< getch5)< return 6< = Input: Cnter the value for n: ) ) Cnter the 1number:, Cnter the 2number:/ Cnter the )number:1 (utput: "he mean value is: /.)))))) Desult: "hus the mean of *n+ numbers is calculated using friend function.
12
94R% "3I$!:
1J
cout99R@n Cnter the value of height:R< cin::b< cout99b< cout99R@n volume of c#linder:R99volume5- b)99R@nR< cout99R@n volume of rectangle bo- @nR< cout99R@n Cnter the value of length:R< cin::#< cout99#< cout99R@n Cnter the value of breadth:R< cin::b< cout99b< cout99R@n Cnter the value of width:R< cin::c< cout99c< cout99R@n@n volume>R99volume5# b c)< getch5)< = int volume5int s) ; return5sAsAs)< = double volume5double r int h) ; return5).1,ArArAh)< = long volume5long l int b int h) ; return5lAbAh)< = Input: Cnter the value of side: 2 Cnter the radius: ) Cnter the value of height: / Cnter the value of length: 0 Cnter the value of breadth: , Cnter the value of width: ) (utput: Iolume of cube>2 Iolume of c#linder: 1,1.) Iolume>12
Desult: "hus volume of cube c#linder rectangular bo- using function overloading concept has been calculated successfull#.
26
45( $o: 4(b) *U$C6I $ "R!U#4$6S: !im: "o write a '33 program to find the total number of hours and minutes using passing ob.ects to functions. !lgorithm: 4tep 1: 4tart 4tep 2: !ssign hours as h and minutes as m. 4tep ): !dd the total number of hours and minutes b# using the formula hours > minU06< min > minW06< hours > hours3t1.hours3t2.hours. 4tep ,: Displa# the result. 4tep /: 4top. 'lass Diagram: time int hours minutes gettime5int h int m) puttime5) sum5time time) Program: 8include9iostream.h: 8include9conio.h: class time ; int hours< int minutes< public:
21
void gettime5int h int m) ; hours>h< minutes>m< = void puttime5void) ; cout99hours99Rhours andR< cout99minutes99RminutesR99R@nR< = void sum5time time)< =< void time::sum5time t1 time t2) ; minutes>t1.minutes3t2.minutes< hours>minutesU06< minutes>minutesW06< hours>hours3t1.hours3t2.hours< = void main5) ; clrscr5)< time t1 t2 t)< t1.gettime52 ,/)< t2.gettime5) )6)< t).sum5t1 t2)< cout99Rt1>R< t1.puttime5)< cout99Rt2>R< t2.puttime5)< cout99Rt)>R< t).puttime5)< getch5)< = (utput: t1>2hours and ,/minutes t2>)hours and )6minutes t)>0hours and 1/minutes Desult: "hus the total number of hours and minutes are calculated using passing ob.ects to functions.
22
:) "I#:
"RR";
&<4C6S
"o develop a c33 program to illustrate the use of arra# of ob.ects. "%! RI6H#: 4"CP 1: 4tart 4"CP 2: Define the entities to represent the emplo#ee details as arra# 4"CP ): !ccept the input values from the user 4"CP ,: Displa# the proceed emplo#ee details. 4"CP /: 4top PR !R"#: 8include9iostream.h: 8include9conio.h: class emp ; char nameK26L< int age< public: void get5void)< void put5void)< =< void emp::get5void) ; cout99R@tCnter nameR99R@n@t@tR< cin::name< cout99R@tCnter ageR99R@n@t@tR< cin::age< = void emp::put5void) ; cout99RName:R99name99R@nR< cout99R!ge:R99age99R@nR< = const int siSe>)< void main5) ; clrscr5)< emp managerKsiSeL< for5int i>6<i9siSe<i33) ; cout99R@n Details of managerR99i3199R@nR< 2)
managerKiL.get5)< = cout99R@nR< for5i>6<i9siSe<i33) ; cout99R@n %anagerR99i3199R@nR< managerKiL.put5)< getch5)< = = U6PU6: Details of manager1 Cnter name asi Cnter age 2, Details of manager2 Cnter name sifa Cnter age ,/ Details of manager) Cnter name rifa Cnter age )6 %anager1 Name:asi !ge:2, %anager2 Name:sifa !ge:,/ %anager) Name:rifa !ge:)6 R4SU%6: "hus the e-ecution of emplo#ee details using arra#s of ob.ect is implemented.
2,
0.a ) "I#:
"o develop a c33 program using constructor overloading to add two complenumbers. "%! RI6H#: 4"CP 1: 4tart 4"CP 2: 7et the floating point numbers as real and imaginar# part. 4"CP ): !dd the two real part numbers and two imaginar# part numbers 4"CP ,: Displa# the added result 4"CP /: 4top PR !R"#: 8include9iostream.h: 8include9conio.h: class comple; float - #< public: comple-5);= comple-5float a) ; ->#>a< = comple-5float real float image) ; ->real< #>image< = friend comple- sum5comple- comple-)< friend void show5comple-)< =< comple- sum5comple- c1 comple- c2) ; comple- c)< c).->c1.-3c2.-< c).#>c1.#3c2.#< return5c))< = void show5comple- c) 2/
; cout99c.-99R3.R99c.#99R@nR< = int main5) ; clrscr5)< comple- a5)., 2./)< comple- b51.0)< comple- c< c>sum5a b)< cout99R@n @t a>R< show5a)< cout99R@n @t b>R< show5b)< cout99R@n @t c>R< show5c)< getch5)< return 6< = U6PU6: 0a. !dd two comple- nos.XX constructor overloading a>).,3.2./ b>1.03.1.0 c>/3.,.1 R4SU%6: Hence the two given comple- numbers are added using constructor overloading.
20
0.b) "I#:
P4R"6 R
94R% "3I$!
"o develop a c33 program using operator overloading to add two comple- numbers. "%! RI6H#: 4"CP 1: 4tart 4"CP 2: 7et the floating point numbers as real and imaginar# part. 4"CP ): !dd the two real part numbers and two imaginar# part numbers 4"CP ,: Displa# the added result 4"CP /: 4top PR !R"#: 8include9iostream.h: 8include9conio.h: class comple; float - #< public: comple-5);= comple-5float real float image) ; ->real< #>image< = comple- operator35comple-)< void displa#5void)< =< comple- comple-::operator35comple- c) ; comple- temp< temp.->-3c.-< temp.#>#3c.#< return5temp)< = void comple-::displa#5void) ; cout99-99R3.R99#99R@nR< = int main5) ; 21
clrscr5)< cout99R@t@t(perator (verloading@nR< comple- c1 c2 c)< c1>comple-52./ )./)< c2>comple-52.1 1.0)< c)>c13c2< cout99R@n@t c1>R< c1.displa#5)< cout99R@n@t c2>R< c2.displa#5)< cout99R@n@t c)>R< c).displa#5)< getch5)< return 6< = U6PU6: (perator (verloading c1>2./3.)./ c2>2.13.1.0 c)>/.23./.1 R4SU%6: Hence the two given comple- numbers are added using operator overloading.
22
= ) P"%I$3R #4 "I#: "o develop a c33 program to check whether in Palindrome or not using pointers. "%! RI6H#: 4tep 1 : 4tart. 4tep 2:7et the iUp string. 4tep ):Gind the length of the string using strlen5) functions. 4tep ,:'heck whether the string is palindrome or not palindrome 4tep /:Displa# the result 4tep 0:4top PR !R"#
#include<iostream.h> #include<conio.h> #include<string.h> void main() { char a[25],*s,*p clrscr() cout<<!"n"t"t #$%&'()*+, cout<<!"n"t"t ********** cout<<!"n ,nter the -ord. cin>>a int n/strlen(a),0/1 p/2a[n34] 5or (s/2a[1] s<2a[(n64)72] { i5 (*s//*p) { 0/4 p33 8 else { 0/1 0rea9 8 8 i5 (0) cout<< !"n :iven -ord is else cout<< !"n :iven -ord is getch() 8
"n! "n! !
s66)
U6PU6:
R4SU%6: "hus the program to find whether the given string is palindrome using pointers has been successfull# created. 2J
8 ) I$H4RI6"$C4 "I#: "o develop a c33 program read the deleivered class data members much as name roll no height and weight from ke#board and displa# the contents n screen using inheritance. "%! RI6H# 4tep 1:4tart 4tep 2:7et the values of name age and se-. 4tep ):'reate a class that inheris properties of outside e-isted cass 4tep ,:In the inherited class get the values of height P weight 4tep /:Print the result 4tep 0:4top PR !R"#
#include<iostream.h> #include<conio.h> class student { protected. char se;,name[<1] int rollno,-eight 5loat height pu0lic. void getdata(void) { cout<<!"n ,nter the 'ame. ! cin>>name cout<<! ,nter the )ollno. ! cin>>rollno cout<<! ,nter the =e;( + 7 >). ! cin>>se; cout<<! ,nter the ?eight. ! cin>>height cout<<! ,nter the @eight. ! cin>>-eight 8 8 class stud . pu0lic student { pu0lic. void putdata(void) { cout<<!"n 'ame. !<<name<<!"n! cout<<!"n )ollno. !<<rollno<<!"n! cout<<!"n =e;(+7>). !<<se;<<!"n! cout<<!"n ?eight. !<<height<<!"n! cout<<!"n @eight. !<<-eight<<!"n! 8 8 void main() {
)6
stud s clrscr() cout<<!"n"t"t =&':%, &'?,)&A$'B, "n! cout<<!"n"t"t ****************** "n! cout<<!"n ,nter the =tudent (etails. "n! s.getdata() cout<<endl cout<<! Ahe (etails o5 the =tudent are. "n! s.putdata() getch() 8 =ample &nput and *utput. =&':%, &'?,)&A$'B, ****************** ,nter the =tudent (etails. ,nter ,nter ,nter ,nter ,nter the the the the the 'ame. ?usna rollno. <C5D =e;(+ 7 >). 5 ?eight. 5.5 -eight. DC
Ahe (etails o5 the =tudent are. 'ame. ?usna )ollno. <C5D =e;(+ 7 >). 5 ?,&:?A. 5.5 @,&:?A. DC
DC4EB": Hence the iUp details has been successfull# displa#ed using single inheritance.
)1