C++ Programming - Functions - Discussion

Discussion Forum : Functions - Programs (Q.No. 26)
26.
What will be the output of the following program?
#include<iostream.h> 
class IndiaBix
{
    public: 
    int x, y;
    IndiaBix(int xx = 10, int yy = 20)
    {
        x = xx;
        y = yy; 
    }
    void Exchange(int *, int *);
};
int main()
{
    IndiaBix objA(30, 40); 
    IndiaBix objB(50); 
    objA.Exchange(&objA.x, &objB.y); 
    cout<< objA.x << " " << objB.y << endl; 
    return 0;
}
void IndiaBix::Exchange(int *x, int *y)
{
    int t;
    t  = *x;
    *x = *y;
    *y = t ; 
}
20 10
30 20
20 30
30 40
50 30
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
2 comments Page 1 of 1.

Keerthu said:   1 decade ago
objA -> x=30 y=40 objB x=50 y=20.
objA.x is 30 and objB.y is 20.

So objA.Exchange(&objA.x, &objB.y);
Is objA.x=20 objB.y=30.

Chanchla said:   9 years ago
When we pass value through object first part will contain 30 replays 10 by 30 as 1 value and in and call, there is no b y value so it takes its value from constructor.

We call onjA(30,20); in the function value exchange because it is call by reference.

Post your comments here:

Your comments will be displayed after verification.