constructor does not run

129 views Asked by At

I do not understand because when you create an object of the "Users" class not the message is printed containing the constructor.

class users
{
public:
    users();
private:
    int i;
};
users::users ()
{
    cout<<"hello world";
}
int main ()
{
    users users1();
    return 0;
}
2

There are 2 answers

1
Barmar On
users users1();

doesn't declare an object of the users class, it declares a function that takes no arguments and returns an object of the users class. To declare an object, use:

users users1;
0
deeiip On
class users
{
public:
    users();
private:
    int i;
};
users::users ()
{
    cout<<"hello world";
}
int main ()
{
    users users1; // either you use this
    users* user2 = new users(); // or you do this
    return 0;
}

This worked fine for me. see here