Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

OOPS - Abstract Data Types in C, Study notes of Object Oriented Programming

This document about Encapsulation, Abstract Data Types in C , Encapsulation, Information Hiding, A Related Language: Java, destructor.

Typology: Study notes

2010/2011

Uploaded on 09/04/2011

vrunda
vrunda 🇮🇳

4.1

(21)

76 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Encapsulation
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. On 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.
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
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
pf2

Partial preview of the text

Download OOPS - Abstract Data Types in C and more Study notes Object Oriented Programming in PDF only on Docsity!

Encapsulation 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. On 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.

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 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 head-dynamic data members of the new object. Constructors are implicitly called when an object of the class type is created. A C++ class can also include a function called a destructor , which is implicitly called when the lifetime of an instance of the class ends. All heap-dynamic objects live until explicitly deallocated with the delete operator. As stated above, stack-dynamic class instances can contain heap-dynamic data members. The destructor function for such an instance can include a delete operator on the heap-dynamic members to deallocate their heap space. Destructors are often used as a debugging aid, in which case they simply display or print the values of some or all the of the object’s data members before those members are deallocated.

A Related Language: Java Java support for abstract data types is very similar to that of C++. There are, however, a few important differences. All user-defined data types in Java are classes and all objects are allocated from the heap and accessed through reference variables. Another difference between the support for abstract data types in Java and that of C++ is that subprograms (methods) in Java can only be defined in classes. So one cannot have just function headers in Java classes. Rather than having private and public clauses in its class definitions, in Java private and public are modifiers that can be attached to method and variable definitions. While C++ depends on classes as their only encapsulation construct, Java includes a second one at a level above classes, the package. Packages are partial friends of one another. Partial here means that the entities defined in a class in a package that either are public or protected or have no access modifiers are said to have package scope, because they are visible throughout the package.