Could this just go in a struct instead of a class?

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.

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
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[];
};
> I have this class and i was wondering if i can just put it in a struct instead

Yes.
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?
Last edited on
> 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.
Last edited on
Well,people have just explained you that,from me :Just always use classes and you'll be okay
so basically theres no advantage or reason to switch to a struct?
Generally speaking, there are only disadvantages to switching to a struct. (Assuming this also removes encapsulation.)
> 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.

http://www.justsoftwaresolutions.co.uk/cplusplus/struct_and_class.html
Last edited on
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.
I never use the class keyword, I always use the struct keyword:
1
2
3
4
5
6
7
8
9
10
struct MyClass
{
    MyClass();
    ~MyClass();

    void DoStuff();

private:
    int x, y;
};
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?
In C, struct has a completely different meaning. In C++, when classes were added, the same features were added to structs.
Last edited on
i guess it's the default access
ah i see, so can i do inheritance with structs?
ah i see, so can i do inheritance with structs?

As JLBorges already said:

Whatever you could do with the keyword class, you can also do with the keyword struct.


Edit: although note the the default access for inheritance is also public for structs, and private for classes.
Last edited on
Topic archived. No new replies allowed.