Need some help with classes and my project

I havent been programming for a while now and i am trying to make a tex Sim program but im stuck on the first part here, i want to make it so i can use the variable happiness in main but i keep getting an error

Program:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class player
{
    public:
        void happy(int happiness = 0)
        {
            if(hunger > 40)
            {
                happiness += 15;

                cout << "happiness " << happiness << endl;
            }
        }

        void angry()
        {

        }

        void hungry()
        {

        }

    private:
        int happiness;
        int anger;
        int hunger;
};

int main()
{
    player M;
    M.happy();

    int choice = 0;

    cout << "Add happiness 1" << endl;
    cin >> choice;

    if(choice = 1)
    {
        happiness += 20;
        cout << happiness << endl;
    }

}



Error

C:\Users\Chay\Desktop\Moods\main.cpp||In function 'int main()':|
C:\Users\Chay\Desktop\Moods\main.cpp|46|warning: suggest parentheses around assignment used as truth value|
C:\Users\Chay\Desktop\Moods\main.cpp|48|error: 'happiness' was not declared in this scope|
||=== Build finished: 1 errors, 1 warnings ===|

i tried M.happiness but still got errors.
The reason you have an error is because happiness is private. The whole idea of classes is to have all the member variables private, then provide public functions which give access to those member variables. I don't mean you should have a public get / set function for each one, because that is the same as making all the variables public.

Instead think about how things might happen in real life. For example if you a class called CPerson which had Names, Address, Phone numbers, Email addresses etc, you could provide functions that set these things in their respective groups, all in one go. That is say a setAddress function which sets the Address1, Address2, Address3, Suburb, PostCode, State etc member variables all in one function.

Hope this helps.
I still dont quite understand :(
The happiness variable is a private data member of the player class. That means you can only access that variable with functions that are members of the class or are declared as friend functions. There are reasons for this split that are a little more then I can explain here so it'd be best to grab a beginner tutorial or a reference on classes and read that instead. Classes and OOP are a deep topic that needs to be taken slowly to properly utilize them.
Have you read this?

http://www.cplusplus.com/doc/tutorial/classes/


Class member variables are supposed to be private, so that only objects of that class can change their values, and not just be changed by any other function.

For example, if I have a square object, I don't want just anyone to be able to go and change the value of one of it's corner coordinates - now it isn't a square any more. I also don't want anyone to be able to move the whole thing to a different location.

Same Idea for a bank account. You wouldn't want random people changing your account balance, the name of the account, transaction details etc.

So there is the idea of having private variables, with public functions that operate on them. The functions can only be called through the object of that class type. It gives the functions a chance to ensure that the object is always valid (A square is still a square). It also means that only valid operations can happen to an object.
Access specification is irrelevant here.
Here is your 'happy' function, as you posted :

1
2
3
4
5
6
7
8
9
void happy(int happiness = 0)
        {
            if(hunger > 40)
            {
                happiness += 15;

                cout << "happiness " << happiness << endl;
            }
        }


Now, you've got two variables named 'happiness'. One is a private member variable, the other is an argument. When two variables with the same name exist within the same scope, the more localized variable takes precedence.
You should be able to solve your problem now, I think.
Last edited on
I still dont understand, im not sure why, i read all your posts and looked at the link to the documentation on this site but its still hard for me to grasp. i was following a tutorial and this is what i got, please tell me if what he did was right and if there is any way it can be made simpler, i dont want to learn what he did and find out its wrong or theres an easier way of doing it.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>

using namespace std;

class Rectangle
{
    private:
        int width;
        int height;

    public:
        Rectangle();
        ~Rectangle();

        void set_width(int w);
        void set_height(int h);

        int calc_area();
        int calc_perim();
};

Rectangle::Rectangle()
{
    cout << "creating rectangle." << endl;
    width = 10;
    height = 15;
}

Rectangle::~Rectangle()
{
    cout << "Destructing Rectangle" << endl;
}

void Rectangle::set_width(int w)
{
    width = w;
}

void Rectangle::set_height(int h)
{
    height = h;
}

int Rectangle::calc_area()
{
    return (width*height);
}

int Rectangle::calc_perim()
{
    return (2*width + 2*height);
}

int main()
{
    Rectangle r;

    cout << "Area of r: " << r.calc_area() << endl;
    cout << "Perimeter of R: " << r.calc_perim() << endl;

    r.set_width(3);
    r.set_height(12);

    return 0;
}
There are multiple problems here.

0. You have no Constructor/Destructor
1. You are writing function bodies inside the class declaration.
2. You are not initialising your member variables.
3. You are using the same names for multiple variables
4. You are trying to access a member variable of an object by just using its name. (Should be theObject.m_variable).
5. The variable you are trying to access is private so even if you were doing the previous part right, it is still wrong.


The example you posted with the Rectangle. Why did you not follow the ways that was done? I suggest going over the basics of creating variables, creating classes, variable scope, variable access etc.
Last edited on
"The example you posted with the Rectangle. Why did you not follow the ways that was done?"

because i was following a tutorial, so if theres errors there they arent mine i just copied what he did but posted it here because i didnt want to learn it if he was doing it wrong.
What the tutorial showed you was absolutely fine. Most folks separate the class declaration into a header file, then the definitions into a separate source file, then you have main in another source, but for small stuff like that it's no biggie.
Topic archived. No new replies allowed.