C++ Programming - Functions - Discussion

Discussion Forum : Functions - Programs (Q.No. 24)
24.
What is correct about the following program?
#include<iostream.h> 
class Addition
{
    int x; 
    public: 
    Addition()
    {
        x = 0;
    }       
    Addition(int xx)
    {
        x = xx;
    }
    Addition operator + (int xx = 0)
    {
        Addition objTemp; 
        objTemp.x = x + xx; 
        return(objTemp);
    }
    void Display(void)
    {
        cout<< x << endl;
    }
};
int main()
{
    Addition objA(15), objB;
    objB = objA + 5;
    objB.Display();
    return 0; 
}
The program will print the output 20.
The program will report run time error.
The program will print the garbage value.
Compilation fails due to 'operator +' cannot have default arguments.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
3 comments Page 1 of 1.

Rahul said:   1 decade ago
Because rule is that overloaded operator cannot have default argument.
(1)

Abhishek said:   1 decade ago
Can anyone explain me operator + can not have default argument.

Rohan said:   2 years ago
Addition operator + (int xx )
{
Addition objTemp;
objTemp.x = x + xx;
return(objTemp);
}

Now this fun will run because it does not have any default argument.

NOTE: When we overload an operator it should not contain any default argument.

Post your comments here:

Your comments will be displayed after verification.