Help with constructors and classes

T

Tommy Lang

Hi !!

I am a c++ novice and I would appreciate any help with the following:

I have created a class called "Number". In the main function of my
program I call the class constructor twice like this(at start up)...
int main(){
Number no1;
Number no2;
.......

Everything is fine so far.
Then in the class constructor I want to ask for two numbers and add
them to the private member variables m_no1 and m_no2...
Number::Number()
{
if (first_time) //first-time is a private boolean variable (how to
init it??)
{cout << "Enter number: ";
cin >> m_no1; cin.get();}
else
{cout << "Enter next number: ";
cin >> m_no2; cin.get();}

}

I have tried this approach but I couldn't get it to work properly. And
when I got it to work it NEVER got to the else statement. First time I
want to get m_no1 next time m_no2. Any pointers???

Thanks,
Thomas
 
R

Ron Natalie

Tommy Lang said:
Hi !!

I am a c++ novice and I would appreciate any help with the following:

I have created a class called "Number". In the main function of my
program I call the class constructor twice like this(at start up)...

You're not really calling the constructor. You're creating two objects. The
constructor is called as part of the entire object creation.
if (first_time) //first-time is a private boolean variable (how to

It is ALWAYS important to provide all the requisite information. Your example
doesn't show the declaration of first_time. I presume from your description it
looks like this:
bool first_time;

Which yes, creates an uninitialized variable first_time. You could initialize it
but it still wouldn't be right. The above is a non-static member. This means that
every instance of your Number class gets it's own copy. Even if you initialize
it to true, it would always test true in the constructor (the constructor never runs
more than once on any single object).

What you want to do is declare it as a static member:

static bool first_time;

This is what in other languages is called a class variable. It's shared between
all instnaces of the same class. You must also define and initialize the varable.
Outsdie the class definition put:

bool Number::first_time = true;
 
G

Gianni Mariani

Tommy said:
Hi !!

I am a c++ novice and I would appreciate any help with the following:

I have created a class called "Number". In the main function of my
program I call the class constructor twice like this(at start up)...
int main(){
Number no1;
Number no2;
......

Everything is fine so far.
Then in the class constructor I want to ask for two numbers and add
them to the private member variables m_no1 and m_no2...
Number::Number()
{
if (first_time) //first-time is a private boolean variable (how to
init it??)
{cout << "Enter number: ";
cin >> m_no1; cin.get();}
else
{cout << "Enter next number: ";
cin >> m_no2; cin.get();}

}

I have tried this approach but I couldn't get it to work properly. And
when I got it to work it NEVER got to the else statement. First time I
want to get m_no1 next time m_no2. Any pointers???


It sounds like you want first_time to be a static value.

Secondly, it seems like you never make it false.

Post a compilable chunk-o-code so we can give you better advice.
 
K

Kevin Goodsell

Tommy said:
Hi !!

I am a c++ novice and I would appreciate any help with the following:

I have created a class called "Number". In the main function of my
program I call the class constructor twice like this(at start up)...
int main(){
Number no1;
Number no2;
......

Everything is fine so far.
Then in the class constructor I want to ask for two numbers and add
them to the private member variables m_no1 and m_no2...
Number::Number()
{
if (first_time) //first-time is a private boolean variable (how to
init it??)

You are right to worry about initializing this. If you execute this 'if'
without first initializing first_time, your program's behavior is
undefined (could do anything, including crashing, behaving
unpredictably, working as expected, making long-distance phone calls, etc.)

The thing is, this is a constructor. Initialization is what it *does*.
So whatever you set 'first_time' to, that's what it's going to be. It
doesn't make much sense to branch based on it, because the result will
always be the same:

first_time = true;
if (first_time)
{
// well, obviously this will be executed.
}
else
{
// this will never be executed.
}
{cout << "Enter number: ";
cin >> m_no1; cin.get();}

You might have to explain why you think you need cin.get() here.
else
{cout << "Enter next number: ";
cin >> m_no2; cin.get();}

}

I have tried this approach but I couldn't get it to work properly. And
when I got it to work it NEVER got to the else statement. First time I
want to get m_no1 next time m_no2. Any pointers???

You don't want to branch. You want 2 numbers, unconditionally, right?

cout << "Enter the first number: ";
cin >> m_no1;
cout << "Enter the second number: ";
cin >> m_no2;

No 'if' is necessary.

....

OK, but from the sound of it, this isn't what you *really* want to do.
You need to understand that the constructor will be invoked
*independently* for each object. In general, the constructor cannot (and
should not) know if it's already been invoked, or how many times. What
you really want is something like this:

#include <iostream>

using namespace std;

class Number
{
public:
Number() { cout << "Enter number: "; cin >> m_no; }
private
int m_no;
};

int main()
{
Number no1;
Number no2;

return 0;
}

This will work, but you can't change the text of the prompt, unless you
want to pass it in as a parameter to the constructor, but basically
doing I/O is a constructor is a bit screwy anyway (you can do it if you
really want to, but it's very unusual), so you should probably do
something like this instead:

#include <iostream>

using namespace std;

class Number
{
public:
void Read() { cin >> m_no; }
private:
m_no;
};

int main()
{
Number no1, no2;
cout << "Enter the first number: ";
no1.Read();
cout << "Enter the second number: ";
no2.Read();

return 0;
}

-Kevin
 
J

jeffc

Tommy Lang said:
Hi !!

I am a c++ novice and I would appreciate any help with the following:

I have created a class called "Number". In the main function of my
program I call the class constructor twice like this(at start up)...
int main(){
Number no1;
Number no2;
......

Everything is fine so far.
Then in the class constructor I want to ask for two numbers and add
them to the private member variables m_no1 and m_no2...
Number::Number()
{
if (first_time) //first-time is a private boolean variable (how to
init it??)

OK, I can tell already that you have a misunderstanding about how objects
are created. You have a serious design problem here. You do NOT want to
ask for both numbers at the same time in the constructor. BAD BAD. Forget
that first_time variable. You want to design your program more like this:

int main()
{
cout << "Enter number:";
cin >> m_no1;
// at this point you want to create no1, but you want to use a constructor
// with which you can pass in m_no1

cout << "Enter next number: ";
cin >> m_no2;
// at this point you want to create no2, but you want to use a constructor
// with which you can pass in m_no2
}

You will have to write a new constructor for Number that can take that
number you pass in. Put that in your pipe and smoke it for awhile and then
come back for a second round of questions.
 
T

Tommy Lang

Thanks Ron. Your help solved my problem. I needed to use a static bool
variable. And setting it to true outside the class def. just like you
said.
(bool Number::first_time = true;)

Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,820
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top