Friday 18 July 2014

Constructor/Destructor


1.       What is constructor?
A constructor in a class is a special type of method/function that is used to create and initialize object of a class. It is invoked automatically when the object of the class is created.
Constructor has the following properties. 
  • It has the same name as the class.
  • It is the only function in the class who does not have any return type. 
  •  It should be declared in public scope. 
  •  You cannot declare a constructor as virtual or static. 
  •  Constructor can neither be declared as const, volatile or const volatile. 
  •  Constructor can be overloaded. 
  •  If we do not declare constructor for our class then compiler provides default. 
  •  Complier provides default constructor only when we do not declare any. 
  •  We have declared a parameterized constructor in our class but we do not have constructor without parameter. If we do not pass arguments while creating object of that class, compiler tries to find default constructor which was not declared, hence we end up in error.

2.       What if constructor is declared in private scope?
If a constructor is declared in private scope then we will not be able to create object of that class outside of its scope. That means we can only create object inside the same class. Private constructor prevents to create any objects of the class.

3.       Can Abstract class have constructor?
Yes, you can either explicitly provide constructor to abstract class or if you don't, compiler will add default constructor of no argument in abstract class. This is true for all classes and its also applies on abstract class.

4.       What is copy constructor?
A copy constructor is a special constructor in the C++ programming language for creating a new object as a copy of an existing object. The first argument of such a constructor is a reference to an object of the same type as is being constructed, which might be followed by parameters of any type. Normally the compiler automatically creates a copy constructor for each class (known as a default copy constructor) but for special cases the programmer creates the copy constructor, known as a user-defined copy constructor. In such cases, the compiler does not create one. Hence, there is always one copy constructor that is either defined by the user or by the system.

 5.       What are the scenarios when a copy constructor is called?
  •  When an object is created from another object during initialization ( Class a = b )
  • When an object is created from another object as a parameter to a constructor (Class a(b))  
  • When an object is passed by value as an argument to a function e.g. fun(class a)  
  •  When an object is returned from a function    e.g.  fun () { .. return (a) ; }
  • When an exception is thrown using object type e.g.   … throw a; 
 
6.     What is the difference between shallow copy and deep copy?
Shallow Copy:

Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the reference addresses are copied i.e., only the memory address is copied.

Deep Copy:
A deep copy copies all fields, and make copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copies along with the object to which it refers.

http://www.jusfortechies.com/java/core-java/deepcopy_and_shallowcopy.php




7.  Can a copy constructor accept an object of the same class as parameter, instead of reference of the object? 
 No, it is specified in the definition of the copy constructor itself. It should generate an error if a programmer specifies a copy constructor with a first argument that is an object and not a reference. 

Because if it's not by reference, it's by value. To do that you make a copy, and to do that you call the copy constructor. But to do that, we need to make a new value, so we call the copy constructor, and so on...

((You would have infinite recursion because "to make a copy, you need to make a copy")

No comments:

Post a Comment