Skip to main content

C++ Encapsulation

C++ Encapsulation

As you all know that C++ is rich in Objects and Classes. So, it comes under Object Oriented Programming category. It is the first successful Object Oriented Programming Language, with all the features of OOPs along with some varied concepts. So abstraction and encapsulation were the first basic sets of concepts of OOPs. Here Abstraction deals with the basic characteristics of an object which distinguishes from other types of objects and hence allows crisply define conceptual boundaries, with respect to the perspective of the viewer.

Concept of Encapsulation

Unable to deal with the complexity of an object, human chooses to ignore its non-essential details and concentrate on the details which are essential to our understanding. You can place a simple context of object model, that abstraction is “looking for what you want” within an object or class. But there is another major concept connected to abstraction which is called encapsulation.
So basically, encapsulation can be defined as the process of hiding all of the details of an object that do not throw in or dealt with its essential characteristics. Encapsulation can also be defined as preventing access to non-essential details of classes or its objects. Abstraction and encapsulation have close bonding among each other. Encapsulation assists abstraction by providing a means of suppressing the non-essential details.

Use of Access Specifiers

Access specifiers are used to determine whether any other class or function can access member variables and functions of a particular class or within a program.
C++ provides its programmers with three access specifiers. These are:
  • public
  • private
  • protected
Access specifiers create the boundaries among the accessible and inaccessible sections of any class. It provides with programmer the privilege to design the class for demarcating which section of the class need to be obscured from the user point of view and further which should be offered to them as an interface for the class.
  • Public: Here, the class members declared under public has to be obtainable and accessible to everyone. All those data members and member functions acknowledged as public can be accessed by other classes also.
  • Private: Private is used where no one can obtain or access the class members declared as private outside that class. When any one tries to access or avail the private member of that class a compile time error gets generated. By default, all members of a class remain in ‘private’ access mode.
  • Protected: It is a special purpose access specifier which is similar to private but it creates class member inaccessible outside that class. But it can get accessed by any sub – class of that class.

Example of C++ Having the Concept of Inheritance

Example:
#include <iostream>
using namespace std;

class rectangle
{
   
private:
   
int l,b;
   
public:
   rectangle
(int x=2,int y=4)
   
{
      l
=x;
      b
=y;
      cout
<<"i am parametrized";
   
}
   
/* rectangle()
   {
      cout<<"i am default";
   }*/

   
void area()
   
{
      cout
<<"\narea is = "<<l*b;
   
}
};

int main()
{
   rectangle r
;
   r
.area();
   rectangle r1
(3,6);
   r1
.area();
   rectangle r2
(10);
   r2
.area();
}

Comments

For Programs Click Here

Popular posts from this blog

Syllabus

Syllabus  C Programming Tutorials C Tutorial C Introduction History of C Programming Language C Installation C Program Structure C Input and Output (I/O) C Format Specifiers Declaration & Assignments C Tokens C Identifiers C Keywords C Constants C Operators C Data Types C Variables C Preprocessors C Type Casting C Custom Header File Flow Control C Decision Making C if Statements C if-else Statements C Nested if-else Statements C else-if Statements C goto Statement C switch Statements C Loops C while loops C do while loops C for loops Functions C Functions C Function Arguments C Library Functions C Variable Scope Arrays & Strings C Arrays C Strings Pointers C Pointers C Dynamic Memory Allocation Structure & Union C Structures C Unions File I/O C File Handling C fopen C fclose C getc C putc C getw C putw C fprintf C fscanf C fgets C fputs C feof                                     ...

Syllabus

Python Tutorials Python Tutorial Python Overview Python Installation Basics of Python Programming Python Operators Python Keywords Python Numbers Python Strings Python Data Types Python Variables Python Lists Python Tuples Python Date and Time Python Decision Making Python Loops Python File Handling Python Dictionaries Python Functions Python Modules Python Exceptions Handling Python Object Oriented Inheritance in Python Python Regular Expressions Python Networking Programming Python Multithreaded Programming Python CGI Programming Python Database Connection Python Metaprogramming Python Data Processing And Encoding Python GUI Programming

C++ Data Abstraction

C++ Data Abstraction Object oriented programming offers various features to write programs with various concepts that help to minimize problems and increase flexibility in program. One of the features of object oriented programming is Data abstraction. In this chapter you will learn about how the concept data abstraction is carried out within the C++ program. What is Data abstraction? Data abstraction allows a program to ignore the details of how a data type is represented. Abstraction (derived from a Latin word  abs , meaning away from and  trahere , meaning to draw) refers to the act of representing essential features without including the background details or explanations. C++ classes use the technique of abstraction and are defined as a list of abstract attributes such as width, cost, size etc and functions to operate on these attributes. They put in a nutshell all the essential properties of an object that are required to be created. The attributes are therefore called...