Sie sind auf Seite 1von 20

CHAPTER-4

Classes and Objects


SHORT ANSWER QUESTIONS
1. What are the differences between a data type struct and data type class in C++?
Ans. struct class
In C++ struct all members are public by default. Whereas in class all members are private by default.
structures are declared using the keyword struct classes are declared using the keyword class
Example:
st r uct S1
{
i nt num; / / def aul t access
/ / speci f i er i s publ i c
voi d set Num( i nt n)
{
/ / code
}
};
Example:
cl ass C1{
i nt num; / / def aul t access
/ / speci f i er i s pr i vat e
publ i c:
voi d set Num( i nt n)
{
/ / code
}
};
2. Can we use the same function name for a member function of a class and an outside i.e., a non-member
function in the same program file? If yes, how are they distinguished? If no, give reasons. Support your
answer with examples.
Ans. Yes. Object of the class is used to distinguish between the member function of a class and a non-member
function with same name. Ex-
cl ass X{
publ i c:
voi d f ( )
{. . . . . }
};
voi d f ( )
{. . . . . }
voi d mai n( ) {
X x;
x. f ( ) ; / / member f unct i on of t he cl ass x
f ( ) ; / / non- member f unct i on
}
3. When will you make a function inline and why?
Ans. We will make a function inline when the functions are small that called often. Inline functions run a little
faster than the normal functions as the compiler replaces the function call statement with the function code
itself and then compiles the entire code. Thus, with inline functions, the compiler does not have to jump to
another location to execute the function, and then jump back as the code of the called function is already
available to the calling program.
4. Rewrite the following C++ code after removing the syntax error(s) (if any). Underline each correction.
include<iostream.h>
class FLIGHT{
long FlightCode;
char Description[25];
public:
void AddInfo()
{
cin>>FlightCode; gets(Description);
}
void ShowInfo()
{
cout<<FlightCode<<":" <<Description<<endl;
http://cbsecsnip.in
Page 1 of 20

}
};
void main(){
FLIGHT F;
AddInfo.F(); ShowInfo.F();
}
Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<st di o. h>
cl ass FLI GHT{
l ong Fl i ght Code;
char Descr i pt i on[ 25] ;
publ i c:
voi d AddI nf o( )
{
ci n>>Fl i ght Code; get s( Descr i pt i on) ;
}
voi d ShowI nf o( )
{
cout <<Fl i ght Code<<" : "
<<Descr i pt i on<<endl ;
}
};
voi d mai n( ) {
FLI GHT F;
F. AddI nf o( ) ; F. ShowI nf o( ) ;
}
5. Rewrite the following program after removing the syntactical error(s) (if any). Underline each correction.
#include[iostream.h]
#include[stdio.h]
class Employee{
int EmpId=901;
char EName[20];
public:
Employee() {}
void Joining()
{
cin>>EmpId; gets(EName);
}
void List()
{
cout<<EmpId<<":"
<<EName<<endl;
}
}
void main(){
Employee E;
Joining.E();
E.List();
}
Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<st di o. h>
cl ass Empl oyee{
i nt EmpI d;
char EName[ 20] ;
publ i c:
Empl oyee( ) {}
http://cbsecsnip.in
Page 2 of 20

voi d J oi ni ng( )
{
ci n>>EmpI d; get s( EName) ;
}
voi d Li st ( )
{
cout <<EmpI d<<" : "
<<EName<<endl ;
}
};
voi d mai n( ) {
Empl oyee E;
E. J oi ni ng( ) ;
E. Li st ( ) ;
}
6. Identify the error(s) in the following code fragment:

