• Now Online : 30
  • admin@codemyne.net

Data Abstraction

Data abstraction is the ability to create user defined data types for modeling real world objects using built-in data type and a set of permitted operators. The construct that helps create such data types and implement data abstraction in C++ is the class. It helps in the creation and usage of user-defined or Abstract Data types.


Encapsulation or data hiding is the mechanism that associates the code and the data that it manipulates into a single unit and keeps them safe from external interference and misuse. The class features of C++ implements the concepts encapsulation by providing access specifiers. Access specifiers control the visibility status of the members of a class. There are three access specifiers in C++, private, public and protected.

The members of a class declared private are accessible only within the class, i.e only to other members of the same class. The members declared are public as accessible anywhere from within the class, or outside the class. The protected access specifier will be discussed in detail in the next chapter. The following example illustrates the usage of private and public access specifiers.

class date
    int dd;
    int mm;
    int yy;
    public :
    void valid (int d, int m)
        cout<<”\n invalid date”;
        cout<<”\n invalid month”;

If the class is declared globally, the public member, have file (i.e.. global) scope. If it is declared within a block, the public members have local scope. Regardless of the place of the declaration, private members always have class scope.

Access specifiers serve the important purpose of defining the boundary between the accessible and inaccessible parts of a class. It is the privilege of the designer of the class to determine which parts of a class must be obscured from the user’s view and which should not.

The design of a data type would be incomplete if the validity of the data stored with in it is questionable. In the case of the class date, if the user was permitted to modify the values of dd, mm and yy, directly, the possibility of illegal values being entered is relatively high. That is why the three variables dd, mm and yy have been hidden from the user’s view in the previous example, if the date has to be set, the appropriate functions can be invoked and the values set after making proper validations.

The member data and functions are accessed just like in the structure, with the help of the member access operator (‘.’). For example

date date1;
date1.valid(10, 12);
/*not valid, since dd is a private member perfectly valid*/

The variable date1, of type date, is called an object. An object is an instance of a class. If the object happens to be a pointer, the operator to be used is (‘->’). For example, the same members could be accessed as follows:

date *date1;
date1 ->dd=10;
date1 ->valid (10, 12);
/*not valid, since dd is a private member perfectly valid*/

While designing a class the designer must examine each member of the class before declaring it as private or public. Most class definitions keep the data elements within the private section and the methods in the public section. This is because the validity of the data is best kept ensured by methods specific to the class. In some cases, the necessity might also arise to declare some methods as private. These methods will act as interface functions, accessible only by other methods within the class.

Dynamic Memory Manipulation:

C++ provides two special operators to perform memory management dynamically. These are the new operator for dynamic memory allocation and the delete operator for dynamic memory deallocation. These operators offer dynamic memory management similar to the library functions malloc() and free().

The new operator:

The general format of the new operator is as follows:

      Datatype *new Datatype [size in integer];

Where data type is the return type, the pointer to that type which will be returned; new is the new operator, data type is again the type for which memory is to be allocated and size is the number of items to be allocated (this is optional). The statement,

date *date1;
date1=new date

allocates memory for an integer and returns a pointer of type int, which is assigned to int_ptr. This statement is similar to the C statement

      date1 = (date *)malloc(sizeof(date));

The delete operator:

The counter part of the new operator, delete, ensures the safe and efficient use of memory. This operator is used to free the block of memory allocated by the new operator. Although the memory allocated is returned to the system when the program terminates, it is safer to explicitly free memory using the delete operator. The format of this operator is

      delete pointer_variable;

where pointer variable is the pointer returned through the new operator. The C++ statement

      date date1;

for deleting the object date1 is equivalence the C statement



If the new operator is used to explicitly allocate memory to a variable, the delete operator must be used to destroy it. Though the variable goes out of scope when the program gets terminated, the memory area allocated by the new operator is not released to the system unless the delete operator explicitly releases it.

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment