My class doesn't get the correct information from the other class

Write your question here.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

#ifndef DATA_H
#define DATA_H



class Data
{
    public:
        Data();

        void newData();
        void saveData();
        void getData();
        void chgStage();
        void display();
        float getGold();
        int getLvl();
        int getStage();
        int getExp();
private:
        float gold;
        int lvl,stage,exp;


};

#endif // DATA_H




/////////////



Data::Data()
{

}

void Data::newData()
{
    fstream flux("data.txt",ios::out);
    flux<<"1 "<<"1 "<<"1 "<<"0";
    flux.close();

}
void Data::saveData()
{

}
void Data::getData()
{
    fstream flux("data.txt",ios::in);
    flux>>lvl>>gold>>stage>>exp;
    flux.close();
    cout<<lvl<<" "<<gold<<" "<<exp;

}

void Data::chgStage()
{
    int temp;
    cout<<endl<<"You are currently on stage "<<stage;
    cout<<endl<<"What stage do you wish to change to?";
    cin>>temp;

    if(temp==stage)
    {
        system("CLS");
        cout<<endl<<"You are already on this stage\n";
        system("PAUSE");
    }

    else if(temp*10>lvl)
    {
        system("CLS");
        cout<<endl<<"You did not unlock that stage yet.\n";
        system("PAUSE");
    }

    else if(temp!=0)
    {
        system("CLS");
        fstream flux("data.txt",ios::out);
        flux<<lvl<<" "<<gold<<" "<<temp<<" "<<exp;
        flux.close();
        cout<<endl<<"Changed to stage "<<temp<<"\n";
        system("PAUSE");
    }
}

float Data::getGold()
{
    return gold;
}
int Data::getLvl()
{
    return lvl;
}
int Data::getStage()
{
    return stage;
}
int Data::getExp()
{
    return exp;
}

void Data::display()
{
    cout<<lvl<<" "<<gold<<" "<<exp;
}





///////////////////


<pre lang="c++">
#ifndef LEVEL_H
#define LEVEL_H

class Level
{
    public:
        Level();
        void stage1();
        void stage2();
        void stage3();
    private:
        float gold;
        int lvl,stage,exp;    

};

#endif // LEVEL_H


///////////


<pre>#include "Level.h"
#include <iostream>
#include "Enemy.h"
#include "Data.h"
#include <windows.h>

using namespace std;


Level::Level()
{


}

 void Level::stage1()
 {
    
 }

 void Level::stage2()
 {

 }
 void Level::stage3()
 {

 }






I want to get those private var from Data to private var in Level to use them there. Also this is probably not the best way to do this i would also appreciate some other better ways to do this.

I tried to do an Data object in Level constructor but it doesn't get the data that is stored in those variables(it shows them as if they are not initialized), also tried Member Initializing , i can get the private data in Main from Data but i can't get the data from Data to Level class .

I'm clearly missing something
Maybe derive class Data from class Level, of Level from Data, or both from a 3rd class that contains
1
2
3
    protected:
        float gold;
        int lvl,stage,exp;    

How do you derrive when u work with headers? i get an error and can't find an example

error: expected class-name before '{' token

edit:nvm i jusd had to include the .h in the other header
Last edited on
closed account (48T7M4Gy)
I want to get those private var from Data to private var in Level to use them there.


1. Since the member properties in both classes are the same why have two separate classes. Unless of course the methods are especially different.

2. Inheritance is another way given 1 above.

3. And lo and behold Davo and I with a few others here have been discussing another possibility worthy of consideration where a class accesses the data members of another class. That's what 'friends' are for isn't it Davo?

4. And the old standby of getters and setters can't be sneezed at.
I've tried to do the Inheritance with protected members but it still apears as it is not initialized,i've tried the getters and setters also, i just think there's someting wrong with the code ,Here's the main:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include "Enemy.h"
#include "Level.h"
#include "Data.h"
#include <windows.h>

using namespace std;


