Lorem ipsum dolor sit amet, consectetur adipiscing elit. Test link
Posts

8.9 Data Encapsulation

1 min read
Image result for Data Encapsulation c++ 


Encapsulation is the method of combining the data and functions inside a class. This hides the data from being accessed from outside a class directly, only through the functions inside the class is able to access the information.

This is also known as "Data Abstraction", as it gives a clear separation between properties of data type and the associated implementation details. There are two types, they are "function abstraction" and "data abstraction". Functions that can be used without knowing how its implemented is function abstraction. Data abstraction is using data without knowing how the data is stored.

C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We know that class can have private, protected and public members. By default, all items defined in a class are private. For example: class add { public: double getadd(void) { return no1 + no2; } private: double no1; double no2; }; The variables no1 & no2 are private. This means that they can be accessed only by other members of the add class, and not by any other part of your program. This is one way encapsulation is achieved.

To make parts of a class public (i.e., accessible to other parts of your program), you must declare them after the public keyword. All variables or functions defined after the public specifier are accessible by all other functions in your program.

Making one class a friend of another exposes the implementation details and reduces encapsulation. The ideal is to keep as many of the details of each class hidden from all other classes as possible.

                      9.1 File Handling          NEXT >>

You may like these posts

  •    A variable in C++ is a name for a piece of memory that can be used to store information. There are many types of variables, which determines the size and la…
  • refers to where variables is declared. It can be Inside a function or a block which is called local variables, In the definition of function parameters which is called f…
  • /* This Program prints Hello World on screen */ #include <iostream.h> using namespace std; int main () { cout << "Hello World!"; return 0; } 1 . /* This program ..…
  • auto The default class. Automatic variables are local to their block. Their storage space is reclaimed on exit from the block. register If possible, the variable will be…
  •   An operator is a symbol. Compiler identifies Operator and performs specific mathematical or logical operation. C provides following operators : # Arithmetic Operators # L…
  • Array is a fixed size collection of similar data type items. Arrays are used to store and access group of data of same data type. Arrays can of any data type. Arrays must…

Post a Comment