answersLogoWhite
notificationBell

Top Answer
User Avatar
Wiki User
Answered 2011-12-25 21:45:05

The constructor is a method that runs to initialize the instance of the class. It runs right after memory allocation. The destructor is a method that runs to deinitialize the instance of the class. If runs right before memory deallocation.

001
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0
User Avatar

Your Answer

Related Questions


Constructor creates an instance of class, destructor destroys it.


In a class, the Destructor is like the opposite of the Constructor. The Constructor is run when an object of the class is created (usually to set up the variables as needed), and the Destructor is run when it is destroyed (usually to clean up or delete variables).


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 class constructor initialises object member variables, ensuring the object is in a valid state from the point of instantiation. The destructor resets those same variables when the object falls from scope. Typically, the destructor is only required to release any memory allocated to any internal class pointers.


Class constructors (there may be more than one constructor per class, one of which must be a copy constructor) allows the programmer to control the initialisation of an object instantiated from a class, thus ensuring consistent initialisation regardless of which constructor is called. The class destructor (there can be only one destructor per class) allows the programmer one final opportunity to release any memory allocated to the class members and to perform any last-minute operations before an object falls from scope.


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.


A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


A constructor creates the initial state of a collection of data. A function modifies or queries the data after it has been initialized.


Every class requires at least one constructor, the copy constructor. It is implied if not declared. If no constructors are declared, a default constructor is also implied. Every class also requires a destructor, which is also implied if not declared. The purpose of constructors is to construct the object (obviously) but by defining your own you can control how the object is constructed, and how member variables are initialised. By overloading constructors, you allow instances of your object to be constructed in several different ways. The copy constructor's default implementation performs a member-wise copy (a shallow-copy) of all the class members. If your class includes pointers, you will invariably need to provide your own copy constructor to ensure that memory is deep-copied. That is, you'll want to copy the memory being pointed at, not the pointers themselves (otherwise all copies end up pointing to the same memory, which could spell disaster when one of those instances is destroyed). The destructor allows you to tear-down your class in a controlled manner, including cleaning up any memory allocated to it. If your class includes pointers to allocated memory, you must remember to delete those pointers during destruction. The destructor is your last-chance to do so before the memory "leaks". The implied destructor will not do this for you -- you must implement one yourself. class foo { public: foo(){} // Default constructor. foo(const foo & f){} // Copy constructor. ~foo(){} // Destructor. };


Destructor usually called when programs exists. You can call destructor during execution of your program to free memory. If destructor is not called you get what is called memory leak. And if you operate with large volume of data in no time you can run out of memory.


You call a constructor automatically whenever you instantiate an object, either by declaring a reference to the object type, or by using the new keyword to create a pointer to an object type.Some examples:// Instantiate a reference to an instance of CString:CString myRef; // Calls the default constructor.myRef = "Hello world"; // Calls the assignment operator.// Declare a pointer to a CString object:CString *myPointer; // Does not call constructor; no instance is created.// Point to an existing instance of CString:myPointer = &myRef; // Does not call constructor; instance already exists.// Copy an instance of CString:myPointer = new CString( myRef ); // Calls copy constructor.delete( myPointer); // Call destructor.// Point to a new instance of CString:myPointer = new CString(); // Call default constructor.*myPointer = myRef; // Call the assignment operator.delete( myPointer ); // Call destructor.


Yes, Constructor can throw an exception in C++. Constructor do not have return type so we cannot use return codes, the best way to report failure in constructor throw an exception. In case of exception in Constructor your object's destructor will not be called and you may get memory leaks. Therefore every data mamber should be responsible to undone their mess. E.g. You may use smart pointers, so will be retuned to free strorage if excpetion occurs in Ctor.


This constructor is used to allocate the memory to the objects at the run time..


That depends on how you define body of class. If you do not define constructors then compiler will provide default constructor and it is not overloaded. If you create your own constructor you automatically overload default constructor.


A destructor in C++ is a method of a class that runs when the class is deleted. It performs cleanup of the members of the class that need cleanup, such as deallocation of subobjects referred to by pointers in the class, subobjects which were earlier allocated by a constructor or other method of the class.


There is no specific keyword for a constructor in C++. Simply define and declare a method of the class with the same name as the class and it will be a constructor. A constructor with no arguments is the default constructor, a constructor with one argument of class type is the copy constructor, and a constructor with one argument of some other type is the conversion constructor. You can provide other overloaded constructors if you want.


By dynamic constructor I assume you mean the C++ new operator. It works by calling malloc to allocate a contiguous block of memory for the specified class, returning a pointer to that memory, or NULL if the allocation fails. The amount of memory allocated is the sum of all member variables, plus padding where required. If the class is a derived class, the contiguous block of memory will be large enough to accommodate all the base class member variables as well, and the least-derived class constructor is called first to initialise those variables, working down the hierarchy to the most-derived class constructor. When the pointer is deleted, the most-derived class destructor is called first, working back up the hierarchy (providing the least-derived class destructor is declared virtual).


You should declare a virtual destructor in any class that has one or more virtual methods besides the destructor. Such classes are intended to act as base classes, and this ensures the most-derived destructor is always called first, whenever an instance of a base class is explicitly destroyed.


It is used in combination with pointers that point to a base class. When the pointer points to a base class, but is assigned to derived object, then a destructor called by the pointer needs to be the correct one. It needs to call the destructor of the derived object, so in this case, the base class destructor needs to be virtual to let the compiler know to call the destructor of the derived object.


The following is a program with 4 classes and a structure. Constructors and destructors are generated automatically by the compiler so you really only need them if you need more specialised construction. However, a user-defined constructor and destructor have been declared in class A. You don't give any details on what the program should actually do, so it's up to you to fill in the details. As it stands, this program does nothing useful whatsoever, but it has everything you asked for. #include<iostream> class A{ A() {} //constructor ~A() {} // destructor }; class B : public A {}; class C : protected B{}; class D : private C {}; struct E : public A {}; int main() { A a; B b; C c; D d; E e; }


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.




A destructor destroys an instance of a class to free up memory.


Constructor overloading, just like any function's overloading, is where more than one configuration of parameters exists for the function. Based on the number and type of the parameters, different versions of the function can be resolved by the linker. This is typically used in the constructor as the default constructor (no parameters), the copy constructor (one reference parameter of the same type as the class), and the conversion constructor (any other combination of parameters).