public and private keyword
In C++, public and private keyword is called the access specifier . Can we use the member variable or function outside that class, it is define these access specifier.
class A { public : int a, b; void funA() { return a+b; } }; |
In this class A, we define member variable and function with public specifier. That means we can use these variable and function in any where in our program within main() and other classes.
class B { private : int a, b; void funB() { return a+b; } }; |
In this class B, we define member variable and function with private access specifier. So we can not use these member outside the class. If we use these member outside the class then the compiler take error and program will be stop.
Example :
This program will generate an error like this :
Written by “Shojib”
No comments:
Post a Comment