class X{
int a b;
void count(void)
{
a++;
}
public:
int x;
void init(int,int,int);
void print(void);
};
void X::init(int i,int j,int k){
a=i;
b=j;
x=k;
}
void X::print(void){
count();
cout<<"a="<<a;<<"b="
<<b<<"x="<<x<<"\";
}
void func(void);
X Ob1;
int main(){
X Ob2;
Ob1.init(0,1,2);
Ob2.init(2,3,4);
Ob1.print();
Ob2.print();
Ob1.count();
Ob2.count();
}
void func(void)
{
X Ob3;
Ob1.init(4,5,6);
Ob2.init(7,8,9);
Ob3.init(9,10,11);
Ob3.a=Ob3.b=Ob3.x;
http://cbsecsnip.in
Page 3 of 20

Ob1.count();
Ob2.count();
Ob3.count();
Ob1.print();
Ob2.print();
Ob3.print();
}
Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<st di o. h>
cl ass X {
publ i c:
i nt a, b;
voi d count ( voi d)
{
a++;
}
i nt x;
voi d i ni t ( i nt , i nt , i nt ) ;
voi d pr i nt ( voi d) ;
};
voi d X: : i ni t ( i nt i , i nt j , i nt k)
{
a=i ;
b=j ;
x=k;
}
voi d X: : pr i nt ( voi d)
{ count ( ) ;
cout <<" a=" <<a<<" b="
<<b<<" x=" <<x<< ;
}
voi d f unc( voi d) ;
X Ob1;
X Ob2;
i nt mai n( ) {
Ob1. i ni t ( 0, 1, 2) ;
Ob2. i ni t ( 2, 3, 4) ;
Ob1. pr i nt ( ) ;
Ob2. pr i nt ( ) ;
Ob1. count ( ) ;
Ob2. count ( ) ;
}
voi d f unc( voi d)
{
X Ob3;
Ob1. i ni t ( 4, 5, 6) ;
Ob2. i ni t ( 7, 8, 9) ;
Ob3. i ni t ( 9, 10, 11) ;
Ob3. a=Ob3. b=Ob3. x;
Ob1. count ( ) ;
Ob2. count ( ) ;
Ob3. count ( ) ;
Ob1. pr i nt ( ) ;
Ob2. pr i nt ( ) ;
Ob3. pr i nt ( ) ;
}
http://cbsecsnip.in
Page 4 of 20

