Assignment XII CS - CONSTRUCTORS & DESTRUCTORS
1. Differentiate between Constructor and Destructor function with respect to Object Oriented Programming.
2. List some of the special properties of the constructor functions.
3. What is a parameterized constructor? How is it useful?
4. What is a copy constructor? What ¡s its significance? Which situation is it invoked in? Support your answer with C++ code.
5. Differentiate between a default constructor and copy constructor, giving suitable examples of each.
6. Describe the importance of destructor. List some of the special properties of destructor.
7. Which of the following is used to identify the copy constructor class type X?
(i) (X&) (ii) X(&X) (iii) X(X&) (iv) X(X)
Justify your answer.
8. Answer the questions (i) and (ii) after going through the following C++ class:
class Stream
{
int StreamCode ; char Streamname[20];float fees;
public:
Stream( ) //Function 1
{
StreamCode=1; strcpy (Streamname,"DELHI");
fees=1000;
}
void display(float C) //Function 2
{
cout<<StreamCode<<":"<<Streamname<<":"<<fees<<endl;
}
~Stream( ) //Function 3
{
cout<<"End of Stream Object"<<endl;
}
Stream (int SC,char S[ ],float F) ; //Function 4
};
i) In Object Oriented Programming, what are Function 1 and Function 4 combined together referred as? Write the definition of function 4.
ii) What is the difference between the following statements?
Stream S(11,”Science”,8700);
Stream S=Stream(11,”Science”,8700);
9. Define a class Customer with the following specifications.
Private Members :
Customer_no integer
Customer_name char (20)
Qty integer
Price, TotalPrice, Discount, Netprice float
Public members:
a). A constructor to assign initial values of Customer_no as 111,Customer_name as “Leena”, Quantity as 0 and Price, Discount and Netprice as 0.
b). Input( ) – to read data members(Customer_no, Customer_name, Quantity and Price) call Caldiscount().
c). Caldiscount ( ) – To calculate Discount according to TotalPrice and NetPrice
TotalPrice = Price*Qty
TotalPrice >=50000 – Discount 25% of TotalPrice
TotalPrice >=25000 and TotalPrice <50000 - Discount 15% of TotalPrice
TotalPrice <250000 - Discount 10% of TotalPrice
Netprice= TotalPrice-Discount
d). Show( ) – to display Customer details.
10. Answer the questions (i) and (ii),after going through the following class:
class test
{ char paper[20];
int marks;
public:
test() //function1
{ strcpy(paper,”computer”);
marks=0; }
test (char p[]) //function 2
{ strcpy(paper,p);
marks=0;}
test(int M) //function 3
{strcpy(paper,”computer”);
marks=M;}
test (char p[],int M) //function 4
{ strcpy(paper, P);
marks=M;}
};
(i) Which feature of object oriented programming is demonstrated using function1, function2, function3 and function4 in the above class test?
(ii) Write statements in C++ that would execute function 4 in the above class test?
11. Answer the questions (i) and (ii), after going through the following class:
class readbook
{
public:
Readbook() //function-1
{cout<<”open the book”<<endl;}
void readchapter() //function-2
{cout<<”reading chapter-1”<<endl;}
~readbook() //function-3
{cout<<”close the book”}
};
(i) In object oriented programming, what is function-1 referred as and when does it get invoked?
(ii) In object oriented programming, what is function -3 referred as and when does it get invoked?
12. Find the syntax errors if any, and write the error free code and also underline the corrections made.
class abc
{
int x=10;
Float y;
abc()
{
Y=5;
}
~(){ }
};
int main()
{
abc a1,a2;
return 0;
}
NOTE: You can take printout of questions and paste them in your copy.
Last Date of Submission of Assignment: 10/Aug/2015