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

Can we compile a program without main() function?

0 Answers  


How do I determine whether a character is numeric, alphabetic, and so on?

0 Answers  


What is volatile c?

0 Answers  


write a program to fined second smallest and largest element in a given series of elements (without sorting)

9 Answers   Yahoo,


Write a program of prime number using recursion.

0 Answers   Aspiring Minds,






provide an example of the Group by clause, when would you use this clause

0 Answers  


main() { float a=3.2e40; printf("%d",a); }

9 Answers   Satyam,


what does the following function print? func(int i) { if(i%2)return 0; eale return 1; } main() { int =3; i=func(i); i=func(i); printf("%d",i);}

9 Answers   TCS,


What is a ternary operator in c?

0 Answers  


Why is c called "mother" language?

0 Answers  


Did c have any year 2000 problems?

0 Answers  


What is meant by gets in c?

0 Answers  


Categories