CONSTRUCTOR
     AND
 DESTRUCTOR
1. Point out the errors, if any, in the following program.
       class date
       {
               private:
                       int day,month,year;
               date()
               {
                       day = 2;
                       month = 10;
                       year = 2008;
               }
       };

       void main()
       {
              date today;
       }
Error :
The default constructor is defined
under private section; so it is not
  accessible in function main( ).
2. Point out the errors, if any, in the following program.
        class value
        {
                private:
                        int i;
                        float f;
                public:
                        value()
                        {
                                 i = 0;
                                 f = 0.0;
                                 return 1;
                        }
        };
        void main()
        {
                value vi;
        }
Error :
The constructor should not return
   any value. Here constructor
   contains return statement.
3. Read the following code segment.
           #include <iostream.h>
           class sample
           {
                      int x, y;
                      public:
                         sample(int a = 10, int b = 20)
                         {
                                 x = a;
                                 y = b;
                        }
                        void show()
                        {
                                 cout << “n x = “ << x << “y = “ << y;
                         }
           };
           void main()
           {
                      sample s1;
                      sample s2(4);
                      sample s3 = sample(6,8);
                      s1.show();
                      s2.show();
                      s3.show();
           }
Explain the working of constructor for the creation of each object.
What will be the output of the above code.
b) x = 10, y=20
   x = 4, y=20
    x = 6, y=8
4. A class named employee is having the
following constructors.
     i) employee();
     ii) employee(int, float);
     iii) employee(employee &f);
A) Name the category to which each
   constructor belongs.
B) Write down the purpose of each.
C) Define the destructor for this class.
1. i) default constructor,
   ii) parameterized constructor,
  iii) copy constructor
2. short note about default constructor,
parameterized constructor and copy
constructor
3. ~employee( ) { }
5. The following code defines a class for finding out the area of a circle.
#include<iostream.h>
class circle
{
          float rad;
          public:
                    circle(float x)
                    {
                              rad = x;
                    }
                    void area()
                    {
                              cout << 3.14 * rad * rad;
                    }
};
void main()
{
          circle(5.5).area();
}
What is the purpose of the statement in the main function?
What is the output?
a)Through this statement, an
anonymous object (temporary instance)
is created and initialized with the value
5.5 and that object calls the member
function area( )
b) 94.985
6. While declaration, initialization is
possible with variables, arrays and
structures. What about of classes?
Justify your answer.


• But an object of a class cannot be initialized
  in similar fashion because the access labels
  restrict the accessibility of the data members.
7. A constructor is usually defined
in public section. Why?


• Generally a constructor is defined under the
  public section of a class so that its objects can
  be created in any function.
8. Is constructor definition
compulsory? Give justification to
support your answer.

• Yes, constructor definition is compulsory. A
  Constructor is defined like other member
  functions of a class. It can be defined either
  inside or outside the class specification.
9. List down the characteristics of
constructors.
• They should be declared in the public section.
• They are invoked automatically when the
  objects are created.
• They do not have return types, not even void
  and therefore, and they cannot return values.
• They cannot be inherited.
• Like other C++ functions, they can have default
  arguments.
• They cannot be virtual.
• We cannot refer to their addresses.
• They cannot be static.
10. List down the characteristics of
destructors.
• They are invoked automatically when the
  objects are destroyed.
• They obey the usual access rules that other
  member functions do.
• They cannot be inherited.
• No argument can be provided to a destructor,
  neither does it return any value.
• They cannot be virtual.
• We cannot refer to their addresses.
• They cannot be static.
• If there is no destructor in a class, a default
  destructor is generated by the compiler.

Constructor and destructor

  • 1.
    CONSTRUCTOR AND DESTRUCTOR
  • 2.
    1. Point outthe errors, if any, in the following program. class date { private: int day,month,year; date() { day = 2; month = 10; year = 2008; } }; void main() { date today; }
  • 3.
    Error : The defaultconstructor is defined under private section; so it is not accessible in function main( ).
  • 4.
    2. Point outthe errors, if any, in the following program. class value { private: int i; float f; public: value() { i = 0; f = 0.0; return 1; } }; void main() { value vi; }
  • 5.
    Error : The constructorshould not return any value. Here constructor contains return statement.
  • 6.
    3. Read thefollowing code segment. #include <iostream.h> class sample { int x, y; public: sample(int a = 10, int b = 20) { x = a; y = b; } void show() { cout << “n x = “ << x << “y = “ << y; } }; void main() { sample s1; sample s2(4); sample s3 = sample(6,8); s1.show(); s2.show(); s3.show(); } Explain the working of constructor for the creation of each object. What will be the output of the above code.
  • 7.
    b) x =10, y=20 x = 4, y=20 x = 6, y=8
  • 8.
    4. A classnamed employee is having the following constructors. i) employee(); ii) employee(int, float); iii) employee(employee &f); A) Name the category to which each constructor belongs. B) Write down the purpose of each. C) Define the destructor for this class.
  • 9.
    1. i) defaultconstructor, ii) parameterized constructor, iii) copy constructor 2. short note about default constructor, parameterized constructor and copy constructor 3. ~employee( ) { }
  • 10.
    5. The followingcode defines a class for finding out the area of a circle. #include<iostream.h> class circle { float rad; public: circle(float x) { rad = x; } void area() { cout << 3.14 * rad * rad; } }; void main() { circle(5.5).area(); } What is the purpose of the statement in the main function? What is the output?
  • 11.
    a)Through this statement,an anonymous object (temporary instance) is created and initialized with the value 5.5 and that object calls the member function area( ) b) 94.985
  • 12.
    6. While declaration,initialization is possible with variables, arrays and structures. What about of classes? Justify your answer. • But an object of a class cannot be initialized in similar fashion because the access labels restrict the accessibility of the data members.
  • 13.
    7. A constructoris usually defined in public section. Why? • Generally a constructor is defined under the public section of a class so that its objects can be created in any function.
  • 14.
    8. Is constructordefinition compulsory? Give justification to support your answer. • Yes, constructor definition is compulsory. A Constructor is defined like other member functions of a class. It can be defined either inside or outside the class specification.
  • 15.
    9. List downthe characteristics of constructors. • They should be declared in the public section. • They are invoked automatically when the objects are created. • They do not have return types, not even void and therefore, and they cannot return values. • They cannot be inherited. • Like other C++ functions, they can have default arguments. • They cannot be virtual. • We cannot refer to their addresses. • They cannot be static.
  • 16.
    10. List downthe characteristics of destructors. • They are invoked automatically when the objects are destroyed. • They obey the usual access rules that other member functions do. • They cannot be inherited. • No argument can be provided to a destructor, neither does it return any value. • They cannot be virtual. • We cannot refer to their addresses. • They cannot be static. • If there is no destructor in a class, a default destructor is generated by the compiler.