Computer Science - Object Oriented Programming Using C++ - Discussion

Discussion Forum : Object Oriented Programming Using C++ - Section 1 (Q.No. 10)
10.
A C++ program contains a function with the header int function(double d, char c). Which of the following function headers could be used within the same program?
char function(double d, char c)
int function(int d, char c)
both (a) and (b)
neither (a) nor (b)
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
12 comments Page 1 of 2.

Rohit Raj said:   1 year ago
@All.

In C++, function overloading allows multiple functions with the same name but different parameter lists.

For the given function header `int function(double d, char c)`, here are some examples of function headers that could be used within the same program:

1. `int function(double d, char c);` (Same as the original)
2. `int function(int i, char c);` (Different parameter type for the first argument)
3. `double function(double d, char c, int i);` (Additional parameter)
4. `int function(char c, double d);` (Different order of parameters)
5. `int function(double d);` (Different number of parameters)

These are just a few examples, and there are more possibilities depending on the types and number of parameters you want to use. Ensure that the function headers differ in terms of the number or types of parameters to avoid ambiguity.

Ajit said:   1 decade ago
The right answer here should be (B). As the compiler differentiates the functions with same name based on.

I) No.of arguments.

II) Type of arguments.

The return type is not a taken into account in case of function overloading.

Thus (A) is ruled out.

Manash E B K said:   10 years ago
In function overloading function names are same for different functions but all these same named function should have different arguments and type of arguments and return type should be same. That's why answer is B.

Deb said:   10 years ago
It uses the concept of function overloading which states that there could be multiple no. of functions with the same name, but the signature list must be different (the no. of and type of parameters).
(1)

Pinky said:   1 decade ago
Because function overloading is according to the change in parameter not in return type., here char function() contain same data type so it is not accepted.

Qama Balti said:   9 years ago
I think here is the case of overriding not overloading guys.

Because in overriding type f.name and parameter should be same.

Yasvanth said:   9 years ago
In function overloading concept the function name is same but the parameters passing through it are different.
(1)

Nishtha said:   1 year ago
But the return type is different in A) then it cannot say overloaded.

Can anyone clarify it to me?

Rohit said:   1 decade ago
Double can take 4 byte, but int can take only 2 byte.

Gautam said:   1 decade ago
Its just like function overloading.


Post your comments here:

Your comments will be displayed after verification.