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 |
1.int a=10; 2.int b=20; 3. //write here 4.b=30; Write code at line 3 so that when the value of b is changed variable a should automatically change with same value as b. 5.
how should functions be apportioned among source files?
How arrays can be passed to a user defined function
What are the header files used in c language?
What is the best way of making my program efficient?
What compilation do?
7 Answers Geometric Software, Infosys,
Q. where is the below variables stored ? - volatile, static, register
How to compare array with pointer in c?
What are the types of arrays in c?
Can u return two values using return keyword? If yes, how? If no, why?
What is the purpose of Scanf Print, getchar, putchar, function?
What does %c mean in c?