Page 172 - C++
P. 172

(b) Answer the questions (i) and (ii) after going through the following class:                      (2)

        class WORK
        {
               int WorkId;
               char WorkType;
        public:
               ~WORK( )               //Function 1
               {
                       cout<<“Un-Allocated”<<endl;
               }
               void Status( ) // Function 2
               {
                       cout<<WorkId<<“:”<<WorkType<<endl;
               }
               WORK( )               // Function 3
               {
                       WorkId=10;
                       WorkType=’T’;
               }
               WORK (WORK &W)   // Function 4
               {
                       WorkId = W.WorkId+12;
                       WorkType=W.WorkType+W;
               }
        };
        (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 an object gets over? Is it a Constructor OR
        Destructor OR Overloaded Function OR Copy Constructor?

        Ans. Function 1       Destructor.

        (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 on execution of statement written as Statement 2? What is this function
        specifically known as out of Destructor or Copy Constructor or Default Constructor?

        Ans. Function 4       Copy Constructor

                                                             OR

        (b) Differentiate between default constructor and parameterized constructor. Explain with suitable example.
                                                                                                             (2)

        Ans. Default Constructor (No argument constructor):- A default constructor accepts no parameters. When no
        constructor is defined in the class, compiler provides the default constructor.
   167   168   169   170   171   172   173   174   175   176   177