Page 1 of 1

Class vs Struct

Posted: Wed Jul 22, 2009 7:31 pm
by TheLividePianoist
I am currently learning the use of Classes and Structures and I just had to ask what the differences and similarities between the two are. When should classes be used and when should structures be used in a program. Can they even be compared? Remeber I am just learning C++ so if you can explain the answers in a fairly understandable way then please do so. Thanks for the help!

Re: Class vs Struct

Posted: Wed Jul 22, 2009 9:05 pm
by herby490
Structs are classes with all members public by default other than that there is no difference in c++.

Re: Class vs Struct

Posted: Wed Jul 22, 2009 10:31 pm
by dandymcgee
To expand a bit on herby's post with an example:

Code: Select all

class Example{
    public:
        int number;
};
..Is the same as a struct with no private/public specifications and:

Code: Select all

struct Example{
    private:
        int number;
};
..Is the same as a class with no private/public specifications.

By the way, this question has been asked on these forums before. Please remember to use the search feature in the future before creating a new topic.

Thanks. :)

Re: Class vs Struct

Posted: Thu Jul 23, 2009 5:29 am
by RyanPridgeon
Also classes cannot be used in C, whereas structs can (although structs cannot contain any functions or private members in C)