Home>IT Tests>Questions>What does the following code display ?
class A { public: virtual void hello() {cout << "A" << endl;} };
class B : public A { public: void hello() {cout << "B" << endl;} };
void meet_value(A a1, A a2) { a1.hello(); a2.hello(); }
void meet_reference(A& a1, A& a2) { a1.hello(); a2.hello(); }
int main() { B b; A a; meet_value(a, b); meet_reference(a, b); return 0; }
Answer : "AAAB".
Question from the C++ - Fundamentals test
What does the following code display ? class A { public: virtual void hello() {cout << "A" << endl;} }; class B : public A { public: void hello() {cout << "B" << endl;} }; void meet_value(A a1, A a2) { a1.hello(); a2.hello(); } void meet_reference(A& a1, A& a2) { a1.hello(); a2.hello(); } int main() { B b; A a; meet_value(a, b); meet_reference(a, b); return 0; } Answer : "AAAB".
Hard
What does the following code display ?
class A {
public:
virtual void hello() {cout << "A" << endl;}
};
class B : public A {
public:
void hello() {cout << "B" << endl;}
};
void meet_value(A a1, A a2) {
a1.hello();
a2.hello();
}
void meet_reference(A& a1, A& a2) {
a1.hello();
a2.hello();
}
int main() {
B b;
A a;
meet_value(a, b);
meet_reference(a, b);
return 0;
}
Author: SamuelStatus: PublishedQuestion passed 26 times
Edit
1
Community EvaluationsNo one has reviewed this question yet, be the first!
Similar QuestionsMore questions about C++