7. Identify the error(s) in the following code fragment:
int x=5;
int y=3;
class Outer {
public:
int x;
int a;
static int s;
class Inner {
public:
void f(int i)
{
x=i;
s=i;
y=i;
a=i;
}
};
// Inner defination over
Inner I1; //Inner object
void g(it i)
{ x=i;
y=i;
a=i;
s=i;
}
}; //outer definition over
Outer Ob1; // outer object
int main()
{ Ob1.I1.f(3); //statement1
Ob1.g(8); //statement2
return 0;
}
What will be the values of ::x, ::y, Outer::x, Outer::a, Outer::s, Inner::a after statement 1 and statement 2 of
above code?
Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<coni o. h>
i nt x=5;
i nt y=3;
cl ass Out er {
publ i c:
i nt x;
i nt a;
st at i c i nt s;
cl ass I nner
{
publ i c:
i nt a; i nt x;
voi d f ( i nt i )
{
x=i ;
s=i ;
y=i ;
a=i ;
}
};
http://cbsecsnip.in
Page 5 of 20

I nner I 1;
voi d g( i nt i )
{ x=i ;
y=i ;
a=i ;
s=i ;
}
};
i nt Out er : : s;
Out er Ob1;
i nt mai n( )
{ Ob1. I 1. f ( 3) ; / / st at ement 1
Ob1. g( 8) ; / / st at ement 2
r et ur n 0;
}
After statement 1 and statement 2 the values are as following:
::x = 5, ::y = 8, Outer::x = 8, Outer::a = 8, Outer::s =8 , Inner::a = 3
8. Define a class to represent a book in a library. Include the following members:
Data Members
Book Number, Book Name, Author, Publisher, Price, No. of copies issued, No. of copies
Member Functions
(i) To assign initial values
(ii) To issue a book after checking for its availability
(iii) To return a book
(iv) To display book information.
Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<coni o. h>
#i ncl ude<st di o. h>
cl ass Li br ar y
{
i nt BookNo;
char BName[ 25] ;
char Aut hor [ 25] ;
char Publ i sher [ 25] ;
f l oat Pr i ce;
i nt No_of _Copi es;
i nt No_of _Copi es_I ssued;
publ i c:
voi d i ni t i al ( )
{
cout <<endl <<" Ent er Book Number : " ;
ci n>>BookNo;
cout <<endl <<" Ent er Book Name: " ;
get s( BName) ;
cout <<endl <<" Ent er Aut hor Name: " ;
get s( Aut hor ) ;
cout <<endl <<" Ent er Publ i sher Name: " ;
get s( Publ i sher ) ;
cout <<endl <<" Ent er Pr i ce: " ;
ci n>>Pr i ce;
cout <<endl <<" Ent er Number of copi es: " ;
ci n>>No_of _Copi es;
}
voi d i ssue_book( )
{
http://cbsecsnip.in
Page 6 of 20

cout <<" Ent er book det ai l s. . . . . . . " <<endl ;
i ni t i al ( ) ;
i f ( No_of _Copi es>0)
{
cout <<" ent er How many book you want t o i ssue: " ;
ci n>>No_of _Copi es_I ssued;
i f ( No_of _Copi es>=No_of _Copi es_I ssued)
{
No_of _Copi es=No_of _Copi es- No_of _Copi es_I ssued;
cout <<endl <<" " <<No_of _Copi es_I ssued<<" book i s i ssued. . " ;
di spl ay( ) ;
}
el se
{
cout <<Copi es_I ssued<<" books i s not avai l abl e i n st ock. . " ;
}
}
el se
{
cout <<" Book i s not avai l abl e" ;
}
}
voi d r et ur n_book( )
{
cout <<" ent er book det ai l you want t o r et ur n. . . " ;
cout <<endl <<" Ent er Book Number : " ;
ci n>>BookNo;
cout <<endl <<" Ent er Book Name: " ;
get s( BName) ;
No_of _Copi es=No_of _Copi es+No_of _Copi es_I ssued;
cout <<endl <<BookNo<<" : " <<BName<<" Book i s r et ur ned. . . . . . " ;
}
voi d di spl ay( )
{
cout <<" Book Number : " <<BookNo<<endl ;
cout <<" Book Name: " <<BName<<endl ;
cout <<" Aut hor Name: " <<Aut hor <<endl ;
cout <<" publ i sher Name: " <<Publ i sher <<endl ;
cout <<" Pr i ce: " <<Pr i ce<<endl ;
}
};
voi d mai n( )
{
cl r scr ( ) ;
Li br ar y l 1;
i nt ch;
cout <<" 1- >I ssue book. . . " <<endl ;
cout <<" 2- >Ret ur n Book. . . . . " <<endl ;
cout <<" Ent er your choi ce. . . " ;
ci n>>ch;
swi t ch( ch)
{
case 1:
l 1. i ssue_book( ) ;
br eak;
case 2:
http://cbsecsnip.in
Page 7 of 20

l 1. r et ur n_book( ) ;
br eak;
}
get ch( ) ;
}
9. Declare a class to represent fixed-deposit account of 10 customers with the following data members:
Name of the depositor, Account Number, Time Period (1 or 3 or 5 years), Amount.
The class also contains following member functions:
(a) To initialize data members.
(b) For withdrawal of money (after alf of the time period has passed).
(c) To display the data members.
Ans. Same as Question no. 14 in which Withdraw() function is defined for withdraw money.
10. Define a class to represent batsmen in a cricket team. Include the following members:
Data Members:
First name, Last name, Runs made, Number of fours, Number of sixes
Member Functions:
(i) To assign the initial values
(ii) To update runs made (It should simultaneously update fours and sixes, if required).
(iii) To display the batsmans information
Make appropriate assumptions about access labels.
Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<coni o. h>
#i ncl ude<st di o. h>
cl ass Bat sman{
char F_Name[ 30] ;
char L_Name[ 30] ;
i nt Runs_made, f our s, si xes;
publ i c:
voi d i ni t i al ( ) {
cout <<endl <<" Ent er Fi r st Name: " ;
get s( F_Name) ;
cout <<endl <<" Ent er Last Name: " ;
get s( L_Name) ;
cout <<endl <<" Ent er The Runs Made: " ;
ci n>>Runs_made;
cout <<endl <<" Ent er how many f our s: " ;
ci n>>f our s;
cout <<endl <<" Ent er how many si xes: " ;
ci n>>si xes;
}
voi d updat e( ) {
i nt new_r un, new_f our , new_si xes, cal _f our , cal _si x;
cout <<endl <<" Ent er new r uns Made: " ;
ci n>>new_r un;
cout <<endl <<" Ent er new f our s Made: " ;
ci n>>new_f our ;
cout <<endl <<" Ent er new si xes Made: " ;
ci n>>new_si xes;
f our s=f our s+new_f our ;
si xes=si xes+new_si xes;
cal _f our =f our s*4;
cal _si x=si xes*6;
Runs_made=Runs_made+new_r un+cal _f our +cal _si x;
di spl ay( ) ;
cout <<" Tot al Runs Made: " <<Runs_made<<endl ;
http://cbsecsnip.in
Page 8 of 20

cout <<" Number of f our s: " <<f our s<<endl ;
cout <<" Number of si xes: " <<si xes<<endl ;
}
voi d di spl ay( ) {
cout <<" . . . . . Bat sman' s i nf or mat i on. . . . . " <<endl ;
cout <<" Name: " <<F_Name<<" " <<L_Name<<endl ;
}
};
voi d mai n( ) {
cl r scr ( ) ;
Bat sman b1;
b1. i ni t i al ( ) ;
b1. updat e( ) ;
get ch( ) ;
}
11. Define a class to represent bowlers in a cricket team. Include the following members:
Data Members:
First name, Last name, Overs bowled, Number of Maiden overs, Runs given, Wickets taken.
Member Functions:
(i) To assign the initial values, (ii) To update the information, (iii) To display the bowlers information
Make appropriate assumptions about access specifiers.
Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<coni o. h>
#i ncl ude<st di o. h>
cl ass Bowl er s{
char F_Name[ 30] ;
char L_Name[ 30] ;
i nt Over s_bowl ed, Mai den_over s, Runs_gi ven, Wi cket s;
publ i c:
voi d i ni t i al ( ) {
cout <<endl <<" Ent er Fi r st Name: " ;
get s( F_Name) ;
cout <<endl <<" Ent er Last Name: " ;
get s( L_Name) ;
cout <<endl <<" Ent er The Over s bowl ed: " ;
ci n>>Over s_bowl ed;
cout <<endl <<" Ent er how many over s maden: " ;
ci n>>Mai den_over s;
cout <<endl <<" Ent er how many r uns gi ven: " ;
ci n>>Runs_gi ven;
cout <<endl <<" Ent er how many wi cket s t aken: " ;
ci n>>Wi cket s;
}
voi d updat e( ) {
i nt
new_over _bol wed, new_mai den_over s, new_r uns_gi ven, new_wi cket s;
cout <<endl <<" Ent er new over s bowl ed: " ;
ci n>>new_over _bol wed;
cout <<endl <<" Ent er new madden over s: " ;
ci n>>new_maden_over s;
cout <<endl <<" Ent er new r uns gi ven: " ;
ci n>>new_r uns_gi ven;
cout <<endl <<" Ent er new wi cket s t aken: " ;
ci n>>new_wi cket s;
Over s_bowl ed=Over s_bowl ed+new_over _bol wed;
Mai den_over s=Mai den_over s+new_mai den_over s;
http://cbsecsnip.in
Page 9 of 20

Runs_gi ven=Runs_gi ven+new_r uns_gi ven;
Wi cket s=Wi cket s+new_wi cket s;
di spl ay( ) ;
cout <<" Tot al over s bowl ed: " <<Over s_bowl ed<<endl ;
cout <<" Tot al mai dden over s: " <<Mai den_over s<<endl ;
cout <<" Tot al r uns gi ven: " <<Runs_gi ven<<endl ;
cout <<" Tot al wi cket s t aken: " <<Wi cket s<<endl ;
}
voi d di spl ay( ) {
cout <<" . . . . . Bol wer ' s i nf or mat i on. . . . . " <<endl ;
cout <<" Name: " <<F_Name<<" " <<L_Name<<endl ;
}
};
voi d mai n( ) {
cl r scr ( ) ;
Bowl er s b1;
b1. i ni t i al ( ) ;
b1. updat e( ) ;
get ch( ) ;
}
12. Define a class student with the following specifications:
private members of class student
admno integer
sname 20 characters
eng, math, science float
total float
ctotal() A function to calculate
eng + math + science with
float return type
public member functions of class student
Takedata() function to accept values for admno, sname, eng, math, science and ivoke ctotal() to calculate
total.
Showdata() function to display all the data members on the screen.
Ans. cl ass st udent {
pr i vat e:
i nt admno;
char sname[ 20] ;
f l oat eng, mat h, sci ence;
f l oat t ot al ;
f l oat ct ot al ( ) {
r et ur n eng+mat h+sci ence;
}
publ i c:
voi d Takedat a( ) {
cout <<" Ent er admi ssi on number : " ;
ci n>> admno;
cout <<endl <<" Ent er st udent name: " ;
get s( sname) ;
cout << " Ent er mar ks i n engl i sh: " ;
ci n>>eng;
cout << " Ent er mar ks i n mat h: " ;
ci n>>mat h;
cout << " Ent er mar ks i n sci ence: " ;
ci n>>sci ence;
t ot al =ct ot al ( ) ;
http://cbsecsnip.in
Page 10 of 20

}
voi d Showdat a( ) {
cout <<endl <<" . . . . . . . . . . St udent i nf or mat i on. . . . " <<endl ;
cout <<" Admi ssi on number " <<admno;
cout <<" \ nSt udent name " <<sname;
cout <<" \ nEngl i sh " <<eng;
cout <<" \ nMat h " <<mat h;
cout <<" \ nSci ence " <<sci ence;
cout <<" \ nTot al " <<t ot al ;
}
};
i nt mai n( ) {
cl r scr ( ) ;
st udent obj ;
obj . Takedat a( ) ;
obj . Showdat a( ) ;
get ch( ) ;
r et ur n 0;
}
13(a)
.














