I have this class and i was wondering if i can just put it in a struct instead, i know the only difference between a struct and class is that a class is private by default and a struct is not.
class Player
{
public:
Player();
~Player();
void gameStart();
void save();
void load();
void startup();
int mainGame();
void accountNumbers();
void shop();
void cheats();
void playerInfo();
void hackBank();
void findBank();
void upgrades();
void alabamaBank();
private:
int money;
string name; //The player will enter their name so name will not be constant
bool isFirstStartup;
vector<string> createNumberList;
vector<string> bankList;
vector<float> bankAccounts; //this stores the money in the list.
int experience;
int accountsHacked;
int PasswordCrackerLvl;
int DecryptionToolLvl;
int PasswordCrackerPrice;
int DecryptionToolPrice;
int plr_inventory[];
};
cool, now if i want to just create variables and group them together i just use a struct but if i want to do something like polymorphism or something than i just use a class? Also since a struct is public i dont need to have "public:" in there anymore right?
> cool, now if i want to just create variables and group them together i just use a struct
> but if i want to do something like polymorphism or something than i just use a class?
Whatever you could do with the keyword class, you can also do with the keyword struct.
With class, members and base classes are private by default; with struct they are public by default.
There are no other differences.
Note: for specifying a template template parameter, the specific keyword class is required.
> so basically theres no advantage or reason to switch to a struct?
No. When the keywords struct or class is used to declare or define a user-defined type, other than the default access specifier for members and base classes, there is no semantic difference between the two.
In common header files that are designed to be used with both C++ and C, use the keyword struct.
Anywhere else, you can choose to use either one.
to give an example, I mostly use the keyword struct when declaring functors that have just one public member function (the function-call operator) and metafunctions that have just one public member type. Just because it makes them easier to read.
So what was the point of Bjarne Stroustrup implementing classes in c++ if the only difference is access level? didnt structs have public and private back then when it was just C? why couldnt you just use public and private in the struct?