2)What do you mean by "get" ? You use a vector the same way you use it normally. But in your class, the vector is private, so you probably would make functions to add elements to your vectors.
3)There is no right way to make a class, it depends on your preferences. But a class to hold variables, isn't a very good idea.
1) Basically what Dash said; you haven't defined your game() function.
void vars::game() { }
2) You should create a getter(inspector) method inside your class to get the values held in the vector. Either that or you could have a public iterator.
Ok i fixed the void vars::game() {} one. It seems to me that no matter what i do i just cant undertand classes. :( ive watched so many tutorials and i did read some articles and i ust dont get it.
So should i just use a struct then? wait a minute... i think something just came to me. structs are for holding variables and classes are for grouping similar functions together, is that right?
Here's an example of the use of classes. You should only use a class if you are going to have more than one object. That's how I think of it. My only exception is if the class is a wrapper for something else.
If you want to pass around all those vars to a different function (which needs a lot of them, not just one, otherwise there is little point) I'd make it struct (or class if you want, just preference really)
and don't forget to pass the struct/class by (const if you don't plan to change a var) reference.
I revised it so I wouldn't have to explain that, but I'll do it anyway. :)
1 2 3
std::ostream& operator << (std::ostream &os, const Student &s1) {
return os << s1.name << " is " << s1.age << " years old.\n";
}
That's overloading the insertion operator. The overloaded operator '<<' takes an object of the std::ostream class and an object of the Student class. It takes 'cout' and the Student object that 'iter' is currently on, then returns the object's information to the 'cout' stream object.
Was that a good explanation? I hardly ever work with classes so I'm not even sure if that is correct. If it isn't and anybody notices then please point it out.
class vars
{
public:
void game();
vars(long long int M, int XP, string PN, string BN);
vars(){}
long long int getMoney(){return money;}
int getXP(){return xp;}
string getPname(){return Pname;}
string getBname(){return Bname;}
private:
long long int money;
int xp;
string Pname;
string Bname;
};
vars::vars(long long int M, int XP, string PN, string BN): money(M), xp(XP), Pname(PN), Bname(BN)
{
}
switch(choice)
{
case 1:
cout << "Welcome, in this game you make your own shop from the ground up and" << endl;
cout << "build a mega store\n" << endl;
cout << "Lets start by getting some information" << endl;
break;
case 2:
vo->game();
break;
}
Ok i got it working by putting vars(){} in there and deleting the : and the stuff after vars(long long int M, int XP, string PN, string BN), why do i need an empty constructor? at what line should i make a destructor for the class?