C++ Access Specifiers In Hindi

📔 : C++ 🔗

पिछले topics में आपने देखा होगा कि Properties / Method को define करने से पहले public keyword का use किया गया था। वह Class Properties / Method का एक visibility / access specifier type ही था।


किसी भी class में किसी भी type के members (Static या Non Static Properties / Methods ) को define करने से पहले उस Properties / Method की visibility define करनी पड़ती है।


Class Visibility का मतलब Property / Method का scope define करना होता है। की वह Property / Method कहाँ - कहाँ Accessible होगा। class visibility को define करने वाले keywords को ही Access Specifiers कहते हैं। C++ में Class Visibility 3 Types की होती है -

C++ private Specifier

private Property / Method सिर्फ और सिर्फ Class के अंदर ही accessible होंगे। Class के बाहर कहीं भी इन्हे access नहीं कर सकते हैं। by default C++ में defined सभी Properties / Methods private ही होते हैं। लेकिन फिर भी अगर आप manually define करना चाहते हैं तो private keyword का use करके किसी class के Properties / Methods को as a private define कर सकते हैं। और अगर आप class के बाहर access करने की कोशिश करते हैं तो कुछ इस तरह से error मिलती है।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define class.
class Test{
  // define a property as private.
  private : 
  string name = "learnhindituts";
};

int main() {
  // try to access that property.
  Test test;
  cout << test.name;
  return 0;
}
Output
In function 'int main()':
error: 'std::string Test::name' is private within this context
14 |   cout << test.name;
   |                ^~~~

example में आप देख सकते हैं , कि private property name को class के बाहर access करने का try किया तो कुछ इस तरह से error मिली।

C++ public Specifier

किसी Class में public keyword का use करके define किये गए Property / Method को कहीं से भी access कर सकते हैं। Class के अंदर भी , बाहर भी या इसके Child class में भी access कर सकते हैं।


ऊपर दिए गए example में defined private property को अगर हम public define करते हैं तो उस property को easily access कर सकते हैं , जैसा कि नीचे example में दिखाया गया है।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

class Test{
  // now define as a public property.
  public : 
  string name = "learnhindituts";
};

int main() {
  Test test;
  
  // now we can easily access that.
  cout << test.name;
  return 0;
}
Output
learnhindituts

C++ protected Specifier

protected Property / Method या तो class के अंदर या Inheritance में इसकी child class में accessible होंगे , इसके अलावा कहीं पर भी इन्हे access नहीं कर सकते हैं , हालाँकि Inheritance को आप आगे detail में पढ़ेंगे। इसके लिए protected keyword की ही use किया जाता है।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define class.
class A{
  // define a property as protected so that we can access it's child class.
  protected : 
  string property_a = "Class A Property";
};

// define another class that inherit A class.
class B : A{
  // we can access Class A's property_a only inside class.
  public : 
    B(){
      cout << property_a;
    }
};

int main() {
  // just create class B object.
  B bObj;
  return 0;
}
Output
Class A Property

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers