Question

What is printed by the following program? (read the code carefully) #include <iostream> using namespace std;...

What is printed by the following program?

(read the code carefully)


#include <iostream> using namespace std; class A { public: int x; }; int main() { A *a = new A; A *b = new A; a->x=1;
b->x=1; if (a == b) { cout << "black"; } else { cout << "white"; } delete a; delete b;
return 0; }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;


class A {
public:   
int x;
};

int main() {
A *a = new A;//creating object
A *b = new A;//creating object
a->x=1; //setting value 1
b->x=1;//setting value 1
if (a == b) //a==b returns false/// since we are comparing two different objects
   {
cout << "black";
} else {//so white is printe
cout << "white";
}
delete a;//clearing memory
delete b;
return 0;
}

output"

white

Add a comment
Know the answer?
Add Answer to:
What is printed by the following program? (read the code carefully) #include <iostream> using namespace std;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT