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
What is variable and explain rules to declare variable in c?
Explain a pre-processor and its advantages.
What are reserved words?
What is putchar() function?
What is data structure in c language?
Explain how do I determine whether a character is numeric, alphabetic, and so on?
Do pointers store the address of value or the actual value of a variable?
Where register variables are stored in c?
c language supports bitwise operations, why a) 'c' language is system oriented b) 'c' language is problem oriented c) 'c' language is middle level language d) all the above
How does struct work in c?
Why do we write return 0 in c?
What is meant by type specifiers?
Draw a flowchart to produce a printed list of all the students over the age of 20 in a class .The input records contains the name and age of students. Assume a sentinel value of 99 for the age field of the trailer record
What is meant by keywords in c?
What are static variables in c?