Examine the following C++ program, in which a wrapper Int class is defined that
ID: 3630813 • Letter: E
Question
Examine the following C++ program, in which a wrapper Int class is defined that contains an overloaded += operator. What is the output of this program?#include <iostream>
using namespace std;
class Int
{
public:
Int(int value) {this->value = value;}
Int& operator+=(const Int& anInt)
{value += anInt.value; return *this;}
void doubleIt() {value *= 2;}
friend ostream& operator<<(ostream& out, const Int& anInt)
{return out << anInt.value;}
private:
int value;
};
int main()
{
Int i(5), j(6);
(i += j).doubleIt();
cout << i << endl;
return 0;
}
Notice that the overloaded += operator returns a reference. Change the += operator to return a value instead and determine what the output would be in that case. Explain the difference in behavior and why the ability to return by reference is essential to be able to implement such operators.
Please explain indepthly! Thanks!
Explanation / Answer
Hi before changing the returning type for the ovrloaded function the value is 22 after changing it 11 if u made the function return by value it will not make any difference coz the value returned will not be saved in the sent variable as a parameter
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.