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
Explain the difference between null pointer and void pointer.
Define Spanning-Tree Protocol (STP)
explain what is an endless loop?
Is c dynamically typed?
Write a program to swap two numbers without using third variable in c?
List the difference between a 'copy constructor' and a 'assignment operator' in C?
Why we use break in c?
Under what circumstances does a name clash occur?
I need previous papers of CSC.......plz help out by posting them.......
What is the difference between near, far and huge pointers?
Write a program to print factorial of given number without using recursion?
Given a valid 24 hour format time find the combination of the value and write a program ,do not hard the value and if any other inputs provided should work with the logic implemented Input: 11:30 Output: 13:10 Input: 18:25 Output: 21:58
Explain what is a 'null pointer assignment' error? Explain what are bus errors, memory faults, and core dumps?
What Is The Difference Between Null And Void Pointer?
Explain the advantages of using macro in c language?