answersLogoWhite

Top Answer
User Avatar
Wiki User
Answered 2009-09-11 13:50:34

Constructor creates an instance of class, destructor destroys it.

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

Your Answer

Related Questions


yes,because in constructor overloading constructor have same and different parameter list. In method overloading method have same name and different parameter list.


Constructors are not functions as such, they merely describe the initialisation phase of a class. Since they are only of relevance to the classes in which they are declared, there is no need nor want to inherit them. Every class must define its own constructors, which can include calls to any base class constructors from within the derived class initialisation list (if no list is declared, or a base class constructor omitted, then the default constructor is called instead). Class destructors are also not functions as such, but if the class has any virtual methods, or the class is not prevented from acting as a base class, the destructor must be virtual. This is simply to ensure that when a base class object is destroyed, its derived class destructor is called first. If the destructor were not virtual, destroying a base class would not destroy the derived class, leaving it in an undefined state.


Object initialisation is taken care of by the object's constructor(s). Initialisation is generally done during the initialisation stage of the constructor (immediately before the body of the constructor). Complex members such as pointers can be initialised to NULL in the initialisation stage. If new memory must be allocated to a member pointer, do it in the body of the constructor. Remember to clean up all allocated memory in the class destructor. class cMyClass { public: // Construction. cMyClass(); // Default constructor. cMyClass(const cMyClass & MyClass); // Copy constructor. cMyClass(const int i); // Overloaded constructor. ~cMyClass(); // Destructor. private: // Member variables. int m_int; // An integer. int * m_pInt; // A pointer to an integer. }; // Default constructor. cMyClass::cMyClass(): // Colon indicates initialisation stage. m_int( 0 ), // Comma-separated list of members to be initialised. m_pInt( NULL ) // Optional; m_pInt will be initialised in the body. { // Body of the constructor: // m_int is currently initialised to the value zero. // m_pInt currently points to NULL. m_pInt = new int; *m_pInt = 0; // m_pInt now points to new memory containing the value zero. // Initialisation is complete! } // Copy constructor cMyClass::cMyClass(const cMyClass & MyClass): // Initialisation m_int( MyClass.m_int ), // Member list m_pInt( NULL ) { // m_int is initialised to the value of MyClass.m_int // m_pInt currently points to NULL. m_pInt = new int; // Allocate new memory. *m_pInt = *MyClass.m_pInt; // Deep-copy the value. // m_pInt now points to a copy of the value pointed to by MyClass.m_pInt. // Initialisation is complete! } // Overloaded constructor cMyClass::cMyClass(const int i): // Initialisation. m_int( i ), // Member list. m_pInt( NULL ) { // m_int is initialised to the value of i. // m_pInt currently points to NULL. m_pInt = new int; *m_pInt = 0; // m_pInt now points to new memory containing the value zero. // Initialisation is complete! } // Destructor. cMyClass::~cMyClass() { // Clean up allocated memory. if( m_pInt ) delete m_pInt; }


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.


The default constructor of a stack is empty because the default value of any container, including a linked list, is an empty container which requires no arguments (all members default to zero).


