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



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

Answer / 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

More C Interview Questions

what defference between c and c++ ?

6 Answers  


What is const and volatile in c?

0 Answers  


What is the output of the following program main();{printf ("chennai""superkings"}; a. Chennai b. superkings c. error d. Chennai superkings

6 Answers  


which of the following shows the correct hierarchy of arithmetic operations in C a) (), **, * or/,+ or - b) (),**,*,/,+,- c) (),**,/,*,+,- d) (),/ or *,- or +

0 Answers  


Explain what is operator promotion?

0 Answers  






Is it valid to address one element beyond the end of an array?

0 Answers  


what is compiler

6 Answers  


What is the difference between int main and void main in c?

0 Answers  


How does struct work in c?

0 Answers  


what is mean by Garbage collection ? Please answer me. Advance thanks.

4 Answers   Excel,


#define f(x) main() { printf("\n%d",f(2+2)); }

5 Answers  


What is a class c rental property?

0 Answers  


Categories