object for test class can be created as

test t=50;
// y so..how can integer be equal to object??
// don't we need to call any constructor here or is it some trick of visual studio??...and if it is then how is it possible??

class test
{
public :
    int a,b;
    test(int x=0,int y=0)
    {
        a=x;
        b=y;
    }
};

void g()
{
    test t2=50;

    cout<<t2.a<<":"<<t2.b<<endl;

}

int main()
{
    g();
    system("pause");
}

using visual studio 2008

Recommended Answers

All 4 Replies

From what I see it appears that the constructor is there...

Basically since the class has pre-determined initial values if the user fails to provide one of 0 and 0 for x and y, you only need to provide 1 value, or 0 values to make the class. It works on pre-determined initial values from right to left, since you provided 1 value, it will use the y=0 and then take the value that you provided and set it equal to x. Hope this helps.

class test
{
public :
int a,b;
test(int x=0,int y=0)  // this is your constructor
{
a=x;
b=y;
}
};

void g()
{
test t2=50;
test t3;

cout<<t2.a<<":"<<t2.b<<endl; // will display 50:0 since 1 is initialized
cout<<t3.a<<":"<<t3.b<<endl: // will display 0:0 since already defined above

}

int main()
{
g();
system("pause");
}

From what I see it appears that the constructor is there...

Basically since the class has pre-determined initial values if the user fails to provide one of 0 and 0 for x and y, you only need to provide 1 value, or 0 values to make the class. It works on pre-determined initial values from right to left, since you provided 1 value, it will use the y=0 and then take the value that you provided and set it equal to x. Hope this helps.

class test
{
public :
int a,b;
test(int x=0,int y=0)  // this is your constructor
{
a=x;
b=y;
}
};

void g()
{
test t2=50;
test t3;

cout<<t2.a<<":"<<t2.b<<endl; // will display 50:0 since 1 is initialized
cout<<t3.a<<":"<<t3.b<<endl: // will display 0:0 since already defined above

}

int main()
{
g();
system("pause");
}

agreed , but the thing is how is this possible logically??
what we are trying to do is "object = int", is this thing logically possible and if yes then how is is this thing internally implemented.

It works but is ambiguous to me. Did you get a warning?

This code test t2 = 50; is the same as test t2 = test(50); and this code test t3; is the same as test t3 = test();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.