Constructors allow class designers to initialise class attributes at the point of instantiation (whenever an object of the class is created). All classes must have at least one constructor other than the copy and move constructors otherwise it would be impossible to instantiate objects of the class. Copy and move constructors are not required but are generated automatically by the compiler unless the class designer explicitly marks them deleted from the class definition. The copy and move constructors allow new instances to be constructed from existing instances. The only difference between the two is that the move constructor transfers ownership of the member attributes, rather than merely copying them. In classes that contain pointers, the compiler-generated copy constructor only copies the pointer (a shallow copy) not what it points at (a deep copy). Thus class designers must provide a copy constructor in order to deep copy any unshared memory resources. Derived class constructors automatically invoke base class constructors, so classes are always constructed from the bottom up. Copy and move constructors automatically invoke their base class copy and move constructors, respectively, however all other constructors automatically invoke their base class default constructors, which may not be the most efficient method of construction. Thus class designers can invoke specific base class constructors through the constructor's own initialisation list (which is also used to initialise member attributes). Constructor bodies do not require any code except in those cases where initialisation is not possible with the initialisation list alone, however this is usually an indication of poor design choices rather than a limitation of the initialisation list. The initialisation list provides the most efficient mechanism for class initialisation. Every class must have one (and only one) destructor. If one is not provided by the class designer, the compiler generates one for you. However, unless the class contains a pointer to unshared memory, there is no need to define your own. Otherwise you must provide a destructor in order to release the memory. The destructor is the last chance to do so before an object of the class falls from scope. Classes that are intended to act as base classes must provide a virtual destructor (that is, the lowest base class must declared the destructor virtual). This ensures that if an object is destroyed polymorphically (via a pointer to one of its base classes), the most-derived destructor is always invoked first. Destruction of hierarchies is always top down. Note that destructors are not virtual by default for the simple reason that not all classes are intended to act as base classes. The containers provided by the Standard Template Library (STL) are a good example of this as none of the containers have virtual destructors -- so they cannot be used as base classes. Note that if any class method is declared virtual, the destructor must also be declared virtual. However, when inheriting from a base class with a virtual destructor, the class destructor is implicitly virtual (as are all virtual functions inherited from base classes) and does not need to be specified, although its good practice to explicitly include it anyway.


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.


