Hello everyone,
I am having a problem with following code. When I try to access the data member "i" in the Numbers class from the main() it prints "-858993460". But in the constructor it assigns and prints a random number without a problem. I am pulling my hair out since I couldn't figure out what I am doing wrong. I appreciate it if you can land me a hand and tell me what's wrong.
Oh by the way I tried with an accessor function and got the same result.
Thanks,
---------------------------------------------------------------------------------
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;
class Numbers {
public:
int i;
Numbers (int i = 0);
};
Numbers::Numbers (int i)
{
i = rand ();
cout << i << endl;
}
int main (void)
{
srand (time (NULL));
Numbers myNumbers;
cout << myNumbers.i << endl;
return 0;
}
---------------------------------------------------------------------------------
Report this post
I am having a problem with following code. When I try to access the data member "i" in the Numbers class from the main() it prints "-858993460". But in the constructor it assigns and prints a random number without a problem. I am pulling my hair out since I couldn't figure out what I am doing wrong. I appreciate it if you can land me a hand and tell me what's wrong.
Oh by the way I tried with an accessor function and got the same result.
Thanks,
---------------------------------------------------------------------------------
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <ctime>
using namespace std;
class Numbers {
public:
int i;
Numbers (int i = 0);
};
Numbers::Numbers (int i)
{
i = rand ();
cout << i << endl;
}
int main (void)
{
srand (time (NULL));
Numbers myNumbers;
cout << myNumbers.i << endl;
return 0;
}
---------------------------------------------------------------------------------
Report this post