Considering the following specifications:
Structure name Data Type Size
Name first array of characters 60
mid array of characters 40
last array of characters 60
Phone area array of characters 4
exch array of characters 4
numb array of characters 6

Class name Data Type
P_rec name Name
phone Phone
with member functions constructors and display_rec.
(i) Declare structures in C++ for Name and Phone.
(ii) Declare a class for P_rec.
(iii) Define the constructor (outside the class P_rec) that gathers information from the user for the above
two structures Name and Phone.
(iv) Define the display_rec (outside the class P_rec) that shows the current values.
Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
st r uct Name
{
char f i r st [ 40] ;
char mi d[ 40] ;
char l ast [ 60] ;
};
st r uct Phone
{
char ar ea[ 4] ;
char exch[ 4] ;
char numb[ 6] ;
};
cl ass P_r ec
{
http://cbsecsnip.in
Page 11 of 20

Name name;
Phone phone;
p_r ec( ) ;
voi d di spl ay_r ec( ) ;
};
P_r ec( )
{
f i r st =" abc" ;
mi d=" aaa" ;
l ast =" j j j " ;
ar ea=1234;
exch=7546;
numb=789456;
}
voi d di spl ay_r ec( )
{
cout <<f i r st <<mi d<<l ast <<ar ea<<exch<<numb;
}
voi d mai n( )
{
cl r scr ( ) ;
P_r ec p;
p. di spl ay_r ec( ) ;
get ch( ) ;
}
13(b)
.
Consider the following class declaration and answer the questions below:
class SmallObj
{
private:
int some,more;
void err_1() {cout<<"error";}
public:
void Xdata(int d) {some=d;more=d++; }
void Ydata() {cout<<some<<" "<<more; }
};

