CHAPTER-4 Classes and Objects LONG ANSWER QUESTIONS SHORT QUESTION ANSWERS Write a program to handle 10 account holders. The program should use the class as defined in question 9 of TYPE B. 1. Make necessary changes in the class c lass definition – if required. Ans. Same as Question no. 14 of Short Answer Question. Define a class Applicant in C++ with the following description: 2. Private Members: A data member ANo (Admission Number) of type long A data member Name of type string A data member Agg (Aggregate Marks) of t ype float A data member Grade of type char A member function GradeMe() to find the Grade as per the Aggregate Marks obtained by a student. Equivalent Aggregate Marks range and the respective Grades are shown as follows: Aggregate Marks Grade >=80 A less than 80 and >=65 B less than 65 ad >=50 C less than 50 D Public Members: A function ENTER() to allow user to enter values for Ao, Name, Agg and call function GradeMe() to find the Grade. A fuction RESULT() to allow user to view the content of all the data members. ude
Ans. #i ncl ude<
#i ncl ude< ude #i ncl ude cl ass Ap Appl i can cant { l ong ANO; char NAME[ 25] 25] ; f l oat Agg; char char Gr ade; ade; voi voi d Gr adeM adeMe( ) { i f ( Agg> gg>=80) 80) Gr ade ade=' A' ; el se i f ( Agg>=65) Gr ade ade=' B' ; el se i f ( Agg>=50) Gr ade ade=' C' ; el se i f ( Agg>=33) Gr ade= ade=' D' ; el se Gr ade ade=' E' ; } publ i c : voi voi d ENTER TER( ) { cout cout <<" Ent er Adm Admi ss i on No: " ; c i n>>ANO ANO; cout cout <<" Ent er Name of of t he Ap Appl i cant cant : " ; get get s( NAME) ; cout cout <<" Ent er Aggr ggr egar egar e Mar ks obt obt ai ned ned by t he Candi andi dat e : " ;
http://cbsecsnip.in
Page 1 of 10
ci n>>Agg; Gr adeMe( ) ; } voi d RESULT( ) { cout <<" *** *** *Appl i cat Det ai l s** *** *** *** " <
Write a class to represent a vector (1-D numeric array). Include member functions: for vector creation for modification of a given element for displaying the largest value in the vector. for displaying the entire vector Write a program using this class.
Ans.
#i ncl ude #i ncl ude #i ncl ude i nt const si ze=50; cl ass vect or { f l oat d[ si z e] ; i nt s ; publ i c : voi d creat e( voi d) ; voi d modi f y( voi d) ; voi d l ar gest ( voi d) ; voi d di spl ay( voi d) ; }; voi d vect or : : creat e( voi d) { cout <<" \ n\ nEnt er of Ar r ay you want t o cr eat e: - " ; ci n>>s; cout <<" Ent er " <>d[ i ] ; } voi d vect or : : modi f y( voi d) { i nt mf y_val ue; f l oat wi t h; cout <<"\ nEnt er Locat i on of ar r ay at whi ch val ue i s t o be modi f i ed: - " ; ci n>>mf y_val ue;
http://cbsecsnip.in
Page 2 of 10
cout <<" Ent er Val ue wi t h whi ch you want t o Repl ace: - " ; ci n>>wi t h; d[ mf y_val ue] =wi t h; } voi d vector : : l ar gest ( voi d) { i nt l ar g_no=d[ 0] ; f or ( i nt i =0; i l ar g_ no) l ar g_no=d[ i ] ; } cout <<" The l ar gest number of vect or i s: " <>choi ce; swi t ch( choi ce) { case 1: o1. creat e( ) ; br eak; case 2: o1. modi f y( ) ; br eak; case 3: o1. l ar gest ( ) ; br eak; case 4: o1. di spl ay( ) ; br eak; case 5: got o end; }
http://cbsecsnip.in
Page 3 of 10
}whi l e( 1) ; end: get ch( ) ; } 4.
Ans.
Write a C++ program to implement a circle class. Each object of this class will represent a circle, storing its radius and the x and y coordinate of its center as floats. Iclude two access functions for: i. calculating area of circle. i. calculating the circumference of circle.
#i ncl ude #i ncl ude #i ncl ude #def i ne pi =3. 14; cl as s ci r c l e { publ i c: f l oat x, y, ar ea, ci r ; voi d cal ar ea( f l oat r adi us) { ar ea=3. 14*r adi us*r adi us; cout <>x; cout <<" Ent er y- cor di nat e : " ; ci n>>y; cout <<" Ent er r adi us : " ; ci n>>r adi us; cout <
5.
Imagine a ticket selling booth at a fair. People passing by are requested to purchase a ticket. A ticket is priced as Rs. 2.50/-. The booth keeps track of the number of people that have visited the booth, and of the total amount of money collected. Model this ticket selling booth with a class called ticbooth including following members: Data Members: number of people visited total amount of money collected.
http://cbsecsnip.in
Page 4 of 10
Member functions To assign initial values (assign 0 to both data members) To increment only people total in case ticket is not sold out To increment people total as well as amount total if a ticket is sold out To display the two totals To display the number of tickets sold out (a ticky one) Include a program to test this class.
Ans.
#i ncl ude #i ncl ude #i ncl ude #i ncl ude cl ass t i cboot h { i nt Peopl eVi st ed; f l oat Tot al Amount ; publ i c: t i cboot h( ) { Peopl eVi st ed = 0; Tot al Amount = 0. 0; } voi d I ncr ement Peopl eCount er ( ) { Peopl eVi st ed++; cout <
http://cbsecsnip.in
Page 5 of 10
cout <<" Ent er your choi ce: " ; ci n>>choi ce; swi t ch( choi ce) { case 1: cout <<"i s t i cket sol d?( Y/ N) : "; ci n>>ch; i f ( ch==' N' ) { t 1. I ncr ement Peopl eCount er ( ) ; } el se { t 1. I ncr ement Peopl eCount er ( ) ; t 1. I ncr e_peopl e_amount ( ) ; } got o l 1; br eak; case 2: t 1. ShowDet ai l s( ) ; got o l 1; br eak; case 3: t 1. Tot al Ti cket sSol d( ) ; got o l 1; br eak; case 4: exi t ( 0) ; br eak; } r et ur n 0; get ch( ) ; } 6.
Ans.
Write a C++ program to perform various operations on a string class without using l anguage supported built-in string functions. The operations on a class are: a) Read a string b) Display the string c) Reverse the string d) Copy the string into an empty string e) Concatenate two strings
#i ncl ude #i ncl ude #i ncl ude #i ncl ude #i ncl ude c l as s st r i ng { char st r 1[ 20] ; char st r 2[ 20] ; publ i c: voi d r ead( ) { cout <<" \ nEnt er st r i ng : => " ; ci n>>st r 1; } voi d Di spl ay( ) { cout <
http://cbsecsnip.in
Page 6 of 10
voi d r ever se( ) ; voi d copy( ) ; voi d concat enat e( ) ; }; voi d s t r i ng: : r e ver s e( ) { i nt i , l , j =0; cout <<" \ nEnt er st r i ng : => " ; ci n>>st r 1; l =s t r l en( s t r 1) ; f or ( i =l - 1; i >=0; i - - ) { st r 2[ j ] =st r 1[ i ] ; j ++; } st r 2[ j ] =' \ 0' ; cout <<" Rever se i s: " < " ; ci n>>st r 1; f or ( i =0; i <=st r l en( st r 1) ; i ++) { st r 2[ i ] =st r 1[ i ] ; } st r 2[ i ] =' \ 0' ; cout <<" Copy i s: " < " ; ci n>>st r 1; cout <<" \ nEnt er s t r i ng2 : => " ; ci n>>st r 2; l =s t r l en( s t r 1) ; f or ( i =0; i <=l ; i ++) { st r 1[ l +i ] =s t r 2[ i ] ; } cout <<" Copy i s: " <
http://cbsecsnip.in
Page 7 of 10
cout <<" 6. Exi t " <>choi ce; swi t ch( choi ce) { case 1: s1. r ead( ) ; got o l 1; br eak; case 2: s1. Di spl ay( ) ; got o l 1; br eak; case 3: s1. r ever se( ) ; got o l 1; br eak; case 4: s1. copy( ) ; got o l 1; br eak; case 5: s1. concat enat e( ) ; got o l 1; br eak; case 6: exi t ( 0) ; br eak; } r et ur n 0; get ch( ) ; } 7.
Define a class named ADMISSION in C++ with the following description: Private Members: AD_NO integer (Ranges 10 - 2000) NAME Array of characters (String) CLASS Character FEES Float Public Members: Function Read_Data() to read an object of ADMISSION type. Function Display() to display the details of an object. Function Draw-Nos() to choose 2 students randomly. And display the details. Use random function to generate admission nos. to match with AD_NO.
Ans.
#i #i #i #i cl {
ncl ude ncl ude ncl ude ncl ude ass ADMI SSI ON pr i vat e: i nt AD_NO; char NAME[ 50] ; char CLASS[ 5] ; f l oat FEES; publ i c : voi d Read_Dat a( ) ; voi d Di spl ay( ) ; voi d Dr aw_Nos( ) ;
}; voi d ADMI SSI ON: : Read_Dat a( )
http://cbsecsnip.in
Page 8 of 10
{ cout <<" Ent er Admi ssi on No ( 10 - 2000) : " ; ci n>>AD_NO; cout <<" Name: " ; get s( NAME) ; cout <<" Cl ass: " ; get s( CLASS) ; cout <<" Fees: " ; ci n>>FEES; } voi d ADMI SSI ON: : Di spl ay( ) { cout <<" Admi ssi on No: " <
Define a class named HOUSING in C++ with the following description: Private Members: REG_NO integer (Ranges 10 - 2000) NAME Array of characters (String) TYPE Character COST Float Public Members: Function Read_Data() to read an object of HOUSING type. Function Display() to display the details of an object. Function Draw-Nos() to choose and display the details of 2 houses selected randomly from an array of 10 objects of type HOUSING. Use random function to generate the registration nos. to match with REG_NO from the array.
Ans.
#i ncl ude #i ncl ude
http://cbsecsnip.in
Page 9 of 10
#i ncl ude #i ncl ude cl ass HOUSI NG{ pr i vat e: i nt REG_NO; char NAME[ 50] ; char TYPE[ 20] ; f l oat COST; publ i c : voi d Read_Dat a( ) ; voi d Di spl ay( ) ; voi d Dr aw_Nos( ) ; }; voi d HOUSI NG: : Read_Dat a( ) { cout <<" Ent er Reg No ( 10 - 1000) : " ; ci n>>REG_NO; cout <<" Name: " ; get s( NAME) ; cout <<" Type: " ; get s( TYPE) ; cout <<" Cost : " ; ci n>>COST; } voi d HOUSI NG: : Di spl ay( ) { cout <<" Reg No: " <
http://cbsecsnip.in
Page 10 of 10