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.
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.
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.
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).
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.
Yes. At least in Java, that's possible, as long as the constructors have a different number, or different types of, parameters.
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.
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.
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).
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.
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 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.
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
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.
Yes. At least in Java, that's possible, as long as the constructors have a different number, or different types of, parameters.
Yes, you can have more than one constructor with a different set of parameters.
You can have any number of constructors for a class. All we need to do is implement constructor overloading. Ex: let us say we want to create multiple constructor for a class Test Public class Test { Public Test() { //code } Public Test(int vals) { //code } Public Test(String val) { //code } }
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.