I was writing a simple class to test my understanding of class and the first class program had a number -int num private- that was modified by the user by void definenum(num) and retrieved with int getnum(). It functioned as I expected-i would define it as 4 and it would retrieve 4. However when i tried to add a constructor to initialize the program it got nutty. It compiled fine with this code:
header:

class numgetter{
private:
	int num;
public:
	numgetter()
	{num=0;}
	void definenum(int num)
	{}
	int getnum()
	{
	return num;
	}
};

main:

#include<iostream>
#include"class_header.h"
using namespace std;
int main()
{
              class numgetter test;
              int in_num;
              cout<<"Input the number";
              cin>>in_num;
              test.definenum(in_num);
              cout<<test.getnum();
              system("pause");
}

I would input 4 and it would retrieve 0.
Why doesnt it have the constructor numgetter() execute when I make the new instance of it, rather all the time-or so it seems?

Recommended Answers

All 10 Replies

Your problem is the overloading of the instance of number.

Let me illustrate.

int num=6;
{
   int num;      // This num is not the same as above
   num=7;
}
// In this scope 
cout<<"num =="<<num;

In your case you have put void definenum(int num) {} and this has produced a new local variable (called num) and it does nothing with it.

you could write

void definenum(const int X) 
{
    num=X;
}

Then that should work as intended.

Hope this helps. If not please feel free to repost / or ask different questions.

commented: nice, simple, straightforward +1

Hi,
The constructor will be called only when the instance is first created. In your function definenum you does not seem to be doing anything. So the value of your private variable remains zero (This is initialised in the constructor.)

void definenum(int num)
{}
it defies num, when called it does a pbv and so int num is now whatever I passed it to be

Also, to STUXYZ why didnt my constructor work-it didn't have access? Because I think its still talking about the same num in terms of overloading.

The constructor did exactly what you asked it to (as all software should.)

numgetter()
{num=0;}

Defines the constructor to initialize the num to zero.

ya, but I cant figure out why when I compile it the constructor works but then I can't define define it again with definenum. If it is an operator overloading error or pbv or pbr error please tell me. It works when I change the void definenum(int num) {} into definenum(int in_num){num=in_num;}

definenum is NOT a constructor, it is a method.

The code void definenum(int num) {} is like me walking up to you and handing you something, but not telling you what to do with it; eventually, you'll throw it away.

When you expand the code to void definenum(int in_num){num=in_num;} you told the class what to do with the something. "When someone calls definenum, take what they gave you and save it in num, throwing away any previous value."

I was saying getnum was a constructor and my problem was that I thought if I call with the parameter int in_num why doesnt it do a pbv and set num=in_num. I needed to know if it was a matter of access priveeges or blocking. the first response to my question by StuXYZ made the most sense.

Well having been away (sorry). But I would like to make a summary of were I think we all are.

As is my custom, it will be with examples. Now the discussion is really about scope. In C++ it is possible to have many different variables called the same thing. But the compiler will only allow one variable name per scope. Once you understand the arcane magic called scoping rules all is easy. (Although true understanding of all scoping rule requires a very very long beard ;)

So to an example. I would like to post complete programs. copy them an run them and see the stangeness on your own computer.
In this case we are going to use the variable name X to mean all the many different X I am going to produce.

#include <iostream>

class A
{
private:
  int X;
  int Y;
public:

  A(int X) : X(17),Y(X+A::X)
    { }
  void setY(int X) { Y=X; }
  int getX() const { return X; }
  int getY() const { return Y; }
};

int X=9;

int
main()
{
  int X=8;
  A xv(20);
  std::cout<<"X in class = "<<xv.getX()<<std::endl;
  std::cout<<"Y in class = "<<xv.getY()<<std::endl;
  xv.setY(455);
  std::cout<<"Y in class= "<<xv.getY()<<std::endl;
  std::cout<<"X = "<<X<<std::endl;
  std::cout<<"X (globally) = "<<::X<<std::endl;
}

Well I had better have a go an explaining the mess. First off
let us look at class A. It has a class variable X.
I have complicated the thing by calling the constructor with a local method variable X. The rules basically say that the more local a variable is the higher its precedence, i.e. which variable will be called X. All other variables can be obtained but have to be qualified.

so line 10 says: set the variable X in A to 17. Then set variable Y to the local variable X + the class variable X which we just set to 17.
From line 23, Y will be 17+20 == 37.

line 12: setY uses a local variable X. Y is set to that local variable. The class variable X is irrelevent until you change the setY(int X) to something like setY(int Z) .

line 17: sets a global variable X.
line 22: sets a local variable X (to the function int main) and as that is more local line 28 use that.
line 29: This shows how to get a global variable when there is a local variable in scope.

Try playing with the values, and the name X to see what happens.
If you understand what goes on here it will make you a MUCH better c++ programmer.

Thank you and i now understand a scope part and i think i know now why my program was malfunctioning. Merry x-mas!

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.