CONSTRUCTOR & DESTRUCTOR EXERCISE

CONSTRUCTOR & DESTRUCTOR   LONG                     THEORY                              
Q1.
Differentiate between Constructor and Destructor function with respect to Object Oriented Programming.

Constructor
Destructor
Constructor is used to initialize the object.
Destructor is used to destroy the objects that are created in memory previously.
Constructor can takes arguments.
Destructor cannot take any arguments.
Constructor overloading can be possible means more than one constructor can be defined in same class.
Destructor overloading cannot be possible.
Syntax of Constructor:
class class_name
{
class_name(){}
class_name(argulist){}
};
Syntax of destructor:
class class_name
{
~class-name(void){}
};

Q2.
When a compiler can automatically generate a constructor if it is not defined then why is it considered that writing constructor for a class is a good practice?

When an object of the class is created a compiler can automatically generates a constructor if it is not defined. It is considered that writing constructor for a class is a good practice because constructor takes over very important duty of initialization of an object being created and relieves us from this task.
Q3.
‘Accessibility of a constructor or a destructor greatly affects the scope and visibility of their class’. Elaborate this statement.

Generally, a constructor and destructor should be defined under the public section of a class, so that its objects can be created and destroyed in any function. A private or protected constructor/destructor is not available to the non- member functions. Thus, accessibility of a constructor or a destructor greatly affects the scope and visibility of their class.
Q4.
Explain the role of a default constructor? When is it considered equivalent to a parameterized constructor? Support your answer with examples.

A default constructor is the one that takes no argument. It is automatically invoked when an object is created without providing any initial values. In case, the programmer has not defined a default constructor, the compiler automatically generates it. For example,
class A {...........};
A ob1;               // uses default constructor for creating ob1.
A parameterized constructor with default argument is equivalent to a default constructor. For example,
class A {
     int i;
     float j;
              public:
     A(int a=0,float b=1000.0);  //constructor with default argument
              };
      A::A(int a,float b)          //constructor definition
      {      i=a; j=b;    }         
int main()
{            A o1(23,27.50);       // argument value passed for o1
              A o2;                         // takes default argument to o2(0,1000.0)
}
Q5
List some of the special properties of the constructor functions.

Constructor functions are invoked automatically when the objects are created.
No return type can be specified for a constructor.
  A constructor may not be static.

  They cannot be inherited, though a derived class can call the base class constructor.
  It is not possible to take the address of a constructors.
Q6.
What is a parameterized constructor? How is it useful?

Ans.  A constructor that accepts parameters for its invocation is known as parameterized constructor. This helps you to
assign initial value to an object at the time of its creation. For example,
class Test 
{    int ant;  
     public:    // constructor with argument
     Test(int i)    
     {    ant=i; }  
};
int main()
{    Test ob1(45);   //argument value provided

}
Q7.
What is a copy constructor? What is its significance? Which situation is it invoked in? Support your answer with examples.

The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to:
Initialize one object from another of the same type. Copy an object to pass it as an argument to a function. Copy an object to return it from a function.
   class Example
  {
int a,b;
public:
Example(int x,int y)                      //Constructor with Argument a=x;
{
b=y;
cout<<"\nParameterized Constructor";
}
void Display()
{
cout<<"\nValues :"<
}          
};        
void main()
{          
Example Object(10,20);             //Copy Constructor
Example Object2=Object;       
Object.Display();                         // Constructor invoked.
Object2.Display();       
getch();           
}           
Q8.
Differentiate between a default constructor and copy constructor, giving suitable examples of each.

Default Constructor
Copy Constructor

A constructor that accepts no parameter is called the default constructor.

A default constructor takes no parameter.
Example:
class D
{
public:
D()
{  cout<<" Default constructor";  }
};

A constructor that initializes a object with the data values of another object is called copy constructor.
Copy constructor takes one parameter of its class& type.
Example:
class A
{  int i;
  public:
  A(int a)      // constructor
   {   i=a; }
A(A &s)     //copy constructor
{ i=s.i;  }
};
Q9.
Describe the importance of destructor. List some of the special properties of destructor.
Ans





A object that is existing must be scrapped off when it is no more needed. The task of scrapping off an object is carried out by a destructor. A destructor deinitializes an object and deallocates all allocated resources.
 Properties of destructor:                          
Destructor functions are invoked automatically when the objects are destroyed.           
There can be only one destructor for a class, means destructor can’t be overloaded.    
No argument can be provided to a destructor, neither does it returns any value.            
Q10





















What will be the output of following program? Explain with reasons:
class student{
     int rollno;
     char grade;
     static int count;
     public:
     student()
     {     rollno=0; grade=' ';
     }     cout<<"Creating object"<<++count<<"\n";
                
     void init(void)
     {     cout<<"\n Enter rollo and grade :";
           cin>>rollno>>grade;
     }     cout<<"\n";
                
     ~student()  { cout<<"Destroying object"<<--count n="" o:p="">
};
int student::count=0;
int main()
{    student classes[5];
     for(int i=0;i<5 i="" o:p="">
     {     cout<<"\n Enter details for student"<
     }     classes[i].init();
                
return 0;
}


Ans
First of all the program executes default constructor as constructor is invoked automatically as soon as object is created and prints following:
Creating object 1
Creating object 2
 Creating object 3
Creating object 4
Creating object 5
After that it executes init() method and asks for Entering details for 5 students as following:
Enter rollno and grade: 55
A
Enter rollno and grade: 24
D
Enter rollno and grade: 44
A
Enter rollno and grade: 67
B
Enter rollno and grade: 89
B
After that it executes destructor in reverse order as destructors are invoked in the reverse order in which the
constructor were called and print following:
Destroying object 4
Destroying object 3
Destroying object 2
Destroying object 1
Destroying object 0
                                                                                                  EXERCISE CONTINUED      

CONSTRUCTOR & DESTRUCTOR   LONG                     THEORY     

No comments: