#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");
}
elseif(temp*10>lvl)
{
system("CLS");
cout<<endl<<"You did not unlock that stage yet.\n";
system("PAUSE");
}
elseif(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>
usingnamespace 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 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:
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
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?
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.