Top Answer
User Avatar
Wiki User
Answered 2010-08-18 02:18:16

Yes, you can use, and often you should, use more than one constructor in a class in C++.

Normally, there is a default constructor, a copy constructor, and one or more conversion constructors. Sometimes, there are also other constructors, overloaded based on argument signature, if there are complex situations.

User Avatar

Your Answer

Related Questions

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.

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

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.

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.

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.

Constructor is not an alternative to class. In Java, you create classes; the classes contain methods - including the constructor, which can be viewed as a special method. If you want to have a constructor, you need a class that surrounds it, so it's not one or the other.

A constructor is not a function. A function is a type, as specified by its return type, and must return a value of that type unless the type is void. A constructor does not return anything, not even void. The purpose of a constructor is to both allocate and initialise memory for an object of the type being constructed. If a valid object cannot be constructed for any reason, the constructor must throw an exception. If the object's class has no data members (attributes), the class does not require a constructor. This is typically the case for most abstract data types and base classes which are used purely as interfaces. Constructors differ from functions in that all constructors have an initialisation section that is used specifically to initialise non-static data members. The body of the constructor is rarely used except to perform initialisations that cannot be more easily performed by the initialisation section. A class may have more than one constructor to provide alternative methods of construction based upon the number and type of arguments supplied (if any). When no arguments are required or all arguments have default values then the constructor is known as the default constructor. If the constructor has only one argument the constructor is known as a conversion constructor (because the argument is converted to an object of the class). However, if the constructor argument is a constant reference to an object of the same class, then it is known as a copy constructor, and when the constructor argument is an rvalue reference, it is known as a move constructor. If copy and/or move constructors are provided for a class, the equivalent assignment operators should also be provided for that class. All other constructors are known as user-defined constructors.

A constructor is a class method which initialises an object of the class at the point of instantiation. Specifically, it initialises the base classes (if any) and the non-static data members (if any). Constructors also play a central role in the resource acquisition is initialisation (RAII) paradigm. Objects which have a natural default value have a default constructor. The default constructor is a constructor that has no arguments or where all arguments have default values. Objects which can be copied have a copy constructor. The copy constructor has just one non-default argument, a const l-value reference of the same type as the class. Objects which can be moved have a move constructor. The move constructor has just one non-default argument, a modifiable r-value reference of the same type as the class. All other constructors that have only one argument of a type other than the class itself are known as conversion constructors. Constructors can also have more than one argument. No specific name is given to these constructors. Other than physical memory constraints, there is no limit to the number of constructors that may be defined for a class.

Yes, you can have more than one constructor with a different set of parameters.

An array of class objects is just a set of class objects arranged linearly in memory. It is no different than an array of elementary objects. You define it the same way. class myClass { ... }; myClass arrayOfMyClass[100]; // creates 100 objects and fires the constructor 100 times

yes, you can overload constructors in Java. You can have more than one constructor for a single class and each should have a different set of parameters.

Multiple constructors are overloaded versions of the class constructor. They are chosen based on their argument types. The default constructor takes no arguments. The copy constructor takes one argument of reference type to a const variable of the class. The conversion constructor takes one argument of different type than the copy constructor. Other constructors take two or more arguments and can be used for special initialization. Often, these other constructors can be built as one, using default values for arguments so, for instance, one of them could function as the default constructor if needed.

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.

There is no such thing as a constructor function in C++ (constructors have no return value, not even void, and cannot be called like regular functions). Constructors are invoked rather than called directly, either by declaring a static variable of the class type, or via the C++ new operator.

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; }

Yes. At least in Java, that's possible, as long as the constructors have a different number, or different types of, parameters.

Constructors exist purely to initialise member variables. Although you can use the constructor body to call member methods in order to perform that initialisation, this is highly inefficient, particularly when the member variable is a complex type (non-primitive). Constructors have initialisation lists specifically for the purpose of initialising member variables at the point of instantiation, which is far more efficient than initialising via the constructor body, which would result in complex types being initialised twice (once during implicit initialisation and then again during explicit initialisation in the constructor body). However, if you specify two or more constructors, each has to have its own specific initialisation list, even if those lists are largely the same for all constructors, which not only results in code duplication but also increases code maintenance. When you add a new member, you must update the initialisation lists of all constructors. Thus the advantage of constructors is increased efficiency, while the disadvantage is increased maintenance. A lot of young programmers often counter this by creating a common initialisation member function which is then called from each constructor's body. While this reduces code maintenance, it increases the inefficiency. There's nothing wrong with having a common initialisation function, but you shouldn't call it from the constructor. Initialisation lists also allow derived classes to invoke specific base class constructors. When not specified, a derived class constructor will automatically call the base class' default constructor, which may not be the most efficient method of construction. This is particularly important when defining a copy constructor. In this case you will want to call the base class copy constructor, not the base class default constructor, in order to maintain efficiency. It should be noted that when you do not declare any constructors, the compiler will generate both a default constructor and a copy constructor, both of which will call the appropriate base class constructors if the class is derived. If you define any constructor, you will automatically lose the compiler-generated default constructor and must define your own if you require one. The copy constructor is always generated for you unless you explicitly declare your own, but you must then call the base class copy constructor explicitly, otherwise the base class default constructor is called implicitly.

