Class vs Struct

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
TheLividePianoist
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Thu Jan 29, 2009 5:48 pm

Class vs Struct

Post 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!
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: Class vs Struct

Post by herby490 »

Structs are classes with all members public by default other than that there is no difference in c++.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Class vs Struct

Post 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. :)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Class vs Struct

Post by RyanPridgeon »

Also classes cannot be used in C, whereas structs can (although structs cannot contain any functions or private members in C)
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
Post Reply