(i) Write the name that specifies the above class.
(ii) Write the data of the class with their access scope.
(iii) Write all member functions of the class along with their access scope.
(iv) Indicate the member function of the SmallObj that sets data.
Ans. (i) SmallObj
(ii) private int some, more;
(iii) private void err_1(){cout<<"error";}
public void Xdata(int d) {some=d;more=d++; }
public void Ydata() {cout<<some<<" "<<more; }
(iv) public void Xdata(int d) {some=d;more=d++; }
14. Declare a class to represent bank account of 10 customers with the following data members.
Name of the depositor, Account number, Type of account (S for Savings and C for Current), Balance amount.
The class also contains member functions to do the following:
(i) To initialize data members
(ii) To deposit money
(iii) To withdraw money after checking the balance (minimum balance in Rs. 1000)
(iv) To display the data members
Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<coni o. h>
http://cbsecsnip.in
Page 12 of 20

#i ncl ude<st di o. h>
cl ass Account
{
char D_Name[ 30] ;
f l oat Amount ;
char acc_t ype[ 2] ;
publ i c:
l ong Acc_No;
voi d i ni t i al ( )
{
cout <<endl <<" Ent er Deposi t er s Name: " ;
get s( D_Name) ;
cout <<endl <<" Ent er Account Number : " ;
ci n>>Acc_No;
cout <<endl <<" Ent er Type of account ( S f or Savi ng and C f or
Cur r ent ) : " ;
get s( acc_t ype) ;
cout <<endl <<" Ent er Ammount : " ;
ci n>>Amount ;
}
voi d Deposi t ( )
{
f l oat di p;
cout <<" Ent er Money t o deposi t : " ;
ci n>>di p;
di spl ay( ) ;
Amount =Amount +di p;
cout <<" Af t er deposi t t ot al amount i s: " <<Amount ;
}
voi d Wi t hdr aw( )
{
f l oat wi d;
cout <<endl <<" Ent r e money t o wi t hdr aw: " ;
ci n>>wi d;
i f ( Amount >=1000)
{
di spl ay( ) ;
Amount =Amount - wi d;
cout <<" Af t er wi t hdr aw t he amount i s: " <<Amount ;
}
el se
{
cout <<" . . . . you can not wi t hdr aw money. . . . . " ;
}
}
voi d di spl ay( )
{
cout <<" Deposi t er s Name: " <<D_Name<<endl ;
cout <<" Account Number : " <<Acc_No<<endl ;
cout <<" Account Type: " <<acc_t ype<<endl ;
cout <<" Amount : " <<Amount <<endl ;
}
l ong get accno( )
{
r et ur n Acc_No;
}
http://cbsecsnip.in
Page 13 of 20

};
voi d mai n( )
{
cl r scr ( ) ;
Account A1[ 10] ;
l ong a;
i nt i , f l ag=0;
i nt ch;
f or ( i =0; i <10; i ++)
{
cout <<endl <<" Ent er i nf or mat i on f or Deposi t er " <<i +1<<" : " <<endl ;
A1[ i ] . i ni t i al ( ) ;
}
f or ( i =0; i <10; i ++)
{
cout <<endl <<" Deposi t er - " <<i +1<<" : " <<endl ;
A1[ i ] . di spl ay( ) ;
}
cout <<" **************************************" <<endl ;
cout <<" 1- >deposi t . . . " <<endl ;
cout <<" 2- >wi t hdr aw. . " <<endl ;
cout <<" Ent er your choi ce: " ;
ci n>>ch;
swi t ch( ch)
{
case 1:
cout <<endl <<" Ent er account number f or whi ch di posi t
money: " ;
ci n>>a;
f or ( i =0; i <10; i ++)
{
i f ( A1[ i ] . get accno( ) ==a)
{
f l ag=1;
br eak;
}
el se
{
f l ag=0;
}
}
i f ( f l ag==0)
{
cout <<" Account number not f ound. . . . . " ;
}
el se
{
A1[ i ] . Deposi t ( ) ;
}
br eak;
case 2:
cout <<endl <<" Ent er account number f or whi ch wi t hdr aw
money: " ;
ci n>>a;
f or ( i =0; i <10; i ++)
{
http://cbsecsnip.in
Page 14 of 20

i f ( A1[ i ] . get accno( ) ==a)
{
f l ag=1;
br eak;
}
el se
{
f l ag=0;
}
}
i f ( f l ag==0)
{
cout <<" Account number not f ound. . . . . " ;
}
el se
{
A1[ i ] . Wi t hdr aw( ) ;
}
br eak;
}
get ch( ) ;
}
15. Define a class worker with the following specification:
Private members of class worker
wname 25 characters
hrwrk float (hors worked and
wagerate per hour)
totwage float(hrwrk*wgrate)
calcwg A fuction to find hrerk*
wgrate with float return type

