answersLogoWhite

0


Best Answer

Functions and Constructors are similar in many ways. They can have arguments, they can have any amount of code, they can access the class's variables etc. the only difference is that a method in java needs to mandatorily have a return type but a Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

Constructor construct assign the value of class object while destructor delete or free memory which is allocated by constructor.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Constructor is called when the object is created. Destructor is called when the object is destroyed

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between the constructor to and destructor?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Constructor cannot be virtual but destructor can be virtual justify?

bcoz constructor cant be invoked


What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


A method that is automatically called when an instance of a class is created?

The constructor of a class is automatically called when an instance of the class is created (using new in C++). The constructor method has the same name as the class that it is a part of. Constructors have no type and do not return anything. Similarly, the destructor is automatically called when the instance of the class is destroyed. The destructor is the same name as the class and is preceded by a tilde (~) For example: class Example { public: Example() // Constructor { printf("Object created\n"); } ~Example() // Destructor { printf("Object destroyed\n") } }; int main() { Example* x = new Example(); // Creates object, calls constructor delete x; // Calls destructor, deletes object return 0; }


Which class methods does the compiler generate automatically if you don't provide them explicitly?

Default constructor: X()Copy constructor: X(const X&)Copy assignment operator: X& operator=(const X&)Move constructor: X(X&&)Move assignment operator: X& operator=(XX&)Destructor: ~X()By default, the compiler will generate each of these operations if a program uses it. However, if the programmer declares any constructor for a class, the default constructor for that class is not generated. If the programmer declares a copy or move operation, no copy, move or destructor is generated. If the programmer declares a destructor, no move operation is generated (a copy constructor is generated for backward compatibility).We can also suppress generation of specific operations with the =delete pseudo-initialiser:class X {public:X (const X&) =delete; // suppress the compiler-generated copy operationsX& operator=(const X&) =delete;// ...};


What are the advantages of constructor and destructor?

Without a copy constructor the only way to copy an object would be to instantiate a new object (or use an existing object) and then assign another object's value to it. However, it would be very odd indeed to have a copy assignment operator without a matching copy constructor. If you have one, you must have both. If you do not need to copy an object of a particular class, however, you can simply delete both the copy constructor and the copy assigment operator for that class. Any attempt to copy or copy assign would then result in a compile-time error.

Related questions

What is difference between constructor and destructor in net?

dono lah bodo


Constructor cannot be virtual but destructor can be virtual justify?

bcoz constructor cant be invoked


Constructor and destructor invocation in c?

Not possible in C.


Why pointer of constructor is made but not of destructor?

When a constructor is invoked dynamically, the new operator allocates the required memory, initialises it according to the constructor, then returns a pointer to the allocation. The destructor is invoked by deleting the pointer. It wouldn't make any sense to return a pointer from a deletion.


How can you recognize a constructor in a class?

A class's constructor will have the same name of the class and no return type (not even void): class Example(){ Example() {printf("This is the constructor\n");} ~Example(){printf("This is the destructor\n");} };


What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


A method that is automatically called when an instance of a class is created?

The constructor of a class is automatically called when an instance of the class is created (using new in C++). The constructor method has the same name as the class that it is a part of. Constructors have no type and do not return anything. Similarly, the destructor is automatically called when the instance of the class is destroyed. The destructor is the same name as the class and is preceded by a tilde (~) For example: class Example { public: Example() // Constructor { printf("Object created\n"); } ~Example() // Destructor { printf("Object destroyed\n") } }; int main() { Example* x = new Example(); // Creates object, calls constructor delete x; // Calls destructor, deletes object return 0; }


Which class methods does the compiler generate automatically if you don't provide them explicitly?

Default constructor: X()Copy constructor: X(const X&)Copy assignment operator: X& operator=(const X&)Move constructor: X(X&&)Move assignment operator: X& operator=(XX&)Destructor: ~X()By default, the compiler will generate each of these operations if a program uses it. However, if the programmer declares any constructor for a class, the default constructor for that class is not generated. If the programmer declares a copy or move operation, no copy, move or destructor is generated. If the programmer declares a destructor, no move operation is generated (a copy constructor is generated for backward compatibility).We can also suppress generation of specific operations with the =delete pseudo-initialiser:class X {public:X (const X&) =delete; // suppress the compiler-generated copy operationsX& operator=(const X&) =delete;// ...};


What are the advantages of constructor and destructor?

Without a copy constructor the only way to copy an object would be to instantiate a new object (or use an existing object) and then assign another object's value to it. However, it would be very odd indeed to have a copy assignment operator without a matching copy constructor. If you have one, you must have both. If you do not need to copy an object of a particular class, however, you can simply delete both the copy constructor and the copy assigment operator for that class. Any attempt to copy or copy assign would then result in a compile-time error.


Why constructor and destructor cannot be made static?

The term "destructor" made me believe this question is related to .Net languages. A destructor is to destroy an instance of object. If it is available at static/class level, what is it going to destroy? The entire class, so the class no longer available? Thus, semantically, destructor should be an instance method. Constructor is on the opposite end of the life cycle of an instance. However, in .NET, a static constructor is allowed. Personally, I call this static constructor as a class initialization method. This method will be invoked by the .net framework only once when the class is loaded into the application domain. With the similar concept, there should be a "finalizer" of the class when the class is unloaded out of the application domain. But wait, does a class ever go out of the application domain once it's loaded? Yes, only at the termination of the application! Currently a class cannot be unloaded explicitly in codes and thus no point to have a static finalizer.


What is the difference between destructors in c plus plus and c sharp?

In C# only class instances can have a destructor, whereas both class and struct instances can have a destructor in C++. While syntactically similar, a C++ destructor executes exactly as written, whereas a C# destructor merely provides the body of the try clause of the class' finalize method.


How do you construct a program with destructor more than constructors?

The question is unclear, but classes can only have one destructor at most. They can have as many constructors as required. Even if you do not declare any constructors, the compiler will automatically generate a default constructor (which initialises all member variables to zero) and a copy constructor (which performs a member-wise, shallow copy of the members). If your class contains member pointers and allocates memory to them, you must provide your own destructor to clean up those memory allocations as well as provide a copy constructor to deep copy the memory allocations (thus ensuring no two instances of the class point to the same memory).