A parametrised constructor is any constructor that accepts one or more arguments where the first argument has no default value. If all parameters have default values then the constructor is regarded as being a default constructor that is overloaded. The copy constructor is an example of a parametrised constructor. If you do not define your own, one is generated for you by the compiler, However, the compiler-generated copy constructor performs a member-wise copy. If your class contains member pointers to memory allocations that are owned by the class, this will result in two or more classes owning the same memory, which would prove disastrous when any one instance is destroyed (it will completely invalidate all other instances). Therefore you must provide your own copy constructor to ensure every instance of the class owns its own allocations, by deep copying the pointers (copying the memory being pointed at rather than just the pointers). Other than that, there is no requirement to provide any parametrised constructor. You only need to provide parametrised constructors when you want to provide an alternative method of construction. Note that if you declare any parametrised constructor, including a copy constructor, the compiler does not generate a default constructor for you. If you require one, then you must provide your own. The default constructor may be parametrised provided all parameters are assigned default values. As an example, consider the following simple class that has a default constructor (as well as a compiler-generated copy constructor): class simple { public: simple():m_data(0){} void setdata(const float data){m_data = data;} void setdata(const int data){m_data = ( float ) data;} private: float m_data; }; When we instantiate this class, the data member is initialised to 0. If we wish to change the data member, we must subsequently call one of the setdata() methods. However, it would be more efficient to set the data member when the class is instantiated. Therefore we should provide a parametrised default constructor: simple(float data=0):m_data(data){} We should also provide a parametrised constructor to cater for integers: simple(int data):m_data(( float ) data){} Now we no longer need to call setdata() to initialise the class as we can do it all via the constructors.

Parametrized constructors are used to increase the flexibility of a class, allowing objects to be instantiated and initialised in more ways than is provided by the default and copy constructors alone. If you define any parametrized constructor, including a copy constructor, you will lose the default constructor generated by the compiler and must declare your own if you need one. The default constructor can also be parametrized, but each parameter must include a default value in the declaration, so that it can be called without any parameters.

A static member is local to the class rather than to an object of the class. A static class is a class that cannot be instantiated. All members of a static class must be declared static and there can be no constructors, no copy or move operators and no destructor. There is no such thing as a static constructor per se. However, you can define a static method that constructs and returns an object of the class. Like so: struct A { static A* construct () { return new A; } }; int main() { A* x = A::construct(); // invoke static constructor // ... delete x; x = nullptr; } Clearly, a "static constructor" serves no practical purpose and would only lead to confusion.

Single inheritance occurs when a derived class inherits from just one base class. Multiple inheritance occurs when a derived class inherits from more than one base class.

A constructor is a special class method that is invoked whenever an object of the class is instantiated. The constructor is used to initialise the object such that when construction is complete, a valid object exists. Constructors cannot be called directly since they are not functions (they have no return value, not even void), however constructors can accept arguments in the same way functions can. Constructors are invoked, rather than called.

A static member variable is local to the class rather than to an object of the class.

There are three basic constructors: the default constructor (one that accepts no arguments or where all the arguments have default values); the copy constructor and the move constructor. But you can also have any number of overloaded constructors over and above these three. There are no limits to the number of constructors you can define, other than those imposed by the architecture (e.g., memory restrictions).

A constructor is a code segment that is invoked automatically whenever you instantiate an instance of a class. All non-primitive objects have at least one constructor but class designers can create as many constructors as required, giving consumers the freedom to construct objects more efficiently. If you don't declare any constructors, the compiler will generate both a default constructor (one that has no arguments) and a copy constructor for you. The purpose of a constructor is simply to initialise the class member variables. This is usually done via the class constructor initialisation list. Constructors also have a body (much like a function) which can be used to call methods of the class to perform more complex initialisations. However the initialisation list is the most efficient method, as it is not unlike initialising a primitive variable at the point of instantiation, rather than instantiating and initialising variables in two stages as you would with C. Constructors are also subject to access specifiers, but will usually be declared public unless the class is an abstract base class in which case they may be declared protected. Private access is typically used when declaring singleton classes (where construction is achieved via a static member method that instantiates the one and only instance of the class, which is itself declared a static member variable of the class). Note that copy constructors are invoked automatically whenever you pass objects to functions by value. Copying a derived object automatically invokes its base class copy constructors, while copying containers automatically invokes the embedded object copy constructors. This is why pass by reference (or via pointer) is the preferred method of passing objects into functions, as there is no need to reconstruct the object being passed.