First of all hello. I am making a small rag game for practice but i struggle in a certain part. As the title says, I do not know how to change the value of a variable. Below is a part of the program:
class ss{
public:
ss();
void ssf(cs cso);
private:
friendclass cs;
int health;
int dexterity;
int heal;
int damage;
int mana;
};
Now the thing is that when i run the program the health etc are displayed as 0 and not as ex.11. The nc is from another class.Any help is appreciated. Thanks in advance.
Ok. Sorry for the lack of information given. So, I created 2 classes: the cs(the user selects the class) and the ss(sets the stats for the specific class selected). Now in the first class(class selection) there is a function called css(class selection function) where the user types the class that he wants (Wizard, Warrior, Rogue, Cleric) and then depending on what class the user has selected, the variable ns(number of classification) changes. I did that so I can simply use 1 instead of Wizard etc.
Now in the second class called ss(set stats) there is a function called (ssf) where the program changes the values of the different attributes depending on the class(c) selected. The 2 classes are friend with each other so they can share the values set. Now even though the values have been changed inside the ssf the real values are still the same and therefore when I try to display them they are displayed as 0. Inside the header file for the set stats class I have created the variables health, dexterity etc.
So the question is how I can keep the values set inside the set stats function? If you need any additional information I will be glad to provide it.
Thank you for your time
I can see that from the code you've posted (nice to that some context, though).
Give something that I could compile and run and obtain the wrong output that you are having. http://www.eelis.net/iso-c++/testcase.xhtml
your `ssf()' should modify the object fine, the problem may be in how you are checking that later.
Thank you for your time. Now I have another question: How can I upload the code to this site http://ideone.com ? The code is 5 different files(main,cs.cpp, cs.hpp, ss.cpp, ss.hpp) and I suppose that if I just paste the one on top of the other it will not run correctly. Even so, below there are the links for each individual file.
Sorry for my stupidity but i do not get what is wrong. In line 16 of your reply i created an object(sso) so I can use it to access the variables health etc.
So, thank you for your help. I thought of making it simpler and create just 2 functions inside of 1 single class. The first functions is called csf where some values are set like the number classification(nc). The thing is that those values now are displayed correctly only inside this function and when I try to use them in another function their value is 0(or nothing). I think that's because I have changed a copy of the variables and not the variables themselves.
#include <iostream>
#include "cs.hpp"
#include <string>
usingnamespace std;
cs :: cs(){
}
void cs::csf (){
cs cso;
cout<<"Select Class: ";
cin>>cso.c;
if((cso.c =="Wizard") || (cso.c =="Warrior") ){
}
else{//start of else
while((cso.c !="Wizard") || (cso.c !="Warrior")){//start of while
cout<<"Available classes:Wizard,Warrior"<<endl;
cout<<"Please enter class again"<<endl;
cin>>cso.c;
if((cso.c == "Wizard") || (cso.c == "Warrior")){//start of if
cout<<"your class is: "<<cso.c<<endl;
break;
}//end of if
}//end of while
}//end of else
//start of setting numberclassification
if (cso.c=="Wizard"){
cso.nc = 1;
}
elseif(cso.c=="Warrior"){
cso.nc = 2;
}
//end of setting numberclassification
cout<<cso.nc<<endl;//here the value set above is displayed correctly
}
void cs :: ssf(){
cs cso;
switch(cso.nc){ //start of switch
case(1):
cso.health = 11;
cso.dexterity = 6;
cso.heal = 3;
cso.damage = 10;
cso.mana = 2;
break;
case(2):
cso.health = 20;
cso.dexterity = 1;
cso.heal = 1;
cso.damage = 8;
cso.mana = 2;
break;
} //end of switch
cout<<"Good..."<<endl;
sleep(1);
cout<<"Now you are a "<<cso.c<<endl;
//above when I try to display the c string that has
//been set in the csf function is displayed as nothing
sleep(1);
cout<<cso.c<<"'s stats are the following:" <<endl;
cout<<"health = "<<cso.health<<endl;
cout<<"dexterity = "<<cso.dexterity<<endl;
cout<<"heal = "<<cso.heal<<endl;
cout<<"damage = "<<cso.damage<<endl;
cout<<"mana = "<<cso.mana<<endl;
}
> I think that's because I have changed a copy of the variables
Not even a copy, you are simply creating another object.
1 2 3 4 5 6 7 8 9
void cs :: ssf(){
cs cso; //this is not the same variable as the one in main()[.code]
You need to understand that names are local. Variables declared in different functions have no relationship between them, it doesn't matter that they have the same name.
Otherwise it would be quite confusing.
Notice that to call a member function you need an object [code]cso.csf();
That object is passed as a hidden parameter, the this pointer, so you may refer to it inside the function.
So you may do
1 2 3
void cs :: ssf(){
this->damage = 42;
}
Doing this->member_variable or simply member_variable is equivalent inside a member function.
Ok. You help me a lot. If I make the variables global and write them on top of the main(), how can I then use them inside the functions of a class? Sorry for changing the question with every post.
Happy Halloween
I read some more tutorials and If I got it correctly every object creates a different set of variables and when for instance you use the object1, you manipulate a copy of the variables created. Then if you want to use the same data that you have set using the object1, you have to use the same object otherwise the data just won't have a value. So if you have the variable A, you can change it using an object1 and an object2. Then the variable A will have 2 values, depending on the object that you use to access it. Please correct me if something I did't got it right.
EDIT: 1 more thing: I read that using the private or protected section of a class is better because the public section slows down the program. Is that correct?