Top Answer
User Avatar
Wiki User
Answered 2013-03-18 13:53:08

You only need a constructor if the default constructor will not suffice. Often times, it is useful to have a constructor that takes common parameters so that you do not have to write additional code. For example, a Point class might have a constructor for Point(int x, int y), which would be a shortcut for assigning x and y independently. Other classes may not need any default values assigned, and for this, it is acceptable to just use the default constructor. Finally, some classes are virtual, static, or abstract, and so may not need a constructor because the constructor is unnecessary (static), or may be defined elsewhere (virtual, abstract).

User Avatar

Your Answer

Related Questions

The only purpose of a constructor is to initialise an instance of your class in a controlled manner. The compiler generated constructors are not guaranteed to perform a controlled initialisation, particularly if your class allocates dynamic memory to its member variables, thus it is in your best interest to declare your own default and copy constructors to ensure correct initialisation. moreover, the default constructor access is public, so if you want to override this you must declare your own constructors. There are no disadvantages in declaring your own constructor. The destructor gives your class one final chance to clean up any dynamic memory allocated to its members. Again, it is in your interest to declare your own destructor but, again, there are no disadvantages in declaring your own destructor.

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).

Yes, If you don't a default constructor will be created for you.

No .... Whats the point of declaring destructor as a private when it is supposed to be designed to free the memory after executing a program or a method ???

A constructor is used to build a new instance of a class. This is done to ensure that your newly created class has all of its data properly initialized to specific values. Java automatically initializes most values to 0 or null automatically, but if your class would like a different default value, then a constructor is the ideal place for this logic. You can also use constructors to open socket connections, database files, initialize event handlers, or almost any other conceivable action (although, you should take care not to call functions that might expose your class to other instances as a side effect before the constructor completes). If you do not provide any constructors in your class, it is automatically given a "default constructor". This is basically a function that calls the parent's default constructor, if it extends an object, then initializes all member variables to false, null, or zero, depending on the type of data. If you do provide a constructor, then only the provided constructors can be used, and the default constructor will simply disappear. So, if you wish to provide any constructors, you must normally provide a default constructor as well. Java does not have destructors. You cannot guarantee when your object will cease to live, and therefore you cannot capture the moment of its death. In other languages, a destructor is called when an object goes out of scope and therefore "dies", or when it is explicitly destroyed. Java's garbage collector does not do this, and so it does not permit destructors. However, there is a "finalize" function you may override from Object that does a similar function, but it is not guaranteed to be called (unlike a destructor), nor will it be called at the moment of death, but sometime thereafter.

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.

There is no such thing as a pure-virtual destructor. Classes can declare destructors to be virtual to ensure base class destructors call their derived class destructors first, but they cannot be declared pure virtual. This would imply the class is abstract and that the destructor must be overridden by all derived classes. But class destructors cannot be overridden. Every class, including an abstract class, must handle its own destruction. The only reason to declare a virtual destructor (but not a pure-virtual destructor) is when the class has one or more virtual methods besides the destructor.

Constructors are not a requirement of CPP. A default constructor and copy constructor are automatically generated by the compiler for every class you create when no constructors are declared. The only time you need to declare a constructor is when you wish to override the default behaviour of the generated constructors, to ensure the class is correctly initialised. When any constructor is declared, the default constructor is no longer generated by the compiler -- you must define your own default constructor (one that has no parameters, or where all the parameters have default values). The copy constructor is always generated for you regardless of how many other constructors you declare. But if the class contains pointers to allocated memory that is "owned" by the class then you must override the generated copy constructor with your own copy constructor. This is to ensure the memory is deep copied (the generated copy constructor only performs a shallow, member-wise copy of the members). Otherwise two objects of the same class will end up pointing at the same memory, which would be disastrous if either one were to be deleted. The other instance would be automatically invalidated because it would point to memory that was released by the other instance's destructor.

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.

