answersLogoWhite

0


Best Answer

No.

If you do not provide a default constructor, the compiler will provide a default constructor that simply allocates memory for the class, but it will not initialize the members of the class.

If you do not provide a copy constructor, then the compiler will provide a copy constructor that allocates memory for the class, and then copies the member's data from class to class. This is bad if the class contains pointers, because only the pointer will be copied - the objects to which the pointers point will not be copied - and you could wind up deleting an object and then using it after deletion, with potentially devastating consequences.

So, yes, it is mandatory, from a good practices point of view, and just plain mandatory when the class has pointers, to always provide a default constructor and a copy constructor, along with the appropriate destructor.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is it mandatory to use the construtors in a class in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you calculate the size of a class in memory in C plus plus?

Use sizeof( ).


Can you use c in c plus plus without using class and object?

Sure.


How do you use mandatory in a sentence?

Education is mandatory in many countries.It is mandatory that you attend this meeting.


What is an instance class in C plus plus?

You have a class(i.g. MyClass): class MyClass{ public: int MyData; }; And then you use the class like this: int main(){ MyClass MyObject; MyObject.MyData=7; }


How do you use Mandatory sentences in a sentence?

Education is mandatory in many countries.It is mandatory that you attend this meeting.


How do you use the word mandatory in a sentence?

Education is mandatory in many countries.It is mandatory that you attend this meeting.


Explain instantiation of objects in c plus plus?

Instantiation of a class literally means creating an instance of a class. This is the process of allocating memory for an object that you can use in your program.


What is the use of private constructor in c plus plus?

Private construction prevents objects from the class from being instantiated other than via a static member function of the class, a friend function or a friend class.


Why you prefer to having parent class in java?

Having a parent class in Java is not mandatory. It is a good approach to extend from a parent class that has much of the functionality that the current class needs already coded. This helps us re-use functionality/code and avoid redundant/duplicate code.


Represent a stack in c plus plus?

Use a vector with a base class type. Any objects derived from the base class can be pushed and popped from the vector just as you would from a stack.


What is a constructor and its mandatory to use constructor in a class?

Constructor is a special block of code similar to the method that is used to initialize the state of objects. If you do not define a constructor in a class, Java compiler automatically put a default constructor in the class.


Why you use public mode in c plus plus?

To expose an interface to the class members. Without an interface of some kind, an object would be useless.