answersLogoWhite

0


Best Answer

As many as are required for the class to function. All classes other than static classes require at least one constructor. Most classes will also provide copy and move constructors (with corresponding copy and move assignment operators). Additional constructors are usually provided to increase the flexibility of the class, allowing users a variety of ways to initialise objects of the class.

Constructors that accept just one argument (excluding the copy and move constructors) are regarded as being conversion constructors because, from a user's perspective, they effectively convert their argument into an object of the class.

Although there is effectively no limit to the number of constructors you can define, do keep in mind the mantra that simple concepts should be expressed simply and no simpler. Make good and proper use of inline initialisation, default arguments and delegation. Use explicit constructors to avoid narrowing issues and implicit conversions where narrowing is acceptable. Also, make good use of RAII (resource acquisition is initialisation) to ensure proper cleanup should any member fail to initialise during construction. For efficiency, perform as much initialisation as possible outside the body of the constructor. Ideally, the constructor body should contain no code at all.

User Avatar

Wiki User

7y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

A class may have any number of constructors, private or not.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

one:A class have only one default constructor.If a class doesn't contain any constructor at run time default constructor is provided by JVM.

Mugdha

This answer is:
User Avatar

User Avatar

Hamza 3mary

Lvl 2
1y ago

1

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How many constructors may be defined for a class?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

In C plus plus What function is called to initialize a class?

Class initialisation is normally handled by the class constructor(s). Every constructor has an optional initialisation section between the declaration and the body of the constructor. This is generally used to call specific base class constructors, but can be used to initialise any member variables via their own constructors. Member variables may alternatively be initialised in the body of the constructor, but this is really only necessary when member pointers need to be allocated new memory. For those classes that have many members and many constructors, the initialisation may be handled by a private member method called by each constructor in order to simplify maintenance during development. However, when the class is finalised, the private member method will generally be replaced with formal initialisation sections in each constructor.


What are the properties of class in c plus plus?

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.


How can you justify the use of constructor and destructor in c plus plus?

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.


What is the purpose of constructor in object oriented programming?

A constructor in a class is one of the first pieces of code to be executed when you instantiate a class. The purpose of constructors is to have code that initialize the class and prepare the variables that may be required by the class...


What is an object and how are they created in object oriented programming languages?

Objects are the examples of the idea which the class represents. Objects of a class are constructed using the "new" keyword. For example: class Person { String name; } class Earth { public static void main(String... args) { Person sunny = new Person(); // Creating Object sunny.name = "Sunny"; } } Here, we have a class Person, the object of this class (in this case a person having name Sunny) is created from another class Earth using the keyword "new".

Related questions

What is constructor and discuss different types of constructor with syntax and examples?

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.


Can you have virtual constructor?

The short answer is no, you cannot. The long answer is that constructors are not functions that can be called directly, and overriding a base class constructor would have no practical meaning since the derived class is itself responsible for calling its own base class constructors (whether implied by omission or explicitly via the derived class' initialisation list). Even so, the derived class isn't calling the base class constructor directly (that's why constructors have no return value; the actual call is made behind the scenes). The base class itself may be derived in which case its base class must be constructed before it can be constructed. This is the complete reverse of how a virtual function behaves, and is the reason that destructors can be virtual but constructors cannot. When a base class is destroyed, all its derivatives must be destroyed first, starting with the most-derived class of object. This can only be achieved through virtual destruction.


In C plus plus What function is called to initialize a class?

Class initialisation is normally handled by the class constructor(s). Every constructor has an optional initialisation section between the declaration and the body of the constructor. This is generally used to call specific base class constructors, but can be used to initialise any member variables via their own constructors. Member variables may alternatively be initialised in the body of the constructor, but this is really only necessary when member pointers need to be allocated new memory. For those classes that have many members and many constructors, the initialisation may be handled by a private member method called by each constructor in order to simplify maintenance during development. However, when the class is finalised, the private member method will generally be replaced with formal initialisation sections in each constructor.


Describe various members a class definition may contain?

Fields - the various variables and objects your class uses, Constructors - used to initialize instance varriables, Methods - allow you to retrieve and manipulate data in an object


What is object in oops?

An instance of a class that holds state. Operations defined by it's class (or superclasses) can be performed on it. Now you may wonder what a class is :).


What is an construtor in c plus plus oops?

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.


What are the properties of class in c plus plus?

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.


How can you justify the use of constructor and destructor in c plus plus?

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.


What are regions always defined by?

Regions may be defined by geographic features such as a river or a "Mason-Dixon" line. They may be defined as language differences or cultural traditions.


What is the purpose of constructor in object oriented programming?

A constructor in a class is one of the first pieces of code to be executed when you instantiate a class. The purpose of constructors is to have code that initialize the class and prepare the variables that may be required by the class...


What is an object and how are they created in object oriented programming languages?

Objects are the examples of the idea which the class represents. Objects of a class are constructed using the "new" keyword. For example: class Person { String name; } class Earth { public static void main(String... args) { Person sunny = new Person(); // Creating Object sunny.name = "Sunny"; } } Here, we have a class Person, the object of this class (in this case a person having name Sunny) is created from another class Earth using the keyword "new".


Who typically writes the Newsday crossword?

Everyday, a few hundred people in North America alone help to write the Newsday crosswords, therefore causing them to be known as "Constructors". These "Constructors" may be everyday people with unrelated experience/jobs.