[#11] – Constructors and Destructors – Questions

C-plus-plus-programming-questions-techhyme

Initializing a variable is considered very helpful while making programs. We can initialize variables of built-in data types at the time of their declarations. For Example

int a = 10;

Index:

In Object Oriented Programming (OOP) language like C++, the need of initialization of objects which are real world entities is even more common.

Key Points To Remember –

  1. A constructor is a special member function which is executed automatically whenever an object of a class is created. It is used to initialize some valid values and allocate resources (memory) to the data members of an object.
  2. The name of the constructor is the same as that of its class. A constructor must not have a return type.
  3. Constructors can also be defined outside the class using scope resolution operator.
  4. The constructor that takes parameters is known as parameterized constructor. It can take one or more parameters.
  5. Constructor overloading allows a class to have more than one constructor that have same name as that of the class but differs only in terms of member of parameters or parameter’s data type.
  6. Constructor with default arguments allows a constructor call to specify fewer number of arguments instead of all the parameters defined in the constructor and the omitted arguments can take default values provided in the constructor definition.
  7. The data members of an object after creation can be initialized at run time using constructor. Such initialization of data members is called dynamic initialization.
  8. A destructor is a member function having the same name as that of its class preceded by a tilde(~) and is executed automatically when an object of a class is destroyed. It neither takes any parameter nor return any value. A class can have only one destructor and hence cannot be overloaded.
  9. Constructors can also be used to allocate memory for the object at run time using new operator. The memory allocated using constructor is deallocated explicitly through destructor member function using delete operator. The operations performed using new and delete operators within constructor and destructor respectively are known as the dynamic operations.
  10. The objects which are created at run time can be destroyed at any stage of the program execution are known as dynamic objects. In C++, programmer can create dynamic objects using new operator and destroy it using delete operator. The unnamed dynamic objects are referred using pointer(s).
  11. A copy constructor is a constructor that creates a new object using an existing object of the same class and initializes each data member of newly created object with its corresponding date members of an existing object passed as argument. A copy constructor is invoked automatically whenever a new object is created form an existing object of the same class. Whenever an object is passed by value or returned from a function, a copy constructor is invoked automatically.
  12. Copy constructor is used in those cases when the data member of the copying object is a pointer pointing to dynamically allocated memory.
  13. Automatic objects are the objects defined inside a function in which they are to be used. The constructor for an automatic object is called at the point where we defined the object and destructor is called when the object goes out of scope.
  14. External objects are created outside any function body. They are visible throughout the program and exists for the lifetime of the program. The constructor are invoked for global objects before any other function executes and destructor is called when main() terminates.
  15. Static objects are the objects which remain in memory throughout the execution of the program and are visible in the function where they are declared. The destructor is called when main() terminates.
  16. If you don’t define any constructor explicitly in the class then the compiler generates a constructor with no parameters and a copy constructor automatically.
  17. If you define a copy constructor explicitly in the class then the compiler will not generate the default copy constructor.
  18. If you define any constructor explicitly in the class (copy or non copy) then the compiler doesn’t generate a constructor with no parameters (default constructor) for you.

Viva Voce Questions – 

Here are commonly asked questions with answers:

  1. Why constructors don’t have any return value not even void ?
    • Constructor don’t have any return value because it is used to initialize data members and is called automatically.
  2. How do you distinguish constructor from a normal member function ?
    • Constructors are distinguished from the normal function of the class as they have same name as that of class.
  3. Can a constructor be overloaded ?
    • Yes, constructors can be overloaded so as to provide more than one type of initialization.
  4. What are the different ways for specifying arguments to a constructor ?
    • There are 3 ways
      rect r1(5,6) ;
      rect r1=rect(5,6);
      rect r2=5;
      For the third form only one argument constructor can work
  5. Can destructor be overloaded ?
    • Since we have only one destructor in a class which cannot have any parameters so it cannot be overloaded.
  6. How a dynamic object can be created in C++ ?
    • A dynamic object can be created using new operator as follows,
      dist *p;// pointer to class dist
      p=new dist;
      The above statement creates an object of dist class dynamically.
  7. How a dynamic object is destroyed explicitly in C++ ?
    • A dynamic object is destroyed explicitly using delete operator as follows,
      delete p;
      where p is a pointer pointing to dynamic object.
  8. What will happen when the statement dist *p=new dist[10]; is executed ? Here dist is the name of the class.
    • The statement,
      dist *p=new dist[10] ;
      on execution creates 10 objects of ‘dist’ class dynamically and return the pointer to first object. Also, no argument constructor is invoked for each object created.
  9. What is the difference between delete ptr; and delete [] ptr; where ptr is pointer pointing to dynamic object(s)?
    • Use the statement,
      delete ptr;// points to dynamic object
      to deallcoate memory allocated to single dynamic object created using statement,
      dist *ptr = new dist;The statement,
      delete [] ptr;
      is used to deallocate block of memory allocated to array of dynamic objects created using the statement,
      dist *ptr=new dist[10];
  10. Can we initialize an object with default constructor using the statement student s1(); ?
    • The statement,
      student sl();
      is not used to initialized s1 object using default constructor as compiler interprets the above statement as declaration of a function having name s1 which takes no parameters and return an object of class student. The correct statement of object initialization using default constructor is to skip the trailing empty parentheses as student s1;
  11. When a copy constructor is invoked ?
    • When an object is passed by value, returned by value or explicitly copied, a copy constructor is invoked.
  12. When the copy constructor is useful ?
    • Copy constructor is useful in those cases when the data member of the copying object (source object) is a pointer pointing o dynamically allocated memory.
  13. If a class contains no constructor, can its objects be created ?
    • Yes, objects of class can still be created. However, these objects are not initialized. For each class, there is a pre-defined default constructor that does nothing.

Other Similar Questions – 

  1. What do you mean by constructor ? Why do we need it ?
  2. Is it necessary to create constructor ? Give reasons.
  3. How a constructor is different from normal member function ?
  4. How will you declare and invoke a constructor ? Give example.
  5. List the characteristics of a constructor ?
  6. How will you define a constructor inside and outside a class.
  7. Can you declare a constructor in the private section of the class ?
  8. What do you mean by default constructor ?
  9. Under what situations the default constructor is used for automatic initialization of objects .
  10. What are the advantages and disadvantages of default constructor.
  11. What is a parameterized constructor ?
  12. Can we have more than one constructor in a class ? If yes, then explain the need of such a situation.
  13. What do you understand by constructor overloading ?
  14. What is an Initializer list ?
  15. Explain constructor with default arguments. How it differs from default constructor.
  16. What do you mean by dynamic initialization of objects ? Why do we need it ?
  17. What is a destructor ? Why do we need it ?
  18. List the characteristics of destructor.
  19. Compare and contrast a destructor from a constructor.
  20. Explain with example the order of construction and destruction of objects ?
  21. How is memory allocated and deallocated dynamically using constructor and destructor.
  22. How is an array deallocated dynamically ?
  23. What is the difference between array of pointers and pointer to array of objects.
  24. What is a copy constructor ? Explain its need .
  25. What are the different situations in which copy constructor is automatically invoked.
  26. Why an argument to a copy constructor is passed by reference and not by value ?
  27. What do you mean by automatic, static and external objects.
You may also like:

Related Posts

Leave a Reply