int main()
{
    bool gameOver=false;
    int dec,dec2;
    Data dobj;
    dobj.getData();

    int stg;


    cout<<endl<<"1.Continue Game";
    cout<<endl<<"2.Start New Game";
    cout<<endl<<"0.Quit game";
    cin>>dec;

    Level lo;

    switch(dec)
    {
    case 1:
    {
        while(gameOver==false)
        {
            system("CLS");
            cout<<endl<<"1.Adventure in stage "<<stg<<".";
            cout<<endl<<"2.Change current stage ("<<stg<<").";
            cout<<endl<<"0.Quit game";
            cin>>dec2;

            switch(dec2)
            {
            case 1:
            {
                if(stg==1)
                {
                    lo.stage1();
                }
                if(stg==2)
                {
                    lo.stage2();
                }
                if(stg==3)
                {
                    lo.stage3();
                }
                break;
            }
            case 2:
            {
                dobj.chgStage();
                break;
            }
            case 0:
            {
                gameOver=true;
                break;
            }
            }

        }
        break;
    }
    case 2:
    {
        dobj.newData();
        break;
    }
    case 0:
    {
        gameOver=true;
        break;
    }
    }
}
closed account (48T7M4Gy)
What's you code supposed to do?

It appears to me you are all over the place and need to go back to square one and plan out what you are doing. Make sure you keep a copy but you are trying to solve too many problems at once.

You have decided on two classes Data and a sub class Level. Why I don't know, but if that's what you've decided then write the two classes with a couple of members and a couple of lines, constructor for instance.

Then write a test main() and get the inheritance right.

Then and only then go ahead and feed back in all the detail testing all the way.

Separate headers just require #include's so the linker knows where to go to get the class header. So follow the linker - ie main needs Data.h, and Level.h needs Data.h for the inheritance to proceed.
I want the data that i got from reading from a file into my Level class the exact same way.. i don't think it's complicated at all.. it's just that i do something wrong probably
Last edited on
closed account (48T7M4Gy)
This is along the lines of the first step:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "Level.h"

using namespace std;

int main()
{
    Data data_object;
    std::cout << "1: " << data_object.getGold() << '\n';
    
    data_object.setGold(34.7);
    std::cout << "2: " << data_object.getGold() << '\n';
    
    Level level_object;
    std::cout << "3: " << level_object.getGold() << '\n';
    
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
// Data.h

class Data{
public:
    Data(double = 204.376);
    
    void display();
    double getGold();
    void setGold( double);
protected:
    double gold;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//  Data.cpp

#include <iostream>
#include "Data.h"

Data::Data(double aGold)
{
    gold = aGold;
}

double Data::getGold()
{
    return gold;
}

void Data::setGold(double aGold)
{
    gold = aGold;
}

void Data::display()
{
    std::cout << "Gold = " << gold << '\n';
}


1
2
3
4
5
6
7
8
9
10
// Level.h
#include "Data.h"

class Level:public Data{
public:
        Level();
        void stage1();
        void stage2();
        void stage3();
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//  Level.cpp

#include "Level.h"

Level::Level()
{
    gold = 987; // default value
}

void Level::stage1()
{
    
}

void Level::stage2()
{
    
}

void Level::stage3()
{
    
}




1: 204.376
2: 34.7
3: 987
Program ended with exit code: 0
Last edited on
It works in main but if i want to call any of those in Level.cpp it doesn't work,can u show me an working example where those values are being printed inside Level.cpp?
closed account (48T7M4Gy)
I don't understand what u mean. What works in main?? Call any of what??

Just so you are clear on what I have done. I have completely separate files - main plus 4 others.

The code runs without any problem using Xcode. And the output is as I expected in the gray box at the bottom of my post.

I have taken my code further and have stubs for saving objects to, and reading objects of both types, from files. That runs fine too. All you have to do is follow the same principle as I have shown.
Last edited on
I want an example where

1
2
3
4
void Level::stage1()
 {
    
 }



Prints the protected data from the Data class, when i try to do it the values get lost
closed account (48T7M4Gy)
Are you talking about with my pattern or your original code?
Doesn't really matter which one
closed account (48T7M4Gy)
Best wishes and good luck with your program. Cheers :)
Topic archived. No new replies allowed.