DISHA NIRDESHAN

Enlightens your career

Tuesday, June 21, 2011

Questions on Constructors

 

CONSTRUCTORS QUESTIONS

 

 

What is a constructor? 

A constructor allocates space for an object and initializes it. Constructors always have the same name as their class. A class may have more than one constructor.

Or

Constructors allow initialization of objects at the time of their creation. Constructor function is a special function that is a member of the class and has same name as that of the class. An object's constructor is automatically called whenever the object is created (statically or dynamically). Constructors are always public. They are used to make initializations at the time of object creation.

 

What is Destructors?

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted. A destructor takes no arguments and has no return type.

                                                Or

Destructors are complements of constructors. When an object is destroyed, its destructor is automatically called. Destructors are mainly useful for doing the clean up job. E.g. an object may have allocated some memory during its lifetime; destructors are the place where this memory is deallocated. Or an object may need to close some files by releasing its handles which it had previously obtained.

Destructor function has same name as that of a constructor; but the name is preceded by a tilde ('~') sign.

Example
Student::~Student() {
     Age = 0;
    
}

 

 

 

What is virtual constructors/destructors?

The explicit destroying of object with the use of delete operator to a base class pointer to the object is performed by the destructor of the base-class is invoked on that object.

 

Explain default constructor or what is a default constructor?

A constructor with no arguments or all the arguments has default values.

What is copy constructor?

Creation of an object as a copy of an existing object us know as copy constructor

What is a conversion constructor?

It is a constructor that accepts one argument of a different type.

 

What are the restrictions apply to constructors and destructors?  

The following restrictions apply to constructors and destructors

1.     Constructors and destructors don't return values.

2.     The addresses of constructors and destructors can't be taken so we can't use references and pointers on them.

3.     Constructors cannot be declared with the keyword virtual.

4.     Constructors and destructors cannot be declared static, const, or volatile.

Explain the order in which constructors are called when an object of a derived class is created.  

The constructors of any virtual base classes are called first in the order of inheritance.
Non-virtual base class constructors are called next.
The derived class constructor is called last.

What is a virtual destructor? Explain the use of it.  

If the destructor in the base class is not made virtual, then an object that might have been declared of type base class and instance of child class would simply call the base class destructor without calling the derived class destructor.

Hence, by making the destructor in the base class virtual, we ensure that the derived class destructor gets called before the base class destructor.

class x
{
           public:          
           x(){printf("\nBase Constructor\n");}
           ~x(){printf("\nBase Destructor\n");} 
};

class y : public x 
{
         public:
         y(){printf("\nDerived Constructor\n");}
         ~y(){printf("\nDerived Destructor\n");} 
};
int main()
{
         x* object=new y;
         delete object;
         return 0;
}

Output:
Base Constructor
Derived Constructor
Base Destructor

By Changing
~x(){printf("\nBase Destructor\n");}
to
virtual ~x(){printf("\nBase Destructor\n");}

 

Output:
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor

 

 

Can constructors have parameters?

Yes, constructors can have parameters. so we can overload it.

0 comments :