answersLogoWhite

0

A default constructor is one where no arguments are declared or required. Thus if all arguments have defaults then it is still a default constructor, but one that can also serve as an overloaded constructor.

Consider the following which has two constructors, one with no arguments (the default) and one with two arguments (an overloaded constructor):

struct A

{

A () : x (42), y (3.14) {}

A (const int a, const double b) : x (a), y (b) {} int x;

double y;

// ...

};

We can invoke these two constructors as follows:

A a; // invokes default constructor (a.x is 42, a.y is 3.14).

A b (0, 1.0); // invokes overloaded constructor (b.x is 0, b.y is 1.0).

Since both constructors are essentially doing the same thing, they can be combined into a single constructor -- we simply make the 'magic numbers' the default values of the overloaded constructor:

struct A

{

A (const int a=42, const double b=3.14): x (a), y (b) {}

int x;

double y;

// ...

};

A a; // a.x is 42, a.y is 3.14.

A b (0, 1.0); // b.x is 0, b.y is 1.0.

As far as the calling code is concerned, nothing has changed, but the class declaration is simplified by removing a redundant constructor.

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

What are the similarities between constructor overloading and function overloading?

The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.


What is the difference between argument and function in Excel?

A function is a built-in formula in Excel designed to do a particular task. Arguments are values that you provide to a function to do its task, like cell address that hold numbers that you want to use for the function. For example, the numbers and addresses in the following SUM function are arguments: =SUM( A2:A5, 7, 800, B18 )


Example of fundamental difference between a polynomial function and an exponential function?

fundamental difference between a polynomial function and an exponential function?


What is the difference between over-loading and over-riding in cpp?

Overloading and overriding do similar things but they are distinct. When you override a function, you are providing a new implementation of a function that was inherited from a base class. The signature of an override must be covariant with the base class signature. Overloading means to create several functions with the same name within the same namespace but with different signatures.


What is the difference between polymorphism and method overloading in java?

The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.Overriding and Overloading are two techiques to achive polymorphism in Java.Method overloading: When a method in a class having the same method name with different arguments is said to be method overloading. Method overriding : When a method in a class having the same method name with same arguments is said to be method overriding.


Difference between normal function inline function?

gffg


What is the difference between the structure of an organ and function of an organ?

the difference is ,a structure is where it is placed and the function is what it daos


What is the difference between the function of an organ and the structure of an organ?

the difference is ,a structure is where it is placed and the function is what it daos


What is the difference between the structure of an organ and the function the organ?

the difference is ,a structure is where it is placed and the function is what it daos


What is the difference between function and recursive function?

I will explain in the easiest way the difference between the function and recursive function in C language. Simple Answer is argument of the function is differ but in the recursive function it is same:) Explanation: Function int function(int,int)// function declaration main() { int n; ...... ...... n=function(a,b); } int function(int c,int d) { ...... ...... ...... } recursive Function: int recursive(int,int)// recursive Function declaration main() { int n; ..... ..... ..... ..... n=recursive(a,b); } int recursive(int a,int b) { ..... .... .... .... } Carefully see, In the recursive Function the function arguments are same.


What is the difference between compile time and run time polymorphism?

Runtime prolymorphism means overriding compiletile polymorphism means overloading


What is the difference between a formula and a function?

A function has no repeated x values