Important questions in Constructor Destructor,Computer Science

advertisement
Question answers on constructor
and destructor
and class and object
Pournomi Sen
PGT,Comp Sc
K.V Ballygunge
2009
1)What is copy constructor?Give an
example in C++ to illustrate copy
constructor. (2)
2)Answer the questions i)and ii) after
going through the following class
Qs:i)Which member function out of
function1,function2,function3 and
function4 shown in above definition of
class work is called
automatically,when the scope of the
object gets over?Is it known as
constructor OR destructor OR
overloaded function OR copy
constructor?
Class work
{
Int workId;
Char workType;
ii)Work w; statement1
Public:
Work y(w);statement2
~work() -function1
{cout<<“un-allocated”<<endl;}
i)Which member function out of
Void status()Function2
function1,function2,function3 and
{ cout<<workId<<:<<eorkType<<endl;}
function4 shown in above definition of
class work will be called an execution
work() function 3
of statement written as statement 1
{ workId=10;workType=‘T’;}
and statement2?What are those
work(work &W)function4
functions specifically known as?
{ workId=w.workId+12;
workType=w.workType+1;
}
};
Define a class Resort in C++ with
the following description:Private members:Rnodata member to store room
no
NameData member to store
customer name
ChargesData member to store
per day charges
DaysData member to store
number of days to stay
Compute()A function to calculate
and return Amount as
days*charges and if the value of
Days *charges is more than
11000 then as
1.02*Days*charges
Public members:Getinfo()A function to enter the
content Rno,Name,charges,and
Days
Dispinfo()A function to display
Rno,Name,charges ,days and
amount(amount to be displayed
by calling function Compute)
2008
• Qs:-
b)Answer the questions i) to ii) after going
through the following program:#include<iostream.h>
#include<string.h>
Class Bazar
{char Type[20]; char product[20];
int Qty;float price;
Bazar()
{ strcpy(Type,”Electronic”);
Strcpy(product,”Calculator”);
Qty=10;price=225;}
Public:
Void disp()
{ cout<<Type<<“”<<product<<“:”<<Qty<<“@”<<price<<e
ndl;
}
};
Void main()
{
Bazar B;statement 1
B.Disp();statement 2
}
•
i)Will statement 1 initialize all the
data members for object B with the
values given in the function 1?(Yes
Or No).Justify your answer
suggesting the correction(s) to be
made in the above code.
•
Ii)What will be the possible output
when the program gets
executed?(Assuming if required,the
suggested corrections are made in
the program)
2.a)Differentiate between public and private
visibility modes in context of Object
Oriented Programming using a suitable
example illustrating each. (2)
c)Define a class Garments in C++ with
the following descriptions:Private members:GCode  of type string
GType  of type string
GSize of type integer
GFabricof type string
GPriceof type float
A function Assign() which calculates
and assigns the value of GPrice as
follows:For the value of GFabric “COTTON”,
Gtype
GPrice(Rs)
Trouser
1300
Shirt
1100
For GFabric other than “COTTON” the
above mentioned GPrice gets
reduced by 10%
Public members:-A constructor to
assign initial values to
GCode,GType and GFabric with the
word “NOT ALLOTTED” and GSize
and GPrice with 0.
A function Input() to input the values of
the data members
GCode,GType,GSize and GFabric
and invoke he assign function.
A function Display() which displays the
content of all the data members for
a Garment.
•
b) Answer the questions(i) and (ii)
after going through the following
program :
2
•
•
class number
•
{ float M;
•
char str[25];
•
public:
•
number( )
//constructor 1
•
{ M=0;
str=’\0’;
}
•
•
•
number(number &t);
•
•
•
//constructor 2
};
•
i) Write c++ statement such that it
invokes constructor 1.
•
ii) Complete the definition for
constructor 2.
•
c) Define a class SalesCounter with
folloeing specifications:
4
Data members:
Net_amt, Amount –> Real values
ClothType - string( 30), Pay_mode string
(30)
Member functions
Constructor – to initialize the Amount as 0,
ClothType as “Cotton”, Pay_mode as
“Cash”, Net_amt as 0
Calc_net () – to calculate discount and
net_amt . The Company offers discount
scheme on each purchase to the
customer. The scheme is as follows
• For Cotton
•
Purchase above 5000 and avail
discount of 10%
•
Purchase above 7500 and avail
discount of 15%
• For Silk
•
Purchase above 5000 and avail
discount of 5%
•
Purchase above 7500 and avail
discount of 10%
–
• For Synthetic
•
•
•
•
– Purchase above 5000 and avail
discount of 15%
Purchase above 7500 and avail
discount
of 25%
Now if the customer is paying by Cash
an additional 2% discount will be given
to them.
If by Cheque no discount will be given,
if payment mode is credit card 2.5% tax
has to be paid by the customer on the
total purchase.
– Purchase () – The Salesman will
enter the detail of the purchasing
made by the customer and will also
enter the payment
mode(CASH/CHEQUE/CREDIT
CARD ) , and will invoke the
Calc_net() to calculate the net
amount
– Show() – The function will generate
the bill to the customer along with
the purchase details and the amount
to be paid
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Given the following C++ code, answer
the questions
(2)
Class TEST
{ int time;
public:
TEST( )
//Function 1
{
time=0;
cout<<”hai”;
}
~ TEST( ) //Function 2
{cout<<”hello”;
}
void exam( )
//Function 3
{
cout<<”god bless u”;
}
TEST(int Duration) //Function 4
{
time=Duration;
cout<<”Exam starts”;
}
•
•
•
•
•
•
TEST(TEST &T) //Function 5
{
time = T.Duration;
cout<<”Exam Finished”
}
};
•
In Object Oriented Programming,
what is Function 1 referred as and
when does it get invoked/called?
•
In Object Oriented Programming,
what is Function 2 referred as and
when does it get invoked/called?
•
Which category of constructor
Function 5 belongs to and what is
the purpose of using it?
•
Write statements that would call the
member Function 1 and 4
Download