Public members of class worker
in_data() a function to accept values for
wno, wname, hrwrk, wgrate
and invoke calcwg() to
calculate totwage.

out_data() a function to display all the
data members on the screen
you should give definations of
functions.
Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>

cl ass wor ker
{
i nt wno;
char wname[ 25] ;
f l oat hewr k, wgr at e;
f l oat t ot wage;
f l oat cal cwg( )
{

t ot wage = hewr k*wgr at e;
http://cbsecsnip.in
Page 15 of 20

r et ur n t ot wage;
}

publ i c:
voi d i n_dat a( ) ;
voi d out _dat a( ) ;
};

voi d wor ker : : i n_dat a( )
{
cout <<" Ent er wor ker number : " ;
ci n>>wno;
cout <<" ent er wor ker name: " ;
get s( wname) ;
cout <<" Ent er hour s wor ked: " ;
ci n>>hewr k;
cout <<" Ent er wage r at e per hour : " ;
ci n>>wgr at e;
cal cwg( ) ;
}



voi d wor ker : : out _dat a( )
{
cout <<" . . . . . . Wor ker I nf or mat i on. . . . . . . . " <<endl ;
cout <<" Wor ker number : " <<wno<<endl ;
cout <<" Wor ker name: " <<wname<<endl ;
cout <<" Hour s wor ked: " << hewr k<<endl ;
cout <<" Wage r at e per hour : " << wgr at e<<endl ;
cout <<" Tot al wage: " <<t ot wage<<endl ;
}

