You are here:

C++/about oops


what is the definition of encapsulation?
if i declare a data member as public whether it fulfill the criteria of encapsulation in a class

Hello, rajesh kushwaha. Thank you for the question.

In regards to programming, data encapsulation refers to the ability to hide data in a program by means of indirect accessors and mutators to it. Declaring data members public would not be data encapsulation by many standards. That also depends on your criteria you have to follow. Here is a quick example of an accessor and mutator to an encapsulated data member:

class Test
int x;
int GetX() {return x;}
int SetX(const int _x) {x = _x;}

int main()
Test t;

std::cout << t.GetX() << '\n';
return 0;

That shows how to get and set an object's encapsulated data member, in case you were confused. Of course, there is always the argument that if you're going to be Getting and Setting data members all the time, why not make them public? Most people consider this bad OOP. The overhead of compiler implicit function calls is virtually nonexistent because the compiler inlines those function calls at run time.

I hope this information was helpful.

- Eddie


All Answers

Answers by Expert:

Ask Experts




I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by I also know a good deal about graphics via OpenGL, and GUIs.


I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2017 All rights reserved.