answersLogoWhite

0


Best Answer

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

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

Wiki User

12y ago

In function overloading, for different tasks there should be different blocks of codes with the same name to perform different tasks, while default arguments has only one block of code and perform only one task.

Function overloading take different types & numbers of argument to perform different tasks, But default arguments perform only one task which has been declared or written for it without weather its called with exact number of parameters called with less number of parameters. but it will perform the same task.

Function overloading take the exact number of parameters to work, But default arguments can take exact number of parameter or less number of parameters or even no parameters. it will assign the default values to the parameters and perform the task.

Hope it will help you,

Please correct if there was mistake.

RAMIN SADAT, South Asian University (MSC)

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Difference between function overloading and default arguments?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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 difference between strcmpi and stricmp?

The strcmpi() function is identical to stricmp() function.


What is the difference between static function and global static function?

'global static'?! There is no such thing.


What is the difference between function prototyping and function overloading?

Function prototypes determine the return type, the name of the function, the argument types expected by the function, and the arity of the function. Function prototyping is used to separate interface from implementation. In C++ all functions must be declared before they are called, thus we use prototypes to provide forward declarations for those functions that have yet to be defined/implemented. We can also use forward declarations for incomplete types such as template functions and classes, however the definition/implementation must be visible to compiler before the function or class is used. In these cases the definitions are typically placed in the same header as the declarations. It is important to note that a definition is also a declaration, and therefore both are also prototypes. The only real difference is that prototypes do not require names for the formal arguments. Even if you provide argument names in your prototypes, they will be ignored by the compiler. The argument names within the definition are the only names of any relevance. Function overloading is where two or more functions share the same name within the same namespace, but have different signatures. The signature of a function is essentially the same as its prototype but excludes the return type, thus overloads cannot differ by return type alone. The compiler uses the function signature to differentiate between your overloads. All function signatures within a namespace must be unambiguous, thus you cannot have two functions with the same name and arguments that are co-variant. For example, the following overloads are invalid because a size_t type is co-variant with unsigned int type, thus the compiler cannot differentiate them. unsigned int max(unsigned int, unsigned int); size_t max(size_t, size_t);


What is the difference between function and use?

what is the difference between function and use? I came across with this problem while I am doing my bilogy home work for instace what is the use of glucose ?and second one is what is the function of glucose? so is for protein

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 )


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.


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 compile time and run time polymorphism?

Runtime prolymorphism means overriding compiletile polymorphism means overloading


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.


Difference between normal function inline function?

gffg


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 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 a formula and a function?

A function has no repeated x values