{"id":4923,"date":"2025-01-29T09:05:00","date_gmt":"2025-01-29T04:05:00","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=4923"},"modified":"2026-02-23T09:46:35","modified_gmt":"2026-02-23T04:46:35","slug":"understanding-access-modifiers-in-c","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/","title":{"rendered":"Understanding Access Modifiers in C++"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What Are Access Modifiers?<\/strong><\/h3>\n\n\n\n<p>In C++, <strong>access modifiers<\/strong> are keywords used to define the level of access that members of a class (variables and methods) can have. They are essential for ensuring <strong>encapsulation<\/strong>, which is a core concept of Object-Oriented Programming (OOP). By using access modifiers, we control how the internal data of a class is accessed, modified, and protected.<\/p>\n\n\n\n<p><strong>Access modifiers<\/strong> in C++ include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>private<\/code><\/strong>: Members are accessible only within the same class.<\/li>\n\n\n\n<li><strong><code>protected<\/code><\/strong>: Members are accessible within the class and derived classes.<\/li>\n\n\n\n<li><strong><code>public<\/code><\/strong>: Members are accessible from anywhere in the program.<\/li>\n<\/ul>\n\n\n\n<p>By controlling access, access modifiers help:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Protect the internal state of a class.<\/li>\n\n\n\n<li>Promote data encapsulation.<\/li>\n\n\n\n<li>Allow controlled access to data and functionality.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Types of Access Modifiers<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>private<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Definition<\/strong>: Members marked as <code>private<\/code> are accessible only within the same class. They are hidden from derived classes and external code.<\/li>\n\n\n\n<li><strong>Use Case<\/strong>: To protect internal data and ensure that class users can&#8217;t modify sensitive data directly.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>protected<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Definition<\/strong>: Members marked as <code>protected<\/code> are accessible within the same class and any derived (child) classes. However, they are not accessible by objects of the class.<\/li>\n\n\n\n<li><strong>Use Case<\/strong>: To allow derived classes to access and modify base class data, while still hiding the data from the outside world.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>public<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Definition<\/strong>: Members marked as <code>public<\/code> are accessible from anywhere. This includes external code, objects, and even derived classes.<\/li>\n\n\n\n<li><strong>Use Case<\/strong>: For functions and variables that need to be accessed or modified by external code.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Code Exampl<\/strong>e<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>class number {<br>    private:<br>        int privateVar; \/\/ Private variable, accessible only within this class.<br><br>    protected:<br>        int protectedVar; \/\/ Protected variable, accessible within the class and derived classes.<br><br>    public:<br>        int publicVar; \/\/ Public variable, accessible from anywhere.<br><br>        \/\/ Constructor to initialize the variables<br>        number() {<br>            privateVar = 10;<br>            protectedVar = 20;<br>            publicVar = 30;<br>        }<br><br>        \/\/ Public function to display values of all members<br>        void display() {<br>            cout &lt;&lt; \"Private Variable: \" &lt;&lt; privateVar &lt;&lt; endl;<br>            cout &lt;&lt; \"Protected Variable: \" &lt;&lt; protectedVar &lt;&lt; endl;<br>            cout &lt;&lt; \"Public Variable: \" &lt;&lt; publicVar &lt;&lt; endl;<br>        }<br>};<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Explanation of the Base Class<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Private Member (<code>privateVar<\/code>)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>privateVar<\/code> is declared <code>private<\/code>, meaning it can only be accessed inside the <code>number<\/code> class. It is hidden from both derived classes and external code.<\/li>\n\n\n\n<li><strong>Purpose<\/strong>: Used to encapsulate and protect data that shouldn&#8217;t be directly modified by the outside world.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Protected Member (<code>protectedVar<\/code>)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>protectedVar<\/code> is declared <code>protected<\/code>, meaning it can be accessed within the <code>number<\/code> class and any class derived from it (like the <code>Derived<\/code> class).<\/li>\n\n\n\n<li><strong>Purpose<\/strong>: Useful for allowing derived classes to interact with base class members, while still hiding the data from external code.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Public Member (<code>publicVar<\/code>)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>publicVar<\/code> is declared <code>public<\/code>, meaning it is accessible from anywhere in the program, including objects of the class, and even in derived classes.<\/li>\n\n\n\n<li><strong>Purpose<\/strong>: Typically used for properties that need to be freely accessed or modified by external code.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Constructor<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The constructor initializes all three variables with values (<code>privateVar = 10<\/code>, <code>protectedVar = 20<\/code>, <code>publicVar = 30<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Function (<code>display<\/code>)<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>display()<\/code> function prints the values of the three variables to show how access modifiers impact the visibility of the class members.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Derived Class Code (<code>Derived<\/code> class)<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>class Derived : public number {<br>    public:<br>        \/\/ Accessing protected variable in the derived class<br>        void displayProtected() {<br>            cout &lt;&lt; \"Protected Variable (from Derived class): \" &lt;&lt; protectedVar &lt;&lt; endl;<br>        }<br>};<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Explanation of the Derived Class<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Inheriting from <code>number<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>Derived<\/code> class inherits from the <code>number<\/code> class using public inheritance. This means all public and protected members of the <code>number<\/code> class are inherited by <code>Derived<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Accessing Protected Member<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>displayProtected()<\/code> function in the <code>Derived<\/code> class prints the value of <code>protectedVar<\/code>. This is allowed because <code>protectedVar<\/code> is protected and can be accessed by derived classes.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Private Members Not Inherited<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong><code>privateVar<\/code><\/strong> is a private member of the <code>number<\/code> class and cannot be accessed directly by the <code>Derived<\/code> class. This demonstrates the limited access of private members.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Main Function Code<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>int main() {<br>    \/\/ Object of base class<br>    number obj;<br><br>    \/\/ Accessing public variable directly<br>    cout &lt;&lt; \"Public Variable: \" &lt;&lt; obj.publicVar &lt;&lt; endl;<br><br>    \/\/ Displaying all variables using the public function<br>    obj.display();<br><br>    \/\/ Object of derived class<br>    Derived derivedObj;<br><br>    \/\/ Accessing protected variable via derived class<br>    derivedObj.displayProtected();<br><br>    return 0;<br>}<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Explanation of the Main Function<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Creating <code>obj<\/code> (Object of <code>number<\/code>)<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>obj<\/code> is an instance of the <code>number<\/code> class. Using this object, we can access the public member <code>publicVar<\/code> directly.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Calling <code>display()<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>obj.display()<\/code> function is called, which prints the values of all three variables (private, protected, and public). Although <code>privateVar<\/code> is private, it can still be accessed within the class through a public function.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Creating <code>derivedObj<\/code> (Object of <code>Derived<\/code>)<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>derivedObj<\/code> is an instance of the <code>Derived<\/code> class. Since <code>Derived<\/code> inherits from <code>number<\/code>, it can access the protected member <code>protectedVar<\/code> through the <code>displayProtected()<\/code> function.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output of the Code<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Public Variable: 30<br>Private Variable: 10<br>Protected Variable: 20<br>Public Variable: 30<br>Protected Variable (from Derived class): 20<br><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Explanation of the Output<\/strong><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>Public Variable: 30<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li>This value is accessed directly from the <code>obj<\/code> object since it is a public member.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>Private Variable: 10<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li>This value is printed by the <code>display()<\/code> function. Although it is private, it can be accessed within the class.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>Protected Variable: 20<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li>This value is printed by the <code>display()<\/code> function. <code>protectedVar<\/code> is accessible within the <code>number<\/code> class.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong><code>Protected Variable (from Derived class): 20<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li>This value is printed by the <code>displayProtected()<\/code> function in the <code>Derived<\/code> class. The derived class can access protected members of the base class.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Access Summary Table<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>Access Modifier<\/strong><\/th><th><strong>Access Inside Class<\/strong><\/th><th><strong>Access in Derived Class<\/strong><\/th><th><strong>Access via Object<\/strong><\/th><\/tr><\/thead><tbody><tr><td><code>private<\/code><\/td><td>\u2705<\/td><td>\u274c<\/td><td>\u274c<\/td><\/tr><tr><td><code>protected<\/code><\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u274c<\/td><\/tr><tr><td><code>public<\/code><\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h3>\n\n\n\n<p>In this tutorial, we explored the three main <strong>access modifiers<\/strong> in C++:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>private<\/code><\/strong>: Restricts access to within the class.<\/li>\n\n\n\n<li><strong><code>protected<\/code><\/strong>: Allows access within the class and derived classes, but not by external code.<\/li>\n\n\n\n<li><strong><code>public<\/code><\/strong>: Grants access to members from anywhere in the program.<\/li>\n<\/ul>\n\n\n\n<p>By using these access modifiers, we can control data visibility, ensuring that class members are accessible in a controlled and secure manner. Access modifiers are fundamental in achieving <strong>encapsulation<\/strong> and <strong>data hiding<\/strong>, which are essential principles of object-oriented programming.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What Are Access Modifiers? In C++, access modifiers are keywords used to define the level of access that members of a class (variables and methods) can have. They are essential for ensuring encapsulation, which is a core concept of Object-Oriented Programming (OOP). By using access modifiers, we control how the internal data of a class is accessed, modified, and protected. Access modifiers in C++ include: By controlling access, access modifiers help: Types of Access Modifiers Code Example class number {&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":5112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[351,637],"tags":[],"class_list":["post-4923","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-courses","category-oop-with-c"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/Access-modifiers.png?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-1hp","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/4923","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=4923"}],"version-history":[{"count":3,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/4923\/revisions"}],"predecessor-version":[{"id":5194,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/4923\/revisions\/5194"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/5112"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=4923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=4923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=4923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}