Output is 5500, but why not 5555?
class product {
public:
int b;
};
class item {
public:
int a;
item(product& obj)
{
cout << a;
}
item() {}
void display()
{
cout << a;
}
};
int main()
{
item obj1;
product obj2;
obj1.a = 5;
cout << obj1.a;
obj1.display();
obj1 = obj2;
//object of product class sent into Constructor of item class*
cout << obj1.a;
return 0;
}
Here constructor is called of item class and product object is pass through it.
The program has undefined behavior because the used data member
ais not initialized.This conversion constructor
that is used in this assignment statement
to convert the object
obj2of the typeproductto an object of the typeitemdoes not initialize the data membera. So the data member has an indeterminate value. And this indeterminate value assigned to the data memberaof the objectobj1is outputted in the constructor and in this statementIt occurred such a way that the memory occupied by the data member
aof the temporary object of the typeitemcontained zeroes. But in general this is not necessary.