C++ Constructors

  Home  C++ Programming  C++ Constructors


“C++ Constructors frequently Asked Questions by expert members with experience in C++ constructors. These questions and answers will help you strengthen your technical skills, prepare for the new job test and quickly revise the concepts”



51 C++ Constructors Questions And Answers

1⟩ Can you please explain Explain constructors and destructors?

Constructors are the member functions of the class that executes automatically whenever an object is created. Constructors have the same name as the class. Constructors initialize the class. Constructors can’t have return type. Destructors are called when the objects are destroyed.

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.

 233 views

2⟩ Do you know what are destructors?

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.

We will expand the above example of a stack class to include a destructor:

#include <iostream>

using namespace std;

class stack

{

int top, bottom;

int data[20];

stack() //constructor function

{

top = bottom;

cout <<”Inside Stack Constructor: Stack initializedn”;

}

int stackfull()

{

return ((top == max – 1)?1:0);

}

int stackempty()

{

return (top == bottom)?1:0);

}

void push(int no)

{

if(stackfull())

cout<<”Stack is full”;

else

data[++top] = no;

}

int pop()

{

if(stackempty())

cout<<”Nothing to pop.. Stack is Empty!n”;

else

return(data[top- -];

}

~stack() //destructor function

{

cout <<”Inside Stack Destructor: Stack Destroyedn”;

}

};

int main()

{

int i, no;

stack st; //object is created; hence constructor is invoked- stack initialization done

cout <<”Entered Mainn”;

for(i = 0; i < 10; i++)

st.push(i);

no = s.pop();

cout <<”The popped element is:”<<no << “n”;

return 0;

}// at the end of object’s lifetime, destructor is invoked

The o/p of the program would be:

Entered Main

Inside Stack Constructor: Stack initialized

The popped element is: 9

Inside Stack Destructor: Stack Destroyed

As seen from the o/p the constructors and destructors are automatically called.

 225 views

4⟩ What is virtual destructor and how to use 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 a

{

public:

a(){printf("nBase Constructorn");}

~a(){printf("nBase Destructorn");}

};

class b : public a

{

public:

b(){printf("nDerived Constructorn");}

~b(){printf("nDerived Destructorn");}

};

int main()

{

a* obj=new b;

delete obj;

return 0;

}

Output:

Base Constructor

Derived Constructor

Base Destructor

By Changing

~a(){printf("nBase Destructorn");}

to

virtual ~a(){printf("nBase Destructorn");}

Output:

Base Constructor

Derived Constructor

Derived Destructor

Base Destructor

 262 views

5⟩ What is Virtual 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.

The above process can be simplified by declaring a virtual base class destructor.

All the derived class destructors are made virtual in spite of having the same name as the base class destructor. In case the object in the hierarchy is destroyed explicitly by using delete operator to the base class pointer to a derived object, the appropriate destructor will be invoked.

 240 views

6⟩ What is constructor in C++?

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.

Consider following example of a stack class:

#include <iostream>

using namespace std;

class stack

{

int top, bottom;

int data[20];

stack() //constructor function

{

top = bottom;

cout <<”Inside Stack Constructor: Stack

initialized”;

}

int stackfull()

{

return ((top == max – 1)?1:0);

}

int stackempty()

{

return (top == bottom)?1:0);

}

void push(int no)

{

if(stackfull())

cout<<”Stack is full”;

else

data[++top] = no;

}

int pop()

{

if(stackempty())

cout<<”Nothing to pop.. Stack is Empty!”;

else

return(data[top--];

}

};

int main()

{

int i, no;

stack st; //object is created; hence constructor is

invoked- stack initialization done

cout <<” Entered Mainn”;

for(i = 0; i < 10; i++)

st.push(i);

no = s.pop();

cout <<”The popped element is:”<

return 0;

}

The o/p of the program would be:

Entered Main

Inside Stack Constructor: Stack initialized

The popped element is: 9

As seen above, the stack object is initialized automatically at the time of creation by the constructor. So we don’t need to write and invoke initialization functions explicitly.

 240 views

9⟩ Tell me what are the restrictions apply to constructors and destructors?

The following restrictions apply to constructors and destructors

Constructors and destructors don't return values.

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

Constructors cannot be declared with the keyword virtual.

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

 215 views

10⟩ What is deep copy?

A deep copy creates a copy of the dynamically allocated objects too. You would need to use a copy constructor and overload an assignment operator for this.

 227 views

11⟩ What is shallow?

A shallow copy just copies the values of the data as they are. Even if there is a pointer that points to dynamically allocated memory, the pointer in the copy will point to the same dynamically allocated object.

 224 views