i nt mai n( )
{
wor ker obj ;
obj . i n_dat a( ) ;
obj . out _dat a( ) ;

get ch( ) ;
r et ur n 0;
}

16. Define a class Teacher with the following specification:
private members:
name 20 characters
subject 10 characters
Basic,DA,HRA float
salary float
Calculate() function computes the salary and returns it.
Salary is sum of Basic, DA and HRA

public members:
Readdata() function accepts the data values and invoke the calculate function
Displaydata() function prints the data on the screen.
http://cbsecsnip.in
Page 16 of 20

Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>

cl ass Teacher
{
char name[ 20] ;
char subj ect [ 10] ;
f l oat Basi c, DA, HRA;
f l oat sal ar y;
f l oat Cal cul at e( )
{

sal ar y=Basi c+DA+HRA;
r et ur n sal ar y;
}

publ i c:
voi d Readdat a( ) ;
voi d Di spl aydat a( ) ;
};

voi d Teacher : : Readdat a( )
{
cout <<endl <<" Ent er name: " ;
get s( name) ;
cout <<" Ent er subj ect : " ;
get s( subj ect ) ;
cout <<" Ent er Basi c : " ;
ci n>>Basi c;
cout <<" Ent er DA : " ;
ci n>>DA;
cout <<" Ent er HRA : " ;
ci n>>HRA;
Cal cul at e( ) ;
}



voi d Teacher : : Di spl aydat a( )
{
cout <<" . . . . . . Teacher Det ai l s. . . . . . . . " <<endl ;
cout <<" Name: " <<name<<endl ;
cout <<" Subj ect : " <<subj ect <<endl ;
cout <<" Basi c: " <<Basi c<<endl ;
cout <<" DA: " <<DA<<endl ;
cout <<" HRA: " <<HRA<<endl ;
cout <<" Sal ar y: " <<sal ar y<<endl ;
}

i nt mai n( )
{
Teacher obj ;
obj . Readdat a( ) ;
obj . Di spl aydat a( ) ;

http://cbsecsnip.in
Page 17 of 20

get ch( ) ;
r et ur n 0;
}

17. Define a class Student with the following specification:
private members:
roll_no integer
name 20 characters
class 8 characters
marks[5] integer
percentage float
Calculate() function that calculates overall percentage of marks and returns the percentage of marks.

