What is the output of the following C++ program?
[gamipress_button type=”submit” label=”Run Code Online” onclick=”window.open(‘https://coderseditor.com’,’_blank’)”]
#include <iostream> using namespace std; class A { public: A& operator=(const A&a) { cout << "A's assignment operator called" << endl; return *this; } }; class B { A a[2]; }; int main() { B b1, b2; b1 = b2; return 0; }
Studently Answered question October 10, 2022
Output
A’s assignment operator called
A’s assignment operator called
There is no user defined assignment operator for class B. The compiler creates a default assignment operator if we don’t write our own. All of the members of the right side object are copied one by one to the left side object by the default assignment operator. There are two class A members in the class B. There are two calls to the assignment operator because they are both copied in the “b1 = b2” statement.
Studently Answered question October 10, 2022