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?
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?
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.
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
>structures are in c++ to make it compatible with c
Yes, but as Prelude has pointed out C_struct != C++_struct.
>Yes, but as Prelude has pointed out C_struct != C++_struct.
He did?
I code.
I think.
Ergo, I think in code.
>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.