public members:
Readmarks() a function that reads marks and invokes the calculate functio
Displaymarks() a function that prints the marks.
Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
cl ass St udent {
i nt r ol l _no;
char name[ 20] ;
char Cl ass[ 8] ;
i nt mar ks[ 5] ;
f l oat per cent age;
f l oat Cal cul at e( ) {

per cent age = ( mar ks[ 0] +mar ks[ 1] +mar ks[ 2] +mar ks[ 3] +mar ks[ 4] ) / 5;
r et ur n per cent age;
}
publ i c:
voi d Readmar ks( ) ;
voi d Di spl aymar ks( ) ;
};
voi d St udent : : Readmar ks( ) {
cout <<endl <<"Ent er r ol l number : " ;
ci n>>r ol l _no;
cout <<endl <<"Ent er name: " ;
get s( name) ;
cout <<"Ent er mar ks i n " ;
f or ( i nt i =0; i <5; i ++)
{
cout <<endl <<"Subj ect "<<i +1<<": ";

ci n>>mar ks[ i ] ;
};
Cal cul at e( ) ;
}
voi d St udent : : Di spl aymar ks( ) {
cout <<". . . . . . St udent Mar ksheet . . . . . . . . ";
cout <<endl <<"Rol l number : "<<r ol l _no<<endl ;
cout <<" Name: " <<name<<endl ;
cout <<" Mar ks i n subj ect - 1: "<< mar ks[ 0] <<endl ;
http://cbsecsnip.in
Page 18 of 20

cout <<" Mar ks i n subj ect - 2: "<< mar ks[ 1] <<endl ;
cout <<" Mar ks i n subj ect - 3: "<<mar ks[ 2] <<endl ;
cout <<" Mar ks i n subj ect - 4: "<<mar ks[ 3] <<endl ;
cout <<" Mar ks i n subj ect - 5: "<< mar ks[ 4] <<endl ;
cout <<" Per cent age: "<<per cent age<<endl ;
}
i nt mai n( ) {
St udent obj ;
obj . Readmar ks( ) ;
obj . Di spl aymar ks( ) ;

get ch( ) ;
r et ur n 0;
}

18. Write a short note on memory allocation of class and its objects.
Ans. When a class is defined, memory is allocated for its member functions and they are stored in the memory.
When an object is created, separate memory space is allocated for its data members. All objects work with
the one copy of member function shared by all.

19. Write a program that invokes a function newdate() to return a object of date type. The function newdate()
takes two parameters: an object olddate of date type and number of days (int) to calculate the newdate as
olddate + number of days and returns the newdate.
Ans. #i ncl ude<i ost r eam. h>
#i ncl ude<coni o. h>
#i ncl ude<st di o. h>

st at i c i nt days_i n_mont h[ ] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31 };
i nt day, mont h, year ;

unsi gned shor t day_count er ;

i nt i s_l eap( i nt y) {
r et ur n ( ( y %4 == 0 && y %100 ! = 0) | | y %400 == 0) ;
}

cl ass dat e{
publ i c:
/ / i nt d, m, y;

voi d ol ddat e( i nt d, i nt m, i nt y) ;
voi d next _day( ) ;
voi d newdat e( dat e set _dat e, i nt days) ;
};

http://cbsecsnip.in
Page 19 of 20

voi d dat e: : ol ddat e( i nt d, i nt m, i nt y) {
m< 1 ? m= 1 : 0;
m> 12 ? m= 12 : 0;
d < 1 ? d = 1 : 0;
d > days_i n_mont h[ m] ? d = days_i n_mont h[ m] : 0;
i f ( i s_l eap( y) ) {
days_i n_mont h[ 2] = 29;
}
el se {
days_i n_mont h[ 2] = 28;
}
day = d;
mont h = m;
year = y;
}
voi d dat e: : next _day( ) {
day += 1; day_count er ++;
i f ( day > days_i n_mont h[ mont h] ) {
day = 1;
mont h += 1;
i f ( mont h > 12) {
mont h = 1;
year += 1;
i f ( i s_l eap( year ) ) {
days_i n_mont h[ 2] = 29;
} el se {
days_i n_mont h[ 2] = 28;
}
}
}
}

voi d dat e: : newdat e( dat e ol ddat e, i nt x) {
i nt i ;
f or ( i =0; i <x; i ++) next _day( ) ;
}
i nt mai n( ) {
cl r scr ( ) ;
dat e d1;
d1. ol ddat e( 22, 2, 1980) ;
d1. newdat e( d1, 62) ;
day_count er = 0;
cout <<" day: " <<day<<" mont h: " <<mont h<<" year : " <<year ;
get ch( ) ;
}
20. What are static members of a class? When and how are they useful?
Ans. A class can have static data members as well as static member functions.
The static data members are the class variables that are common for all the objects of the class. Only one
copy of static data members is maintained which is shared by all the objects of the class. They are visible only
within the class but their lifetime is the entire program. They can be accessed by all the member functions.
A member function that accesses only static data members of a class is static member functions. It cannot
access other data members but static members.
The static data members are useful when some data values are to be shared across objects of the same class.
LONG QUESTION ANSWERS
http://cbsecsnip.in
Page 20 of 20

Das könnte Ihnen auch gefallen