Class variables?

I want to put variables inside a class, and make it so every function inside that class can use and edite it. I can't make it to work... I want to call the variable inside other functions too (like int main), to check what's its state.
Last edited on
If you can paste the code that is causing you problems it will be easier to explain exactly what you are misunderstanding.
Well, just an example. Don't say anything about the stupidness of it, it's just an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

class Example {
    public:
    int X = 27;
    static void Cout(){
        cout << Example::X;
    }
    static void ChangeX(int value){
        Example::X = value;
    }
}E;

int main(){
    E.ChangeX(19);
    E.Cout();
    if(E.X<20){
        cout << " is lower than 20";
    }
}


How to make that to work?
Last edited on
The first problem is you are not allowed to define values straight away when declaring them in a class. (unless they are const static variables, in which case you can't change them). If you want X to have a starting value then you will want to use a constructor to set it when the object is created.
http://www.cplusplus.com/doc/tutorial/classes/

The other problem is that you are using static functions which can't change values in a class unless they are also static and are usually not called with class objects.

Assuming you don't actually need anything static, here is an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Example 
{
    public:
	Example() {X = 27;}
	int X;

	void Cout()
	{
	    cout << Example::X;
	}

        void ChangeX(int value)
        {
            Example::X = value;
        }
}E;


(On a side note class variables are usually made private and you use public functions to access their value).
But as far as i tried, unless a function inside a class is static i can't call them.
I think you are messing up with the distinction between instance and static variables. Try this example: can you see the difference in the behaviour of x (instance) and Y (static) ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

class Example{
public:
	int x;
	static int Y;


	Example(int v) : x(v) { }

	void Cout(){ cout << "x=" << x << ", Y=" << Y << endl; }

    void ChangeX(int value){ x = value; }

};
int Example::Y = 1;

int main(int argc, char *argv[]){

	Example a(1);
	Example b(10);

	cout << "before: " << endl;
	a.Cout(); // x=1, Y=1
	b.Cout(); // x=10, Y=1

	a.x = -1; a.Y = 100;

	cout << "after: " << endl;
	a.Cout(); // x=-1, Y=100
	b.Cout(); // x=10, Y=100

//system("pause"); //???
return 0;
}


maybe it helps, maybe it will baffle more the situation :O-O

Hm, yes, I understand some of that. But, now that i see something new, what does Example(int v) : x(v) { } do? And what are you doing with Example a(1); Example b(10);? Does it have something to do with structures?
That's the way you a) initialise class members and b) invoke a constructor with params. For a) I think you can stick with the assignments (does it perform two assignments instead of one or am I wrong??).

As pointed out by James, you find a more deep overview at: http://www.cplusplus.com/doc/tutorial/classes/

regards
-dean
Last edited on
static means that the variable is only instanced once:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class SelfCounter
{
private:
  static int amount;
public:
  SelfCounter(void)
  {
    amount++;
    cout << "You have made " << amount << " of me\n";
  }
};
int SelfCounter::amount = 0;

int main(void)
{
  SelfCounter first;  // displays 1 
  SelfCounter second; // displays 2
  SelfCounter third;  // displays 3
  return 0;
}
Last edited on
LowestOne, i just noticed you're using C. I use C++ because sometimes is easier (But it depends on who makes the program, this is my opinion).

Ignoring that, what i put after the function calling does't matter? So can i put "SelfCounter Hello;"?
samrux wrote:
LowestOne, i just noticed you're using C. I use C++ because sometimes is easier


It's not C. Because it has a class in it - It is definitely C++

Ignoring that, what i put after the function calling does't matter? So can i put "SelfCounter Hello;"?


SelfCounter first; // displays 1

That bit is creating an object called first which is an instance of SelfCounter. Not to be confused with a function call. It is calling a function - the constructor function that is part of the SelfCounter class, which is called every time an object of that type is created. The static variable remembers it's value, so when you create another instance of the class, you can do something with the retained value.
Topic archived. No new replies allowed.