answersLogoWhite

0

What is the role of constructor using default argument?

Updated: 4/7/2022
User Avatar

Wiki User

8y ago

Best Answer

A default constructor is any constructor that has no arguments or where all required arguments are assigned default values. The latter provides a useful means of overloading a default constructor without incurring otherwise unnecessary code duplication.

Consider the following class definition:

class complex {

double re, im;

public:

complex (): re {0}, im {0} {}

complex (double real): re {real}, im {0} {}

complex (double real, double imaginary): re {real}, im {imaginary} {}

// ...

}

Although the constructors are relatively trivial, the class definition is much more verbose than it needs to be. Essentially we have three constructors all doing exactly the same job. We could use in-class initialisation to eliminate the unnecessary default initialisations in the constructors:

class complex { double re {0}, im {0};

public:

complex () {}

complex (double real): re {real} {}

complex (double real, double imaginary): re {real}, im {imaginary} {}

// ...

}

However, because all three constructors are essentially doing the same thing, it makes sense to combine them into a single default constructor with default arguments:

class complex {

double re {0}, im {0};

public:

complex (double real=0, double imaginary=0): re {real}, im {imaginary} {}

// ...

}

User Avatar

Jaquan Kemmer

Lvl 10
2y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the role of constructor using default argument?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.


How do you get a default constructor?

The default constructor is an empty (only call the super constructor) with no parameters constructor inserted by the java compiler when you don't define a constructor in your class. If you write something like this: public class NoConstructorClass{ //no constructor goes here } Then you get something like this: public class NoConstructorClass{ public NoConstructorClass(){ // Default constructor that you didn't write super(); } }


Who has the role of an ISTG by default?

Intersite Topology Generator (ISTG), which is responsible for the connections among the sites. By default Windows 2003 Forest level functionality has this role. By Default the first Server has this role. If that server can no longer preform this role then the next server with the highest GUID then takes over the role of ISTG.


What is the role of the constructor?

Several meanings for "constructor". In object-oriented programming, it is the subroutine used to create an object. The programmer can add commands here, to get the object ready for his needs; for example, initialize the variables used in the object.


What is the role of the clincher?

The clincher summarizes and reinforces the main points of an argument or presentation. It is usually the final statement that leaves a lasting impression on the audience and helps to drive home the key message. A well-crafted clincher can inspire action or evoke emotion in the audience.


What is the role of textual evidence in an interpretive argument?

Textual evidence serves as the foundation for an interpretive argument by providing concrete support for an analysis or claim. It helps to validate interpretations and allows readers to follow the logic of the argument. Effective use of textual evidence can strengthen the persuasiveness and credibility of the overall argument.


What role did the role pope did play in in settleing the argument over land ownership?

The whole men


One argument for a strong federal government is its role in ensuring?

equality


What is a primary argument of diversity theory to explain intercultural contact?

The primary argument of diversity theory to explain intercultural contact is the role of power and how it can be unbalanced in an intercultural workplace.


What rule should you follow for the Client Access Server role?

I would suggest you to keep the default settings.


Why can't an admin user post in the site news forum in BIggyan Cloud eLearning?

* Default role for all users (in Administration > Users > Permissions > User policies) - should be set to Authenticated user* Default frontpage role (in Administration > Front Page > Front Page settings) - should be set to none or Student* That the admin user is not assigned the role of guest as a system role or a front page role or in the site news forum context


What is the role of the mens and womens work?

That is not true. women can do anything that a man can. For instance I know a couple where the women is a constructor and the manstays home and tends to the children. It would be unfair to say that women and men can't do the same things.