{"id":126,"date":"2026-05-21T04:46:00","date_gmt":"2026-05-21T04:46:00","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=126"},"modified":"2026-05-22T11:15:25","modified_gmt":"2026-05-22T11:15:25","slug":"access-specifiers","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=access-specifiers","title":{"rendered":"Access Specifiers"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Access specifiers in C++ are keywords used to control the visibility and accessibility of class members. They help protect data and support the concept of encapsulation in Object-Oriented Programming (OOP).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Access Specifiers?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Access specifiers define who can access variables and functions inside a class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They are used to control data security and program structure.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Access Specifiers in C++<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">C++ provides three main access specifiers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Public<\/li>\n\n\n\n<li>Private<\/li>\n\n\n\n<li>Protected<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Public Access Specifier<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Public members can be accessed from anywhere in the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Public Access<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>class Student {<br><br>public:<br><br>    string name;<br><br>    void display() {<br><br>        cout &lt;&lt; name;<br>    }<br>};<br><br>int main() {<br><br>    Student s1;<br><br>    s1.name = \"Ali\";<br><br>    s1.display();<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Ali<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. Private Access Specifier<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Private members can only be accessed inside the class. They cannot be accessed directly from outside the class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Private Access<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>class Student {<br><br>private:<br><br>    string name;<br><br>public:<br><br>    void setName(string n) {<br><br>        name = n;<br>    }<br><br>    void display() {<br><br>        cout &lt;&lt; name;<br>    }<br>};<br><br>int main() {<br><br>    Student s1;<br><br>    s1.setName(\"Ahmed\");<br><br>    s1.display();<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Ahmed<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Protected Access Specifier<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Protected members are similar to private members, but they can also be accessed in derived classes through inheritance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Protected Access<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>class Base {<br><br>protected:<br><br>    int number;<br>};<br><br>class Derived : public Base {<br><br>public:<br><br>    void setValue() {<br><br>        number = 50;<br>    }<br><br>    void show() {<br><br>        cout &lt;&lt; number;<br>    }<br>};<br><br>int main() {<br><br>    Derived d1;<br><br>    d1.setValue();<br><br>    d1.show();<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>50<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Default Access Specifier<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In a class, if no access specifier is written, the default is <code>private<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Test {<br><br>    int x; \/\/ private by default<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between Access Specifiers<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Access Specifier<\/th><th>Accessible Inside Class<\/th><th>Accessible Outside Class<\/th><th>Accessible in Derived Class<\/th><\/tr><\/thead><tbody><tr><td>Public<\/td><td>Yes<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Private<\/td><td>Yes<\/td><td>No<\/td><td>No<\/td><\/tr><tr><td>Protected<\/td><td>Yes<\/td><td>No<\/td><td>Yes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How Access Specifiers Work<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Public members are open to all<\/li>\n\n\n\n<li>Private members are hidden from outside<\/li>\n\n\n\n<li>Protected members allow controlled inheritance access<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a bank account:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Account balance = private<\/li>\n\n\n\n<li>Deposit and withdraw functions = public<\/li>\n\n\n\n<li>Special banking features for branch systems = protected<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This protects sensitive information while allowing controlled access.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Access Specifiers<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Protect sensitive data<\/li>\n\n\n\n<li>Improve code security<\/li>\n\n\n\n<li>Support encapsulation<\/li>\n\n\n\n<li>Control data access<\/li>\n\n\n\n<li>Make programs more organized<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Access Specifiers are Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Access specifiers are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prevent unauthorized access<\/li>\n\n\n\n<li>Improve program reliability<\/li>\n\n\n\n<li>Support secure programming<\/li>\n\n\n\n<li>Help create modular applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Applications of Access Specifiers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Access specifiers are widely used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Banking software<\/li>\n\n\n\n<li>Student management systems<\/li>\n\n\n\n<li>Medical applications<\/li>\n\n\n\n<li>Large enterprise software<\/li>\n\n\n\n<li>Secure data handling systems<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Access specifiers in C++ control how class members are accessed inside and outside a class. Public, private, and protected access levels help improve data security, program organization, and encapsulation, making them essential for Object-Oriented Programming.<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779448587376\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced C++ > Object-Oriented Programming > Access Specifiers<\/span><\/span><\/div>","protected":false},"menu_order":39,"template":"","class_list":["post-126","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Access Specifiers - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ access specifiers with examples of public, private, and protected members to understand encapsulation and secure data access.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Access Specifiers - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ access specifiers with examples of public, private, and protected members to understand encapsulation and secure data access.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T11:15:25+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=access-specifiers\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Access Specifiers - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T04:46:00+00:00\",\"dateModified\":\"2026-05-22T11:15:25+00:00\",\"description\":\"Learn C++ access specifiers with examples of public, private, and protected members to understand encapsulation and secure data access.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced C++ > Object-Oriented Programming > Access Specifiers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Access Specifiers - Learn C++Language with GiGz.PK","description":"Learn C++ access specifiers with examples of public, private, and protected members to understand encapsulation and secure data access.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Access Specifiers - Learn C++Language with GiGz.PK","og_description":"Learn C++ access specifiers with examples of public, private, and protected members to understand encapsulation and secure data access.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T11:15:25+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=access-specifiers","url":"https:\/\/gigz.pk\/","name":"Access Specifiers - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T04:46:00+00:00","dateModified":"2026-05-22T11:15:25+00:00","description":"Learn C++ access specifiers with examples of public, private, and protected members to understand encapsulation and secure data access.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Advanced C++ > Object-Oriented Programming > Access Specifiers"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/126","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}