CONSTRUCTOR & DESTRUCTOR EXERCISE CONTINUED

       CONSTRUCTOR & DESTRUCTOR   LONG                                  THEORY
Q11.
Consider the following code:
class ci   
{     int l;
public:    
      ci(int j) { l=j; }
      ci(ci &rv) { l=rv.l; }
};    void initialize() { l=0; }
           
main()     
{     ci original(1);
      ci X1(original);
}     ci X2=original;
           
Referring to the sample code above, what initializes the object X1?
i.             Initialize() function
ii.            The default constructor
iii.           The copy constructor
iv.           The default copy constructor
Justify your answer.
Ans
The default constructor initializes the object X1 as constructor is invoked as soon as the object is created.
Q12.
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.                      
Ans
X(X&) is used to identify the copy constructor class type X as copy constructor is a constructor of the form classname (classame &).
Q13.
In the case of copy constructor, which of the following is true?
(i) Used to instantiate an object from another existing object.
(ii) To copy one object to another existing object.
(iii) Can be a substitute for a “=” operator for class objects.
(iv) All of the above.                     
Ans
iv) All of the above.
Q14
What do you think is the advantage of declaring the constructor and destructor functions for public member access?
(i) It allows the constructor and destructor access to the data members.
(ii) It means that the constructor and destructor can be called directly by code in main() functions.
(iii) It allows the constructor access to the other member functions.
(iv) None of the above.
Ans
(ii) It means that the constructor and destructor can be called directly by code in main() functions.
Q15
Answer the question (i) and (ii) after going through the following class:
class WORK             
{     int WorkId; char WorkType;
      public:           //Function 1
      ~WORK()          
      { cout<<"Un-Allocated"<
      void status()     //Function 2
      { cout<
      WORK()            //Function 3
      { WorkId=10; WorkType='T'; }
      Work(WORK &W)     //Function 4
      {     WorkId=W.WorkId+12;
      }     WorkType=W.WorkType+1;
};                                                          
                                                               
(i) Which member function out of Function 1, Function 2, Function 3 and Function 4 shown in the above definition
of class WORK is called automatically, when the scope of a object gets over? Is it known as Constructor OR
Destructor OR Overloaded Function OR Copy Constructor?
(ii) WORK W;                     //Statement 1  
                WORK Y(W);      //Statement 2  
Which member function out of Function 1, Function 2, Function 3 and Function 4 shown in the above definition of
class WORK will be called an execution of statement written as statement 2? What is this function specifically
known as out of Destructor or Copy Constructor or Default Constructor?

Ans
(i) Destructor                     (ii) Copy Constructor
Q16
Identify the error(s) in the following code and correct the code, explaining every change being introduced:
#include
class code                          
{              int no;  
                char branch;
                static int count;
                code (int i=0,char b);
                public: 
                code(code A)
                {              no=A.no;
                }              branch=A.branch;
                                               
                ~code()               
                {              cout<<"Destroying Object"<<--count n="" o:p="">

};             }                             
                                               
code(int i,char b)
{              no=i;                    
}              branch=b;          
                                               
int main()                           
{              code X,Y;            
                :                              
}              return 0;             

Ans
Class code
{
int no; char branch;
static int count;
public:
code(int i=0,char b);
code(code &A)
{no=A.no;
branch=A.branch;
}
~code()
{
cout<<"Destroying Object"<<--count n="" p="">
}
};

int code::count=0;
code::code(int i,char b)
{no=i;
branch=b;
}
int main()
{ code X,Y;
 return 0;
}

Changes being introduced are as following:
i.              Constructor definition should be public so that it can be accessed outside the class.
ii.             There should be a use of ‘&’ operator in copy constructor.
iii.            There should be a definition of the static variable outside the class definition.
iv.           There is a invalid use of ‘:’ expression.
Q17
Identify the error(s) in the following code and correct the code, explaining every change being introduced in the
program:            
#include
class Sample
{              int i;
                float j;
                void Sample(void)
                {              i=0;
                }              j=0.0;
                               
                init()
                {              cin>>i>>j;
                }             
                display()
                {              cout<<"i="<

                }              cout<<"j="<
                               
};             void Sample(void){}
                               
Sample s1,s2;

Ans
#include
class Sample
{              int i;
                float j;
public:
Sample()
{              i=0;                        
}              j=0.0;    
                                               
void init()           
{              cin>>i>>j;           
}                                             
void display()    
{              cout<<"i="<
}              cout<<"j="<
};                                                            
void main()
{                             
Sample s1,s2;   
s1.init();                              
s1.display();      
}                                             
Changes being introduced are as following:
Constructor definition should be public so that it can be accessed outside the class.
Constructor should not have return type.
init() and display() method should have return type.
Object should be created in main() method and methods are called with the help of that object.
Q18
Answer the question (i) and (ii) after going through the following program:
#include 
#include        
class Retail                        
{              char Category[20];          
                char Item[20];  
                int Qty;                
                float Price;         
                Retail() //Fuction 1
                {              strcpy(Category,"Cereal");
                               
                                strcpy(Item,"Rice");
                                Qty=100;             
                }              Price=25;            
                                                               
                public:                  //Function 2
                void Show()      
                {              cout<

};             }                                             
                                                               
void main()                       
{              Retail R;               //Statement 1
                               
}              R.Show();            //Statement 2
                                                               
(i) Will Statement 1 initialize all the data members for object R 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)           

Ans
(i)                  No. Since the default constructor Retail() is declared inside private section, it cannot initialize the objects declared outside the class. Correction needed are:
The constructor Retail() should be declared inside public section.
(ii) Cereal-Rice:100@25                                        
Q19
Answer the question (i) and (ii) after going through the following class:
class Maths       
{              char Chapter[20];
                int Marks;          
                public:  //Member Function 1
                Maths()               
                {              strcpy(Chapter,"Geometry");
                                Marks=10;          
                }              cout<<"Chapter Initialized";
                ~Maths()   //Member Function 2
                {              cout<<"Chapter Over";
};             }                             
                                               
(i) Name the specific features of class show by Member Function 1 and Member Function 2 in the above example.
(ii) How would Member Function 1 and Member Function 2 gets executed?                     
Ans
(i) Function 1 is Constructor. Function 2 is Destructor.
(ii) Function 1 (the Constructor) will get executed every time an object of class Maths gets created.
Function 2 (the Destructor) will get executed every time an object of class Maths goes out of scope i.e., its scope gets over.
Q20
Answer the question (i) and (ii) after going through the following class:
class Interview                                
{              int Month;                                         
                public:                                  //Constructor 1
                Interview(int y) {  Month=y; }   
};             Interview(Interview &t);                             //Constructor 2
                                                                               
(i) Create an object, such that it invokes constructor 1. 
(ii) Write complete definition for Constructor 2.             
               
Ans
(i) Interview obj1(3);                                     
(ii) Interview(Interview &t)                        
{              Month=t.Month;             }             
Q21
Answer the question (i) and (ii) after going through the following class:
class Exam                                         
{              int Rno,MaxMarks,MinMarks,Marks;    
                public:                  //Module 1
                Exam()                 
                {              Rno=101; MaxMarks=100;          
                }              MinMarks=40;Marks=75;                            
                                                                //Module 2
                Exam(int Prno,int Pmarks)         
                {              Rno=Prno; MaxMarks=100;        
                }              MinMarks=40;Marks=Pmarks;  
                                                                //Module 3
                ~Exam()                              
                {              cout<<"Exam Over"<

                }                                              //Module 4
                void Show()                      
                {              cout<
                                cout<<"[MarksGot]"<
};             }                             
                                               
(i) As per Object Oriented Programming, which concept is illustrated by Module 1 and Module 2 together?
(ii) What is Module 3 referred as? When do you think, Module 3 will be invoked/called?

Ans
(i) Constructor overloading.       
(ii) Destructor. It will be invoked when scope of an object gets over.
Q22
Answer the question (i) and (ii) after going through the following program:
#include 
#include        
class Bazar                         
{              char Type[20];  
                char Product[20];            
                int Qty;                
                float Price;          //Fuction 1
                Bazar()                 
                {                             
                strcpy(Type,"Electronic");
                strcpy(Product,"Calculator");
                Qty=10;               
                Price=225;          
                }                             
                public:                  //Function 2
                voif Disp()          
                {  cout<

};             }                             
                                               
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 shall be the possible output when the program gets executed? (Assuming, if required – the suggested
correction(s) are made in the program)
               

Ans
(i) No. Since the default constructor Bazar() is declared inside private section, it cannot initialize the objects declared
outside the class. Correction needed are:
The constructor Bazar () should be declared inside public section.
(ii) Electronic-Calculator:10@225
Q23
Define a class Play in C++ with the following specifications:
private members of class Play
                Playcode             integer
                PlayTitle              25 character
                Duration              float
                Noofscenes        integer
public member function of class Play
A constructor function to initialise Duration as 45 and Noofscence as 5.
Newplay() unction to accept values for Playcode and PlayTitle.
Moreinfo() function to assign the values of Duration and Noofscenes with the help of corresponding values passed as parameters to this function.
Showplay() function to display all the data member on the screen.

Ans
class Play            
{
int Playcode;
char Playtitle[25]; float Duration; int Noofscenes; public:

Play()
{
Duration=45.0;
Noofscenes=5;
}
void Newplay()
{ cout<<"enter playcode: "; cin>>Playcode; cout<<"enter playtitle: "; gets(Playtitle);

}
void Moreinfo(float d,int n)
{
Duration=d;Noofscenes=n;
}
void Showplay()
{
cout<<"Playcode: "<

}
};

CONSTRUCTOR & DESTRUCTOR   LONG                                  THEORY

No comments: