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

write a c program to print "Welcome" without using semicolon in the whole program ??

15 Answers   Infosys, TCS,


How can I call fortran?

0 Answers  


What is calloc() function?

0 Answers  


int j =15,i; for (i=1; 1<5; ++i) {printf ("%d%d ",j,i); j = j-3; }

2 Answers  


How would you rename a function in C?

0 Answers   Tech Mahindra,






Are there any problems with performing mathematical operations on different variable types?

0 Answers  


can we change the default calling convention in c if yes than how.........?

0 Answers   Aptech,


What is the difference between malloc() and calloc() function in c language?

0 Answers  


Can you think of a way when a program crashed before reaching main? If yes how?

2 Answers  


Using which language Test cases are added in .ptu file of RTRT unit testing???

0 Answers  


Can a program have two main functions?

0 Answers  


where do we use volatile keyword?

1 Answers  


Categories