#include <iostream>
using namespace std;
class cls1 {
protected:
int x;
public:
cls1() {
x = 13;
cout << "Constructor for cls1 was called\n";
}
};
class cls2 : public cls1 {
int y;
public:
cls2() {
y = 15;
cout << "Constructor for cls2 was called\n";
}
int f(cls2 ob) {
return (ob.x + ob.y);
}
};
int main()
{
cls2 ob;
cout << ob.f(ob);
return 0;
}
The code above prints:
constr1
constr2
28
Why isn't the constructor for cls2 called twice since, in the function f, the object is passed by value instead of reference, meaning a new temporary object is created?