Class vs. Struct and public/private rulings

This is a discussion on Class vs. Struct and public/private rulings within the C++ Programming forums, part of the General Programming Boards category; In structs all members of them are public, w/ a class there is a public/ private/protected section, why use which ...

  1. #1
    Unregistered
    Guest

    Class vs. Struct and public/private rulings

    In structs all members of them are public, w/ a class there is a public/ private/protected section, why use which section and for what?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Actually, structs can use those keywords too. The only difference between structs and classes in C++ is that structs are public by default and classes are private by default.

    >why use which section and for what?
    public should be used for anything that you want a program to access directly:
    object.variable = value
    The goal with data hiding is to minimize this as much as possible. Make your class members private and have public methods that access them.

    private should be used for as much as you can. As I said above, public methods that actually effect your data and private members/methods that do the implementation of your class. The user of your class should only be able to see the interface and not be able to do anything with your class except use it the way you want them to.

    protected is for inheritance, it is used when you want the class members private to everything except your class and classes derived from your class.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    correct me if i'm wrong
    structures are in c++ to make it compatible with c
    classes were introduced to help support c++ become more of an object-oriented programming language

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >structures are in c++ to make it compatible with c

    Yes, but as Prelude has pointed out C_struct != C++_struct.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Location
    South Africa
    Posts
    35

    _Struct.

    >Yes, but as Prelude has pointed out C_struct != C++_struct.

    He did?
    I code.
    I think.
    Ergo, I think in code.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The only difference between structs and classes in C++
    This kind of implies that structs in C++ are not the same as structs in C, though I didn't explicitly say it since the question mentioned nothing about C structs.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  2. public/private inheritance
    By xion in forum C++ Programming
    Replies: 3
    Last Post: 06-17-2004, 06:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21