473,387 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with constructors and classes

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
Jul 19 '05 #1
5 1804

"Tommy Lang" <mu*****@yahoo.se> wrote in message news:78**************************@posting.google.c om...
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;

Jul 19 '05 #2
Tommy Lang wrote:
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.

Jul 19 '05 #3
Tommy Lang wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4

"Tommy Lang" <mu*****@yahoo.se> wrote in message
news:78**************************@posting.google.c om...
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.
Jul 19 '05 #5
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


"Ron Natalie" <ro*@sensor.com> wrote in message news:<3f***********************@news.newshosting.c om>...
"Tommy Lang" <mu*****@yahoo.se> wrote in message news:78**************************@posting.google.c om...
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;

Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
4
by: Jimmy Johns | last post by:
Hi, I have some classes as follows: #include <iostream> using namespace std; class A { public: virtual A* clone() const = 0; };
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
3
by: SLE | last post by:
Hi there, I know constructors are not inherited from the base class, not in VB.NET nor C# (nor Java I suppose). I never wondered,but reflecting on the reason why, I cannot find a solid answer. ...
6
by: Doug | last post by:
I have a public abstract class that I want to inherit from in two other classes. My public abstract one has a constructor with several parameters. For some reason I cannot get to that constructor...
3
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand...
3
by: jack | last post by:
Hi there, I have a function F(x, y, z) and I want to calculate f(a) + f(b), where f(x) = F(x, x, z0) z0 is fixed. Suppose somebody wrote a routine called "Compute" which simply computes f(a)...
3
by: Alexander Muylaert | last post by:
Hi Is their a way to keep constructors visible in inherited classes? public class X{ public x(int A){} } public class Y : X{};
22
by: Peter Morris [Droopy eyes software] | last post by:
Look at these two classes public class Test { public readonly string Name; public Test(string name)
21
by: Michael Hull | last post by:
Hi, I remember from somewhere reading that inlining constructors is a 'BadThing', but now can't seem to find the original source. I can't however thing of a reason why it would be for simple...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.