To construct a derived object we invoke the appropriate derived class constructor, just as we would for any non-derived object. However, every class constructor has an implicit initialiser list and this provides the means by which we can initialise member data efficiently (at the point of instantiation), but we can also use the initialiser list to invoke specific base class constructors where required. If we do not specify a base class constructor in the initialiser list, the base class default constructor will be invoked implicitly. But if there is no base class default constructor, or we wish to invoke a base class constructor other than the default, then we must specify it explicitly. Given the following base class: class A { int value; public: A (int val = 0) value {val} {} // default constructor A (const A& other): value {other.value} {} // ... }; A derived class might be defined as follows: class B : public A { public: B (int val = 0): A {val} {} B (const B& other): A {other} {} // ... }; Note how B's default constructor explicitly invokes A's default constructor, while B's copy constructor explicitly invokes A's copy constructor.


The only "feature" of a derived class constructor is that it can, optionally, call any specific base class constructor via the derived class constructor initialisation list, as shown in the following example: class base { public: // or protected constructors base():m_data(0){} base(const base& rhs):m_data(rhs.m_data){} base(const int i):m_data(i){} }; class derived { public: derived():base(){} // calls the base class default constructor derived(const derived& rhs):base(rhs){} // calls the base class copy constructor derived(const int i):base(i){} // calls the base class overloaded constructor }; Note that the initialisation list of a constructor is the most efficient method of initialising member variables. If you choose to initialise members from the body of the constructor, you are effectively initialising the members twice, once via the implicit initialisation list, and then again via explicit initialisation in the body of the constructor. By using explicit initialisation, you only initialise once, at the point of instantiation. By explicitly invoking base class constructors via the initialisation list, you achieve the same efficiency with base class construction. If you do not specify a base class constructor, the default constructor is executed implicitly, which may not be the most efficient method of initialising the base class, especially if you then have to call other base class methods in order to initialise it properly, before initialising your derived class. There will be times when the initialisation list is not sufficient to initialise your classes properly, but you should always try to do as much initialisation as you possibly can via the initialisation list, to ensure the most efficient construction possible.


Yes. Depending on the signature (argument list) there can be several different constructors. The default constructor takes no arguments, the copy constructor takes one argument of class type, and the conversion constructor takes one argument of non-class type. Other constructors take more than one argument. Any constructor can have default parameters, making it possible for different types of constructors to share the same code body.


Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples: class Car { Car() { } Car(String s) { } } The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments.


EngineerArchitectInterior DesignerTechnicianConstructorand many more...


Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples: class Car { Car() { } Car(String s) { } } The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments


Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples: class Car { Car() { } Car(String s) { } } The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments. Because there's no code in the no-arg version, it's actually identical to the default constructor the compiler supplies, but remember-since there's already a constructor in this class (the one that takes a string), the compiler won't supply a default constructor. If you want a no-arg constructor to overload the with-args version you already have, you're going to have to type it yourself, just as in the Car example. Overloading a constructor is typically used to provide alternate ways for clients to instantiate objects of your class. For example, if a client knows the Car name, they can pass that to a Car constructor that takes a string. But if they don't know the name, the client can call the no-arg constructor and that constructor can supply a default name.


Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples: class Car { Car() { } Car(String s) { } } The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments. Because there's no code in the no-arg version, it's actually identical to the default constructor the compiler supplies, but remember-since there's already a constructor in this class (the one that takes a string), the compiler won't supply a default constructor. If you want a no-arg constructor to overload the with-args version you already have, you're going to have to type it yourself, just as in the Car example. Overloading a constructor is typically used to provide alternate ways for clients to instantiate objects of your class.


Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples: class Car { Car() { } Car(String s) { } } The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments. Because there's no code in the no-arg version, it's actually identical to the default constructor the compiler supplies, but remember-since there's already a constructor in this class (the one that takes a string), the compiler won't supply a default constructor. If you want a no-arg constructor to overload the with-args version you already have, you're going to have to type it yourself, just as in the Car example. Overloading a constructor is typically used to provide alternate ways for clients to instantiate objects of your class. For example, if a client knows the Car name, they can pass that to a Car constructor that takes a string. But if they don't know the name, the client can call the no-arg constructor and that constructor can supply a default name


You cannot actually call any constructor, you can only invoke construction, either by instantiating a static instance of a class or by dynamically creating one with the new operator. In the case of base class construction via a derived class, the base class constructor is invoked by the derived class' initialisation list. Every class constructor has an initialisation list whether you define one or not (the compiler will generate one automatically if you don't). When you derive one class from another, the derived class initialisation list invokes a call to the base class default constructor. The only exception is the compiler-generated copy constructor which automatically calls the base class copy constructor. If you define your own initialisation list, then you can explicitly invoke any base class constructor overload, thus making your construction code all the more efficient. However, copy constructors should always invoke the base class copy constructor, so if you define a copy constructor, you must explicitly invoke the base class copy constructor -- the compiler will not invoke it implicitly from a user-defined copy constructor. While many programmer's use the constructor's body to initialise a class, this is highly inefficient. Even if you don't specify an initialisation list, one is created for you, resulting in every base class and every member variable being initialised twice, which can quickly add up to a substantial cost in performance. The constructor's body should only really be used for initialisation when it would be difficult or impossible to do so from the initialisation list. Remember that your object doesn't physically exist until initialisation is complete, so you may not have access to some members, particularly base class members, at certain points in the initialisation process. Initialisation must be done from the ground up, starting with the base classes and ending with the actual class members, and all in the order they were declared. Note that only direct base classes (or virtual base classes) should be invoked from the initialisation list. The base classes themselves should invoke their own base class constructors, if they have any. Thus no matter which derivative you construct, the least-derived class is always constructed first.


We use the class constructor initialiser list to transfer values to the base class constructor.Consider the following class definitions:class A {private:int m_x;int m_y;public:A (int, int);// ...};class B : public A {B (int, int);// ...};Class A has two data members. Being built-in types, we must initialise those members during construction otherwise they'd be left in an uninitialised state. We could initialise them via the constructor body:A::A (int x, int y) {m_x = x;m_y = y;}However, this is inefficient because both m_x and m_y must exist before we enter the constructor body, so we are actually instantiating both variables first and then initialising them via assignment. Although this costs very little when initialising built-in types, for more complex types initialisation through assignment can add an unacceptable overhead.It is always more efficient to initialise variables (including member data) at the point of instantiation as soon as we have a value to assign to them. We achieve that in class constructors by using an initialiser list:A::A (int x, int y): m_x {x}, m_y {y} {}The constructor initialiser list begins with a colon (:) and is followed by a comma-separated list of initialisers, followed by the (now empty) constructor body. Initialiser lists can also be used to initialise base classes, thus the B class constructor can be implemented in the same way, as follows:B::B (int x, int y): A {x, y} {}Note how the initialiser, A {x, y}, invokes the public base class constructor A::A (int, int).


1)It has same name as that of class 2)It does not return any data type even void.


You can overload instance methods and constructors (ref. Prog. Logic)


