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:
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.
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(); }
No comments:
Post a Comment