What will be the output of the following C++ code? [code] #include <iostream> using namespace std; class Integer {

Nguyên Lê

New member
What will be the output of the following C++ code?
Code:
    #include <iostream>
    using namespace std;
    class Integer 
    {
        int i;
        public:
        Integer(int ii) : i(ii) {}
        const Integer
        operator+(const Integer& rv) const 
        {
            cout << "operator+" << endl;
            return Integer(i + rv.i);
        }
        Integer&#038;
        operator+=(const Integer&#038; rv) 
        {
            cout << "operator+=" << endl;
            i += rv.i;
            return *this;
        }
    };
    int main() 
    {
        int i = 1, j = 2, k = 3;
        k += i + j;
        Integer ii(1), jj(2), kk(3);
        kk += ii + jj;
    }
A.
operator+
operator+=
B.
operator+=
operator+
C.
operator+
operator+
D.
operator+
operator=
 
Answer: a
Explanation: We are using two operator functions and executing them and the result is printed according to the order.
Output:
Code:
$ g++ oper2.cpp
$ a.out
operator+
operator+=
 

Tìm kiếm

Back
Top