A constructor is a special method that is created when the object is created or defined. This particular method holds the same name as that of the object and it initializes the instance of the object whenever that object is created. The constructor also usually holds the initializations of the different declared member variables of its object. Unlike some of the other methods, the constructor does not return a value, not even void.When you create an object, if you do not declare a constructor, the compiler would create one for your program; this is useful because it lets all other objects and functions of the program know that this object exists. This compiler created constructor is called the default constructor. If you want to declare your own constructor, simply add a method with the same name as the object in the public section of the object. When you declare an instance of an object, whether you use that object or not, a constructor for the object is created and signals itself.A constructor is declared without a return value, that also excludes void.Therefore, when implemented, do not return a value:Constructor Exampleclass rectangle { // A simple class int height; int width; public: rectangle(void); // with a constuctor, ~rectangle(void); // and a destructor }; rectangle::rectangle(void) // constuctor { height = 6; width = 6; } sagar sainath samant. sybsc (computer science)

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.

A constructor is a special class method that instantiates an object of the class. All objects must have at least two constructors, one of which must be a copy constructor. Even if you do not explicitly declare a copy constructor, one is generated for you with public access by the compiler. The purpose of the copy constructor is to instantiate new objects from existing objects. Even if you never explicitly call the copy constructor, it is automatically called whenever you pass an object to a function by value, as the object must be copied. If you do not declare any constructors, then both the copy constructor and the default constructor are generated for you. The default constructor is a constructor that accepts no parameters, or that is declared with all default parameters. Although the compiler will generate default and copy constructors for you, it is always recommended that you declare your own constructors, thus ensuring your object members are always correctly initialised and valid. An uninitialised object is a potential time-bomb. Constructors are not functions; they do not return any value, not even void. The assignment operator (which does return a value) is not a constructor; it is used to initialise an existing object from another existing object -- it does not instantiate a new object. Constructors are called whenever you instantiate a reference to an object, or allocate memory to an object using the new operator, or copy a new object from an existing object. All constructors have the same name as the class itself. Construction overloads can be differentiated by their signature (the number and type of parameters they accept). The copy constructor is signified by the fact its only parameter is a constant reference to an object of the same class. class Object { public: Object(); // Default constructor (no parameters) Object(const Object& object); // Copy constructor Object(int x); // Overloaded constructor } As well as constructors, it is recommended you also declare your own assignment operator and destructor. Even if the compiler-generated versions are adequate for your needs, it costs nothing but a little time to declare your own. But if your class allocates dynamic memory on the heap, you must include code in the constructors, destructor and assignment operator to ensure that memory is correctly initialised and released, and that self-references are correctly accounted for; the compiler-generated methods will not do it for you.

We can't override a constructor. When any method is declared as virtual it should be overridden in its derived class. Since constructor is used for initialising the variables and if declare a constructor as virtual it cant be overridden

Overloading a function simply means providing the same function name with different argument types. Class constructors are no different. In fact, even if you declare no constructors in a class, there will be two compiler-generated constructor overloads provided for you: a default constructor; and a copy constructor. If you declare any other constructors, the compiler-generated default constructor will no longer be generated. You must declare your own default constructor if you require one. The copy constructor is always generated, however the default implementation only performs a member-wise copy of the class members. If your class contains a pointer to allocated memory you must provide your own copy constructor to perform a deep-copy of those pointers, so each instances "owns" its own copy of the memory.

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.

No. But you can override it. A default destructor is provided even if you don't declare one.

To declare a class, a basic layout could look like this: //any header files that are going to be required in the class should //be defined here, for example //example inclusion of header file #include "name_of_header.hpp" class NameOfClass { public: //constructor NameOfClass(); //destructor ~NameOfClass(); private: //you can include any data members only accessible by the //class here };

More or less as you would any other function, except there is no return type (not even void) and an initialisation list can be placed between the declaration and the definition that follows it. The initialisation list allows your constructor to call base class constructors besides the default constructor as well as initialise member variables according to the parameters passed to your constructor. The constructor's name must be the name of the class. Note that if you don't declare any constructors, the compiler generates both a default and copy constructor. If any constructor is declared you lose the default constructor unless you declare one yourself. The copy constructor is always present but must be overridden if your class contains pointers to memory allocated to the class itself. If you don't, the compiler generated copy constructor will perform a member-wise copy of the member variables, resulting in a shallow copy of the pointers themselves, rather than a deep copy of the memory they point to. The copy constructor must accept a constant reference to the same class of object.

I dont think we can have Protected Constructors but yes we can have Private constructors. We can declare the constructor as Private to ensure that no other class can instantiate it. We use this in the singleton design pattern

A constructor is not a mandatory member that we need to code specifically for a class. While creating a class, even if we miss out coding the constructor, Java would create a default constructor all by itself. The constructor is usually the place where we initialize things that are required by the class. Hence it is a good practice to code the constructor for our class. Tip: If you do not want anyone to instantiate your class, you can declare the constructor as private. In that way no other class can instantiate your class.

All classes require constructors. Even if you declare none, the compiler will automatically generate a default constructor (which invokes the base class default constructor) and a copy constructor (which invokes the base class copy constructor). But if you define any constructor, you lose the default constructor (the copy constructor is always generated). In this particular case the compiler-generated constructors may suffice, in which case you need not define your own. However, if the base class has other constructors besides a default and copy constructor, you may wish to add similar constructors to your derived class in order to make use of them. Another cavaet is that if either or both the default and copy constructors of the base class have been defined but are not implemented (disabled), you must also disable them in the derived class, as well as provide user-defined constructors that match the base class constructors. Remember that you invoke specific base class constructors via the derived class constructor initialisation list, as follows: class base { public: base(): m_data(0) {} base( const base& rhs ) : m_data(0) {} virtual ~base() {} base& operator= ( const base& rhs ) { m_data=rhs.m_data; return( *this ); } private: int m_data; }; class derived : public base { public: derived(): base() {} derived( const derived& rhs ) : base( rhs ) {} }; Note also that unless you define your own, a destructor and a default assignement operator are also generated by the compiler. However, the base class destructor must be declared virtual to ensure correct tear-down. The compiler-generated destructor of a base class is always declared non-virtual. In the above example, there is no need to declare either a destructor nor an assignement operator in the derived class, since the compiler-generated versions will behave accordingly.

The same way you overload any function -- you provide a new signature for it. The signature is determined by the number and type of parameters it accepts, and whether they are const or not. Every class has a public default constructor if no other constructor is declared . Also every class has a public copy constructor whether you declare one or not. So every constructor you do declare is, in fact, an overloaded constructor. Example: class myClass { public: myClass():myInt(0){} // default ctor (no parameters) myClass(const myClass & copy){myint = copy.myInt;} // copy ctor myClass(int newInt):myInt(newInt){} // overloaded ctor private: int myint; };

Yes, but that means you can't define an instance of the class without arguments to new.

Constructors are implicitly constant members of a class. They do not modify objects, rather they initialize them. So constant and not constant objects can invoke them: const MyClass object; // invokes MyClass::MyClass() constructor

A constructor is usually the first method that gets invoked when a class is instantiated. This method usually creates the class object and sets initial variable values in order for the class object to do its functions. 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.

Copyright ยฉ 2021 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.