The constructor initialisation list is the most efficient method of initialising class members, including primitive members. You use it always and without exception to ensure efficient construction. Consider the following: class foo { int m_data; public: foo(int data=0){ m_data=data; } foo(const foo& f){ m_data=f.m_data; } }; This class employs inefficient construction technique by initialising the member variables in the constructor bodies rather than the initialisation list. In other words, m_data is instantiated and initialised in two stages instead of one. It is not unlike the following two stage initialisation: int m_data; m_data =0; A more efficient way of writing these lines is to initialise at the point of instantiation using a compound statement: int m_data=0; The same applies to class construction. By using the initialisation list, member variables are initialised at the point of instantiation, as per the following: class foo { int m_data; public: foo(int data=0):m_data(data) {} foo(const foo& f):m_data(f.m_data) {} }; You will note that primitive members use object-oriented syntax in the initialisation list. This is not merely sugar-coating -- it applies to all members, including base classes. This makes it possible to invoke specific base class constructors, rather than the default constructor that would be implicitly invoked (or the base class copy constructor in the case of a derived class copy constructor). Consider the following: class base { int m_data; public: base(int data=0):m_data(data) {} base(const base& b):m_data(b.m_data) {} }; class derived : public base { public: derived(int data=0):base(data) {} derived(const derived& d):base(d) {} }; Note how the derived class default constructor invokes the base class default constructor, but passes the data argument to it. Had you not used the initialisation list you'd have to construct base, then derived, and then initialise the base class member via a setter. Clearly the initialisation list is more efficient because the base class can be initialised before derived is even instantiated, which cannot happen until the base class is fully-instantiated. Note also that the copy constructor specifically invokes the base class copy constructor. Although the argument, d, is not a base class, it is a type of base class, thus there is no need to upcast the argument. Ultimately, it can be seen that the constructor bodies are largely redundant. You still need a constructor body, even an empty one, but most of the time you will use the body for debug trace code. There will be times, however, where the class initialisation list cannot fully initialise a member because one or more arguments require more complex processing (such as calling a complex setter). In this case, you must use the constructor body. However, you must still perform as much initialisation as is possible via the initialisation list to ensure the most efficient construction method. Only those members that require additional processing need not be initialised, but it costs nothing to initialise them to an arbitrary or default value. The initialisation list is vital in the case of the copy constructor, as passing an object to a function by value will automatically invoke the copy constructor. If any member variables are embedded objects, or the object is derived from one or more base classes, then their copy constructors will be invoked as well. Therefore it pays to make construction as efficient as possible when copying objects. However, the same applies to any constructor. The more efficient the constructor performs, the better your programs will perform, especially in complex hierarchies where objects may be deeply embedded within other objects that are themselves embedded.


No, a virtual constructor would not only be meaningless, it would be ambiguous. The implication is that calling a base class constructor would somehow call a derived class constructor -- but how is the compiler to determine which derived class it is meant to construct? Derived objects can only be constructed by calling the derived object's constructor directly. In so doing, all base class constructors will be called in sequence, from the least-derived class upwards. Only when all the base classes are fully instantiated can the derived object itself be instantiated. If you need to call specific base class constructors in order to pass parameters to them, you can do so via the derived object's initialisation list(s).


1. You need not code them explicitly. Java will automatically place a default constructor 2. You can pass arguments to the constructor 3. They can return only an object of type of that class 4. They can be made private 5. They would be executed always (Everytime a class is instantiated)


The least-derived base classes are always constructed first, in the order specified by the derived class inheritance list. The most-derived class (the one you are actually instantiating) is always constructed last. Destruction is basically the reverse of construction. However, base class destructors must be declared virtual to ensure that the most-derived class destructor is called first, regardless of which class destructor is actually invoked. That is, if you hold a pointer to a base class that has no virtual destructor, deleting that pointer will only destroy the base class, not the derived class, leaving the derived class in an invalid state (because it no longer has an underlying base class) and with no way to recover the memory it consumes. It is important to remember that if you declare any virtual methods within a class, you must also declare the destructor virtual. A class without a virtual destructor is not intended to be derived from. If it has virtual methods, but no virtual destructor, it is not well-formed and must not be used as a base class.


What is the difference between a list and an outlin?



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.