answersLogoWhite

0


Best Answer

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

User Avatar

Wiki User

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

Wiki User

12y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

You overload a constructor by declaring two or more constructors in a class, each with different signatures.

When no constructor is specified, a default constructor and a copy constructor are implied. Both can be overridden. The default constructor can also be overloaded if all arguments are given default values in the declaration.

You can add as many construction overloads as required in order to initialise your class. If your class has many members to initialise, it may be helpful to use a structure and an overloaded constructor to accept the structure.

For every constructor that has exactly one argument, there should also be an equivalent assignment operator overload. The copy constructor assignment overload is implied if not specified.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

That depends on how you define body of class. If you do not define constructors then compiler will provide default constructor and it is not overloaded. If you create your own constructor you automatically overload default constructor.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

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.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the method of constructor overloading in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Can you write own constructor in c plus plus?

Yes.


What is the difference between constructor and friend function in c plus plus?

A constructor is a method that fires when the object is instantiated. A friend function is a function that has special access to the object. They are two different types of things, and cannot be further differenced.


True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


What is the difference between implicit and explicit call of constructor in c plus plus?

An implicit constructor call will always call the default constructor, whereas explicit constructor calls allow to chose the best constructor and passing of arguments into the constructor.


Can you declare a method within a method in c or c plus plus?

C: there are no methods in C. C++: no.


Difference between Method overloading and method overriding in C plus plus?

Yes. Any base class method that is declared virtual can be overridden by a derived class. Overriding a method that is not declared virtual can still be called, but will not be called polymorphically. That is, if you call the base class method, the base class method will execute, not the override. To call a non-virtual override you must call it explicitly.


Can constructor be declared as constant in c plus plus?

No. Constructors initialise objects and, by definition, must be able to modify the member variables. Uninitialised members are a disaster waiting to happen even without a constructor declared const! Thankfully, the compiler won't permit a const constructor.


Is it possible to do operator overloading in c?

No. Operator and/or function overloading is only a C++ thing.


Why use new and delete operator overloading in c plus plus?

one reason to use new and delete operator overloading in c++ is when you are using your own memory manager code. when the user of your code calls the new keywork, your memory manager code can allocate memory.


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.


How can a constructor be invoked at the time of inheritance in C Plus Plus?

It cannot. Inheritance is a compile-time operation. Constructors are invoked at runtime at the point of instantiation.


How do you invoke the constructor function in c plus plus?

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.