How does C++ help with the tradeoff of safety vs. usability?

Answer Posted / abalonesoft

In C, encapsulation was accomplished by making things static
in a compilation unit or module. This prevented another
module from accessing the static stuff. (By the way, static
data at file-scope is now deprecated in C++: don't do that.)

Unfortunately this approach doesn't support multiple
instances of the data, since there is no direct support for
making multiple instances of a module's static data. If
multiple instances were needed in C, programmers typically
used a struct. But unfortunately C structs don't support
encapsulation. This exacerbates the tradeoff between safety
(information hiding) and usability (multiple instances).

In C++, you can have both multiple instances and
encapsulation via a class. The public part of a class
contains the class's interface, which normally consists of
the class's public member functions and its friend
functions. The private and/or protected parts of a class
contain the class's implementation, which is typically where
the data lives.

The end result is like an "encapsulated struct." This
reduces the tradeoff between safety (information hiding) and
usability (multiple instances).

Is This Answer Correct ?    3 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Do array subscripts always start with zero?

788


Who developed c language and when?

588


What is a shell structure examples?

595


What is the difference between test design and test case design?

1574


What are near, far and huge pointers?

650






What is character set?

687


What is c language used for?

562


Why isnt any of this standardized in c?

641


What is oops c?

616


In the DOS enveronment, normal RAM that resides beyond the 1mb mark. a) expanded memory b) swapped memory c) Extended memory d) none

726


Can we compile a program without main() function?

637


What is an lvalue in c?

698


What are the standard predefined macros?

638


Can we replace the struct function in tree syntax with a union?

784


Synonymous with pointer array a) character array b) ragged array c) multiple array d) none

620