tags:

views:

136

answers:

5

Structure's member are public by default ans class's members are private by default. We can access private data members through a proper channel (using member function). If we have access to member functions we can read/write data in private data member, so how it is secure...we are accessing it and we are changing data too.....

+3  A: 

primarily because the member functions can validate the values before storing them. eg say you have a field called 'age', which must be between 0 and 150. in a structure (or a class with a public field), you could just do obj.age = 200. whereas if you only had a setAge(int) method, that method could sanity check the value is between 0 and 150 before storing, possibly throwing an exception if necessary or just clamping the value if not.

oedo
additional: in C# you can use properties in which you can check the value before setting it.
PoweRoy
thanks a lot sir
Asad Hanif
+4  A: 

Access specifiers, such as private and public have nothing to do with security. In C++, for example, these specifications get compiled away and don't even exist in the compiled binary.

Rather, access specifiers are designed to make types easy to use correctly, and difficult to use incorrectly.

Billy ONeal
this can also be done in structures, then what was the need of classes....
Asad Hanif
@Asad Hanif: There is no need. The only difference between a class and a struct is that a `struct`'s members are public by default. Use of the keyword `class` encourages you to make more of the data members of the class `private`, which is good design. Also, when we are talking about these things, unless it's a C-style POD type, we are going to refer to it as a class, rather than a structure, even if it is declared with the `struct` keyword. Syntactically however, everything `class` does could have been accomplished with `struct`.
Billy ONeal
thanks a lot sir
Asad Hanif
+1  A: 

the public/private/protected keywords are not meant for security but rather for encapsulation. you design your class by separating the implementation (where the private/protected members are used - those actually used to implement the functionality) from the interface (where you expose the public members - which users of the class -other objects- will access/call) and thus you are later able to change the private parts without changing the interface. the outside objects only need to know about the public members and use these safely regardless of the implementation. if you are speaking about security, think that anyone could change the private members of your class if they really wanted to and knew the internal structure of the class , just by overwriting the appropriate memory locations with new values - but the problem here is not about security, it's at a lower level

matei
+3  A: 

There are only two syntactic differences between class and struct:

  1. In a class, members are by default private, whereas in a struct, they are public by default.
  2. For historical reasons, class can be used instead of typename to declare a template type parameter.

In fact, there's no difference between

struct X {
  void f() {}
private:
  int i;
};

and

class Y {
  int i;
public:
  void f() {}
};

The only way that a class is more "secure" than a struct is that it gently "presses" you towards better encapsulation by defaulting to private members.

There really was no need to introduce a new keyword class, Stroustrup himself said that on a few occasions. (Read his book The Design and Evolution of C++ if you're interested in such things.) Basically, class was introduced to emphasize the fact that a struct with member functions isn't really a "structure" anymore in the way the term was used in C: a collection of loosely coupled objects of built-in types. It's a "class" in the sense the term is used in statically typed object-oriented languages, even though syntactically, it's no real difference from a struct.

sbi
+1  A: 

Taking that classes and structs are exactly the same besides the default access, the question is how encapsulating the internal representation from the interface helps build more robust code.

The main difference is that you control when and how your class data is modified, and as such you can control your class invariants. Consider a simple vector class that has a pointer and a size. By having them private you can control how they change: if the resize method is called, both the pointer and the internal size variable will be coherently updated, keeping the invariant that accessing any position in the range [0..size) is well defined. If the member attributes were public, user code would update the storage or size without actually updating the other field, and that invariant could be broken.

Many classes have internal invariants for the correct usage. If you write a class that contains a string with user email addresses, by providing an accessor method you can control that the value passed in is a valid email address, while if the email field was public user code could reset it to anything...

The whole thing is that you control how your members are accessed and modified and that reduces the number of places where mistakes can be made and/or your chances of detecting it.

David Rodríguez - dribeas