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

Đăng Khánh

New member
What will be the output of the following C++ code?
Code:
    #include <iostream>
    using namespace std;
    class sample 
    {
        public:
        int x, y;
        sample() {};
        sample(int, int);
        sample operator + (sample);
    };
    sample::sample (int a, int b) 
    {
        x = a;
        y = b;
    }
    sample sample::operator+ (sample param) 
    {
        sample temp;
        temp.x = x + param.x;
        temp.y = y + param.y;
        return (temp);
    }
    int main () 
    {
        sample a (4,1);
        sample b (3,2);
        sample c;
        c = a + b;
        cout << c.x << "," << c.y;
        return 0;
    }
A. 5, 5
B. 7, 3
C. 3, 7
D. 3, 5
 
Answer: b
Explanation: In this program, we are adding the first number of a with first number of b by using operator function and also we are adding second number by this method also.
Output:
Code:
$ g++ oper.cpp
$ a.out
7, 3
 

Tìm kiếm

Back
Top