

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This document about Encapsulation
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!
Encapsulation & Information hiding
When the size of a program reaches beyond a few thousand lines, two practical problems appear. From the programmer’s point of view, having such a program appear as a single collection of subprograms does not impose an adequate level of organization on the program to keep it intellectually manageable. One solution is to organize the program into syntactic containers that include groups of logically related subprograms and data. These syntactic containers are often called modules, and the process of designing them is called modularization. The second practical problem for larger programs is recompilation. In case of a small program, recompiling the whole program after each modification is not costly. But when programs grow beyond a few thousand lines, the cost of recompilation ceases to be insignificant. So there is an obvious need to find ways to avoid recompilation of the parts of a program that are not affected by a change. This can be provided by organizing programs into collection of subprograms and data, each of which can be compiled without recompilation of the rest of the program. Such a collection is called a compilation unit. An encapsulation is a grouping of subprograms and the data they manipulate. An encapsulation, which is either separately or independently compilable, provides an abstracted system and a logical organization for a collection of related computations. Therefore, an encapsulation solves both of the practical problems described above. Encapsulation are often placed in libraries and made available for reusing programs other than those for which they were written.
Information Hiding Information hiding is manifested by enabling programmers to restrict access to certain data and code. This way, a class may prevent other classes and functions from accessing its data members and some of its member functions, while allowing them to access its non-restricted members
Abstract Data Types in C++
C++ provides the class, which more directly supports abstract data types. C++ classes are types. A C++ program, variables are declared to be of class types. A C++ program unit that declares an instance of a class can also access any of the public entities in that class, but only through an instance of the class.
Encapsulation in C++ The classes of C++ are based on the structure in C. The data defined in a class are called data members; the functions defined in a class are called member functions. All of the instances of a class share a single set of member functions, but each instance gets its own set of the class’s data members. Although class instances can also be static and heap- dynamic, we consider only stack-dynamic classes here. The instances of such classes are always created by elaboration of an object declaration. Classes can have heap-dynamic data members, so that even though a class instance is stack-dynamic, it can include data members that are heap-dynamic and allocated from the heap. C++ provides the new and delete operators to manage the heap. A member function of a class can be defined in two distinct ways: The complete definition can appear in the class, or only in its header. When both the header and the body of a member function appear in the class definition, the member function is implicitly inlined.
Information Hiding A C++ class can contain both hidden and visible entities. Entities that are to be hidden are placed in a private clause, and visible, or public , entities are written in a public clause. The public clause therefore describes the interface to class objects. There is also a third category of visibility, protected. C++ allows the user to include constructor functions in class definitions, which are used to initialize the data members of newly created objects. A constructor may also allocate the heap-dynamic data members of the new object. Constructors are implicitly called when an object of the class type is created.