{"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 class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>What Are Access Modifiers?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\"><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 class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\">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 class=\"wp-block-paragraph\"><\/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":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"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\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Afzal Badshah, PhD\"\/>\n\t<meta name=\"google-site-verification\" content=\"B8FNGv4w1toMpU3HUnD_SN9H9YMtp1ObsLuEDuirArI\" \/>\n\t<meta name=\"p:domain_verify\" content=\"87b5ca26c36438b20581a102dc290a47\" \/>\n\t<meta name=\"yandex-verification\" content=\"0eb3e2a9157fd34d\" \/>\n\t<meta name=\"keywords\" content=\"courses,oop with c++\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_GB\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Afzal Badshah, PhD - Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Understanding Access Modifiers in C++ - Afzal Badshah, PhD\" \/>\n\t\t<meta property=\"og:description\" content=\"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\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/Access-modifiers.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/Access-modifiers.png\" \/>\n\t\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2025-01-29T04:05:00+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-02-23T04:46:35+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/web.facebook.com\/abmanduri\/\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@DrAFZALBADSHAH\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Understanding Access Modifiers in C++ - Afzal Badshah, PhD\" \/>\n\t\t<meta name=\"twitter:description\" content=\"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\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@DrAFZALBADSHAH\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/Access-modifiers.png\" \/>\n\t\t<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t\t<meta name=\"twitter:data1\" content=\"Afzal Badshah, PhD\" \/>\n\t\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"BlogPosting\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/#blogposting\",\"name\":\"Understanding Access Modifiers in C++ - Afzal Badshah, PhD\",\"headline\":\"Understanding Access Modifiers in C++\",\"author\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#person\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/afzalbadshah.com\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/Access-modifiers.png?fit=1920%2C1080&ssl=1\",\"width\":1920,\"height\":1080},\"datePublished\":\"2025-01-29T09:05:00+05:00\",\"dateModified\":\"2026-02-23T09:46:35+05:00\",\"inLanguage\":\"en-GB\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/#webpage\"},\"articleSection\":\"Courses, OOP with C++\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/afzalbadshah.com\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"name\":\"Courses\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"position\":2,\"name\":\"Courses\",\"item\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/oop-with-c\\\/#listItem\",\"name\":\"OOP with C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/oop-with-c\\\/#listItem\",\"position\":3,\"name\":\"OOP with C++\",\"item\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/oop-with-c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/#listItem\",\"name\":\"Understanding Access Modifiers in C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"name\":\"Courses\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/#listItem\",\"position\":4,\"name\":\"Understanding Access Modifiers in C++\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/oop-with-c\\\/#listItem\",\"name\":\"OOP with C++\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#person\",\"name\":\"Afzal Badshah, PhD\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/#personImage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/wp-content\\\/litespeed\\\/avatar\\\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787150587\",\"width\":96,\"height\":96,\"caption\":\"Afzal Badshah, PhD\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/\",\"name\":\"Afzal Badshah, PhD\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/#authorImage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/wp-content\\\/litespeed\\\/avatar\\\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787150587\",\"width\":96,\"height\":96,\"caption\":\"Afzal Badshah, PhD\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/#webpage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/\",\"name\":\"Understanding Access Modifiers in C++ - Afzal Badshah, PhD\",\"description\":\"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\",\"inLanguage\":\"en-GB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/author\\\/afzalbadshah-com\\\/#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/afzalbadshah.com\\\/wp-content\\\/uploads\\\/2024\\\/11\\\/Access-modifiers.png?fit=1920%2C1080&ssl=1\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/#mainImage\",\"width\":1920,\"height\":1080},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/29\\\/understanding-access-modifiers-in-c\\\/#mainImage\"},\"datePublished\":\"2025-01-29T09:05:00+05:00\",\"dateModified\":\"2026-02-23T09:46:35+05:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#website\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/\",\"name\":\"Afzal Badshah, PhD\",\"alternateName\":\"Afzal Badshah\",\"description\":\"Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence\",\"inLanguage\":\"en-GB\",\"publisher\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#person\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Understanding Access Modifiers in C++ - Afzal Badshah, PhD","description":"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","canonical_url":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/","robots":"max-image-preview:large","keywords":"courses,oop with c++","webmasterTools":{"google-site-verification":"B8FNGv4w1toMpU3HUnD_SN9H9YMtp1ObsLuEDuirArI","p:domain_verify":"87b5ca26c36438b20581a102dc290a47","yandex-verification":"0eb3e2a9157fd34d","miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"BlogPosting","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/#blogposting","name":"Understanding Access Modifiers in C++ - Afzal Badshah, PhD","headline":"Understanding Access Modifiers in C++","author":{"@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author"},"publisher":{"@id":"https:\/\/afzalbadshah.com\/#person"},"image":{"@type":"ImageObject","url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/Access-modifiers.png?fit=1920%2C1080&ssl=1","width":1920,"height":1080},"datePublished":"2025-01-29T09:05:00+05:00","dateModified":"2026-02-23T09:46:35+05:00","inLanguage":"en-GB","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/#webpage"},"isPartOf":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/#webpage"},"articleSection":"Courses, OOP with C++"},{"@type":"BreadcrumbList","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com#listItem","position":1,"name":"Home","item":"https:\/\/afzalbadshah.com","nextItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","name":"Courses"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","position":2,"name":"Courses","item":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/","nextItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/oop-with-c\/#listItem","name":"OOP with C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/oop-with-c\/#listItem","position":3,"name":"OOP with C++","item":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/oop-with-c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/#listItem","name":"Understanding Access Modifiers in C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","name":"Courses"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/#listItem","position":4,"name":"Understanding Access Modifiers in C++","previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/oop-with-c\/#listItem","name":"OOP with C++"}}]},{"@type":"Person","@id":"https:\/\/afzalbadshah.com\/#person","name":"Afzal Badshah, PhD","image":{"@type":"ImageObject","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/#personImage","url":"https:\/\/afzalbadshah.com\/wp-content\/litespeed\/avatar\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787150587","width":96,"height":96,"caption":"Afzal Badshah, PhD"}},{"@type":"Person","@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author","url":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/","name":"Afzal Badshah, PhD","image":{"@type":"ImageObject","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/#authorImage","url":"https:\/\/afzalbadshah.com\/wp-content\/litespeed\/avatar\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787150587","width":96,"height":96,"caption":"Afzal Badshah, PhD"}},{"@type":"WebPage","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/#webpage","url":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/","name":"Understanding Access Modifiers in C++ - Afzal Badshah, PhD","description":"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","inLanguage":"en-GB","isPartOf":{"@id":"https:\/\/afzalbadshah.com\/#website"},"breadcrumb":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/#breadcrumblist"},"author":{"@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author"},"creator":{"@id":"https:\/\/afzalbadshah.com\/index.php\/author\/afzalbadshah-com\/#author"},"image":{"@type":"ImageObject","url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/Access-modifiers.png?fit=1920%2C1080&ssl=1","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/#mainImage","width":1920,"height":1080},"primaryImageOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/#mainImage"},"datePublished":"2025-01-29T09:05:00+05:00","dateModified":"2026-02-23T09:46:35+05:00"},{"@type":"WebSite","@id":"https:\/\/afzalbadshah.com\/#website","url":"https:\/\/afzalbadshah.com\/","name":"Afzal Badshah, PhD","alternateName":"Afzal Badshah","description":"Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence","inLanguage":"en-GB","publisher":{"@id":"https:\/\/afzalbadshah.com\/#person"}}]},"og:locale":"en_GB","og:site_name":"Afzal Badshah, PhD - Unlocking Mastery in Parenting, Teaching, Learning, Academic, and Life Skills: Your Guide to Excellence","og:type":"article","og:title":"Understanding Access Modifiers in C++ - Afzal Badshah, PhD","og:description":"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","og:url":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/","og:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/Access-modifiers.png","og:image:secure_url":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/Access-modifiers.png","og:image:width":1920,"og:image:height":1080,"article:published_time":"2025-01-29T04:05:00+00:00","article:modified_time":"2026-02-23T04:46:35+00:00","article:publisher":"https:\/\/web.facebook.com\/abmanduri\/","twitter:card":"summary_large_image","twitter:site":"@DrAFZALBADSHAH","twitter:title":"Understanding Access Modifiers in C++ - Afzal Badshah, PhD","twitter:description":"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","twitter:creator":"@DrAFZALBADSHAH","twitter:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/Access-modifiers.png","twitter:label1":"Written by","twitter:data1":"Afzal Badshah, PhD","twitter:label2":"Est. reading time","twitter:data2":"5 minutes"},"aioseo_meta_data":{"post_id":"4923","title":null,"description":null,"keywords":null,"keyphrases":{"focus":{"keyphrase":"Access Modifiers","score":90,"analysis":{"keyphraseInTitle":{"score":9,"maxScore":9,"error":0},"keyphraseInDescription":{"score":9,"maxScore":9,"error":0},"keyphraseLength":{"score":9,"maxScore":9,"error":0,"length":2},"keyphraseInURL":{"score":5,"maxScore":5,"error":0},"keyphraseInIntroduction":{"score":9,"maxScore":9,"error":0},"keyphraseInSubHeadings":{"score":3,"maxScore":9,"error":1},"keyphraseInImageAlt":[],"keywordDensity":{"type":"best","score":9,"maxScore":9,"error":0}}},"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":true,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"BlogPosting","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2024-11-22 09:18:30","updated":"2026-02-23 04:56:58","focus_keyword":"Access Modifiers","additional_keywords":null,"truseo_locale":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/afzalbadshah.com\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/\" title=\"Courses\">Courses<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/oop-with-c\/\" title=\"OOP with C++\">OOP with C++<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tUnderstanding Access Modifiers in C++\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/afzalbadshah.com"},{"label":"Courses","link":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/"},{"label":"OOP with C++","link":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/oop-with-c\/"},{"label":"Understanding Access Modifiers in C++","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/29\/understanding-access-modifiers-in-c\/"}],"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}]}}