I have the following code:
#include <iostream>
class TestClass {
private:
int var1, var2;
public:
TestClass()
:var1(0), var2(0)
{}
TestClass(int x)
{
std::cout << "\nOne argument constructor\n";
}
void showData()
{
std::cout << "var1 = " << var1 << std::endl;
std::cout << "var2 = " << var2 << std::endl;
}
};
int main()
{
TestClass obj;
obj.showData();
obj = 3;
obj.showData();
return 0;
}
OUTPUT:
My questions:
1 - Why am i able to call a one argument constructor in a statement where the object isn't declared? 2 - Why has the value of var1 and var2 changed after calling one argument constructor?
I ran the code on VS community edition.