Assignment-2
Constructors and Destructors
Chapter-5
1)What is a copy constructor? Give an example in C++ to illustrate copy
constructor.
2)Answer the questions i)and ii) after going through the following class
class work
{
int workid;
char worktype;
public:
~work() // Function1
{cout<<“un-allocated”<<endl;}
void status() //
Function2
{
cout<<workid<<endl;
}
work() // Function 3
{
workid=10;worktype=‘t’;}
work(work &w) // Function4
{
workid=w.workid+12;
worktype=w.worktype+1;
}
};
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?
ii)
Work w; àstatement1
Work y(w);àstatement2
Which member function out of function1,function2,function3 and function4
shown in above definition of class work will be called an execution of
statement written as statement 1 and statement2? What are those functions
specifically known as?
3)
Given the following C++
code, answer the questions
class test
{ int time;
public:
test( ) //function
1
{
time=0;
cout<<”Bye”;
}
~ test( ) //function 2
{cout<<”hello”;
}
void
exam( ) //function
3
{
cout<<”GOD
BLESS YOU”;
}
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
4) Answer the questions (i) and (ii) after going
through the following program:
#include <iostream.h>
#include<string.h>
class bazaar
{ char Type[20] ;
char product [20];
int qty ;
float price ;
bazaar()
//function 1
{ strcpy (type , “Electronic”) ;
strcpy (product , “calculator”);
qty=10;
price=225;
}
public :
void Disp() //function 2
{ cout<< type <<”-”<<product<<”:” <<qty<< “@” << price << endl ;
}
};
void main ()
{ Bazaar B ; //statement 1
B. disp() ; //statement 2
}
#include<string.h>
class bazaar
{ char Type[20] ;
char product [20];
int qty ;
float price ;
bazaar()
//function 1
{ strcpy (type , “Electronic”) ;
strcpy (product , “calculator”);
qty=10;
price=225;
}
public :
void Disp() //function 2
{ cout<< type <<”-”<<product<<”:” <<qty<< “@” << price << endl ;
}
};
void main ()
{ Bazaar 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 shall be the possible output when the program gets executed?
(Assuming, if required _ the suggested correction(s) are made in the program).