Difference between function overloading and constructor overloading in c++
Answers
ʜᴇʀᴇ ɪs ʏᴏᴜʀ ᴀɴsᴡᴇʀ
Functions are the building blocks and Constructor is a special method that have same name as that of class. Constructors are invoked at the time of object creation.
Constructor Overloading
class Add
{
Add(int x, int y)
{
int c=x+y;
System.out.println(c);
}
Add(int x,int y,int z)
{
int c =x+y+z;
System.out.println(c);
}
public static void main(String[] argus)
{
Add ad=new Add(10,20);
Add ad1=new Add(10,20,30);
}
}
output-:
30
60
Method Overloading
When a class has two or more methods with same name but with different parameter list is known as method overloading.
class Add
{
void add(int x, int y)
{
int c=x+y;
System.out.println(c);
}
void add(int x, int y, int z)
{
int p=x+y+z;
System.out.println(p);
public static void main(String[] argus)
{
Add ad=new Add();
ad.add(10,20);
ad.add(10,20,30);
}
}
Output
30
60
ᴇʜsᴀss ^_^)!!
Answer:
In short Function overloading is used for different function with same name but different parameter and Constructor overloading is used for different constructor with same name but different parameter.
Explanation:
Function overloading in C++ allows multiple functions with the same name to exist in the same scope, but with different parameter lists. This means that a single function name can be used to perform different tasks based on the number or type of arguments passed to it.
Constructor overloading, on the other hand, is a feature that allows multiple constructors to exist in a class with the same name but with different parameter lists. This allows objects of a class to be initialized in different ways depending on the arguments passed to the constructor.
More questions and answers
https://brainly.in/question/53687108
https://brainly.in/question/50192228
#SPJ3