{"id":7126,"date":"2025-01-22T09:00:00","date_gmt":"2025-01-22T04:00:00","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=7126"},"modified":"2026-03-17T19:02:55","modified_gmt":"2026-03-17T14:02:55","slug":"encapsulation-in-c-a-beginner-guide","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/","title":{"rendered":"Encapsulation in C++: A Beginner Guide"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Encapsulation in object-oriented programming is a core principle. It helps keep data safe and programs modular. Imagine a School Management App: students can view their class (through a getter), but only the administration system can update it (using a setter). This story highlights how encapsulation mirrors real-world roles. Encapsulation ensures data safety and modular design.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Encapsulation in C++ | OOP Lecture | Data Hiding Explained with Example\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/za5_bII_qN4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine using a car or a smartphone: you interact with simple controls on the surface, while the complicated wiring and mechanisms are hidden inside. This is exactly what encapsulation provides in software engineering.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>\u201cEncapsulation is the practice of hiding the internal details and exposing only what is necessary through a well-defined interface.\u201d<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">This principle simplifies usage by concealing complexity, allows internal changes without breaking external code, and protects data integrity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Access Specifiers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Access specifiers in C++ determine the visibility and accessibility of members inside and outside of a class. They help enforce encapsulation by controlling how data and functions can be used.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Public<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Public members are the part of a class interface that can be freely accessed from outside. They usually represent operations the object exposes.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/7.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-7127\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/7.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/7.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/7.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/7.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/7.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/7.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/7.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Public Access Specifiers<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Example {\npublic:\n    int x; \/\/ accessible everywhere\n};\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Accessible from outside the class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Private<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Private members hold a class\u2019s internal details and are not visible outside the class. Use <code>private<\/code> to enforce invariants and prevent arbitrary external mutation; only member functions (and declared friends) can access them.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/9-1.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-7128\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/9-1.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/9-1.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/9-1.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/9-1.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/9-1.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/9-1.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/9-1.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Private Access Specifiers<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Example {\nprivate:\n    int y; \/\/ accessible only within the class\n};\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Restricted to the class itself.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Protected<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Protected members are intended for collaboration with derived classes. They enable extension in subclasses while staying hidden from non-member, non-friend code\u2014use sparingly to avoid tight coupling to internals.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"640\" height=\"360\" src=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/11.jpg?resize=640%2C360&#038;ssl=1\" alt=\"\" class=\"wp-image-7129\" srcset=\"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/11.jpg?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/11.jpg?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/11.jpg?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/11.jpg?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/11.jpg?resize=480%2C270&amp;ssl=1 480w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/11.jpg?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/11.jpg?w=1280&amp;ssl=1 1280w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><figcaption class=\"wp-element-caption\">Protected Access Specifiers<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Example {\nprotected:\n    int z; \/\/ accessible in class and derived classes\n};\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Accessible within the class and derived classes.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Access Specifier<\/th><th>Same Class<\/th><th>Derived Class<\/th><th>Outside World<\/th><\/tr><\/thead><tbody><tr><td>public<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u2705<\/td><\/tr><tr><td>protected<\/td><td>\u2705<\/td><td>\u2705<\/td><td>\u274c<\/td><\/tr><tr><td>private<\/td><td>\u2705<\/td><td>\u274c<\/td><td>\u274c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstrating Access Specifiers with a Student Class<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To better understand the behavior of <code>public<\/code>, <code>private<\/code>, and <code>protected<\/code>, let\u2019s create a <code>Student<\/code> class. This class will have data members such as <code>name<\/code>, <code>className<\/code>, and <code>address<\/code>, and we will experiment with placing them under different access specifiers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student {\npublic:\n    string name;      \/\/ Public: accessible directly from outside\n\nprivate:\n    string className; \/\/ Private: hidden from outside, only accessible inside class\n\nprotected:\n    string address;   \/\/ Protected: accessible in this class and in derived classes\n};\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By changing the access specifier, you can observe which members are accessible from outside, which are hidden, and which are reserved for inheritance.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Complete Program Demonstrating Access Specifiers<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Student {\npublic:\n    string name;   \/\/ public\n\nprivate:\n    string className; \/\/ private\n\nprotected:\n    string address;   \/\/ protected\n\npublic:\n    void showInfo() {\n        cout &lt;&lt; \"Name: \" &lt;&lt; name &lt;&lt; endl;\n        cout &lt;&lt; \"Class: \" &lt;&lt; className &lt;&lt; endl;\n        cout &lt;&lt; \"Address: \" &lt;&lt; address &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Student s;\n    s.name = \"Ali\";        \/\/ allowed (public)\n    \/\/ s.className = \"BSSE\"; \/\/ error (private)\n    \/\/ s.address = \"City\";   \/\/ error (protected)\n\n    s.showInfo();\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output (conceptual)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Name: Ali\nClass: \nAddress: \n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This simple program demonstrates how public members are accessible, while private and protected members are restricted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Getters and Setters for Private Members<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When class data members are marked as <code>private<\/code>, they cannot be accessed directly from outside the class. To read or write their values safely, we use special functions called <strong>getter<\/strong> and <strong>setter<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Setter functions<\/strong> allow assigning a value to a private member in a controlled way.<\/li>\n\n\n\n<li><strong>Getter functions<\/strong> allow retrieving the value of a private member without exposing it directly.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example with Student Class<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Student {\nprivate:\n    string className; \/\/ private member\n\npublic:\n    void setClassName(string c) {\n        className = c;   \/\/ setter assigns value\n    }\n\n    string getClassName() {\n        return className; \/\/ getter returns value\n    }\n};\n\nint main() {\n    Student s;\n    s.setClassName(\"BSSE\");      \/\/ using setter to set value\n    cout &lt;&lt; s.getClassName();    \/\/ using getter to get value\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The private member <code>className<\/code> cannot be accessed directly.<\/li>\n\n\n\n<li>The setter <code>setClassName<\/code> provides a safe way to assign a value.<\/li>\n\n\n\n<li>The getter <code>getClassName<\/code> provides a way to read the value.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This ensures <strong>data hiding<\/strong> and <strong>controlled access<\/strong> to private fields.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Protected Members with Derived Class<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When a data member is declared as <code>protected<\/code>, it is not accessible from outside the class, but it can be accessed inside the class itself and inside its derived classes. This makes <code>protected<\/code> useful when you want child classes to reuse or extend internal details without exposing them to everyone.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example with Student and GraduateStudent<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Student {\nprotected:\n    string address; \/\/ protected member\n\npublic:\n    void setAddress(string a) {\n        address = a;\n    }\n};\n\nclass GraduateStudent : public Student {\npublic:\n    void showAddress() {\n        cout &lt;&lt; \"Address: \" &lt;&lt; address &lt;&lt; endl;\n    }\n};\n\nint main() {\n    GraduateStudent gs;\n    gs.setAddress(\"University Campus\"); \/\/ setter from base class\n    gs.showAddress(); \/\/ derived class accesses protected member\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>address<\/code> is <strong>protected<\/strong>, so it is not accessible directly from <code>main()<\/code>.<\/li>\n\n\n\n<li>The derived class <code>GraduateStudent<\/code> can access <code>address<\/code> and use it inside its own function <code>showAddress<\/code>.<\/li>\n\n\n\n<li>This shows how <code>protected<\/code> allows inheritance-based access while keeping the member hidden from external code.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-canva wp-block-embed-canva\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Access Modifers in OOP (Java)\" src=\"https:\/\/www.canva.com\/design\/DAGWbH9_S2k\/3NgfwqGLU-kdmhHaIE_6IQ\/view?embed&amp;meta\" height=\"360\" width=\"640\" style=\"border: none; border-radius: 8px; width: 640px; height: 360px;\" allowfullscreen=\"allowfullscreen\" allow=\"fullscreen\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Encapsulation in object-oriented programming is a core principle. It helps keep data safe and programs modular. Imagine a School Management App: students can view their class (through a getter), but only the administration system can update it (using a setter). This story highlights how encapsulation mirrors real-world roles. Encapsulation ensures data safety and modular design. Imagine using a car or a smartphone: you interact with simple controls on the surface, while the complicated wiring and mechanisms are hidden inside. This&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":7130,"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":[637],"tags":[704,716,603,620],"class_list":["post-7126","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oop-with-c","tag-c","tag-enscapsulation","tag-oop","tag-protected"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Encapsulation in object-oriented programming is a core principle. It helps keep data safe and programs modular. Imagine a School Management App: students can view their class (through a getter), but only the administration system can update it (using a setter). This story highlights how encapsulation mirrors real-world roles. Encapsulation ensures data safety and modular design.\" \/>\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=\"c++,enscapsulation,oop,protected,oop with c++\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/\" \/>\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=\"Encapsulation in C++: A Beginner Guide - Afzal Badshah, PhD\" \/>\n\t\t<meta property=\"og:description\" content=\"Encapsulation in object-oriented programming is a core principle. It helps keep data safe and programs modular. Imagine a School Management App: students can view their class (through a getter), but only the administration system can update it (using a setter). This story highlights how encapsulation mirrors real-world roles. Encapsulation ensures data safety and modular design.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/1.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/1.jpg\" \/>\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-22T04:00:00+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-03-17T14:02:55+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=\"Encapsulation in C++: A Beginner Guide - Afzal Badshah, PhD\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Encapsulation in object-oriented programming is a core principle. It helps keep data safe and programs modular. Imagine a School Management App: students can view their class (through a getter), but only the administration system can update it (using a setter). This story highlights how encapsulation mirrors real-world roles. Encapsulation ensures data safety and modular design.\" \/>\n\t\t<meta name=\"twitter:creator\" content=\"@DrAFZALBADSHAH\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/1.jpg\" \/>\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=\"4 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\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/#blogposting\",\"name\":\"Encapsulation in C++: A Beginner Guide - Afzal Badshah, PhD\",\"headline\":\"Encapsulation in C++: A Beginner Guide\",\"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\\\/2025\\\/09\\\/1.jpg?fit=1920%2C1080&ssl=1\",\"width\":1920,\"height\":1080},\"datePublished\":\"2025-01-22T09:00:00+05:00\",\"dateModified\":\"2026-03-17T19:02:55+05:00\",\"inLanguage\":\"en-GB\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/#webpage\"},\"articleSection\":\"OOP with C++, c++, enscapsulation, OOP, protected\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/#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\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/#listItem\",\"name\":\"Encapsulation in C++: A Beginner Guide\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"name\":\"Courses\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/#listItem\",\"position\":4,\"name\":\"Encapsulation in C++: A Beginner Guide\",\"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\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/#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\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/#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\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/#webpage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/\",\"name\":\"Encapsulation in C++: A Beginner Guide - Afzal Badshah, PhD\",\"description\":\"Encapsulation in object-oriented programming is a core principle. It helps keep data safe and programs modular. Imagine a School Management App: students can view their class (through a getter), but only the administration system can update it (using a setter). This story highlights how encapsulation mirrors real-world roles. Encapsulation ensures data safety and modular design.\",\"inLanguage\":\"en-GB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/#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\\\/2025\\\/09\\\/1.jpg?fit=1920%2C1080&ssl=1\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/#mainImage\",\"width\":1920,\"height\":1080},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/encapsulation-in-c-a-beginner-guide\\\/#mainImage\"},\"datePublished\":\"2025-01-22T09:00:00+05:00\",\"dateModified\":\"2026-03-17T19:02:55+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":"Encapsulation in C++: A Beginner Guide - Afzal Badshah, PhD","description":"Encapsulation in object-oriented programming is a core principle. It helps keep data safe and programs modular. Imagine a School Management App: students can view their class (through a getter), but only the administration system can update it (using a setter). This story highlights how encapsulation mirrors real-world roles. Encapsulation ensures data safety and modular design.","canonical_url":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/","robots":"max-image-preview:large","keywords":"c++,enscapsulation,oop,protected,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\/22\/encapsulation-in-c-a-beginner-guide\/#blogposting","name":"Encapsulation in C++: A Beginner Guide - Afzal Badshah, PhD","headline":"Encapsulation in C++: A Beginner Guide","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\/2025\/09\/1.jpg?fit=1920%2C1080&ssl=1","width":1920,"height":1080},"datePublished":"2025-01-22T09:00:00+05:00","dateModified":"2026-03-17T19:02:55+05:00","inLanguage":"en-GB","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/#webpage"},"isPartOf":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/#webpage"},"articleSection":"OOP with C++, c++, enscapsulation, OOP, protected"},{"@type":"BreadcrumbList","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/#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\/22\/encapsulation-in-c-a-beginner-guide\/#listItem","name":"Encapsulation in C++: A Beginner Guide"},"previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","name":"Courses"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/#listItem","position":4,"name":"Encapsulation in C++: A Beginner Guide","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\/22\/encapsulation-in-c-a-beginner-guide\/#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\/22\/encapsulation-in-c-a-beginner-guide\/#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\/22\/encapsulation-in-c-a-beginner-guide\/#webpage","url":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/","name":"Encapsulation in C++: A Beginner Guide - Afzal Badshah, PhD","description":"Encapsulation in object-oriented programming is a core principle. It helps keep data safe and programs modular. Imagine a School Management App: students can view their class (through a getter), but only the administration system can update it (using a setter). This story highlights how encapsulation mirrors real-world roles. Encapsulation ensures data safety and modular design.","inLanguage":"en-GB","isPartOf":{"@id":"https:\/\/afzalbadshah.com\/#website"},"breadcrumb":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/#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\/2025\/09\/1.jpg?fit=1920%2C1080&ssl=1","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/#mainImage","width":1920,"height":1080},"primaryImageOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/#mainImage"},"datePublished":"2025-01-22T09:00:00+05:00","dateModified":"2026-03-17T19:02:55+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":"Encapsulation in C++: A Beginner Guide - Afzal Badshah, PhD","og:description":"Encapsulation in object-oriented programming is a core principle. It helps keep data safe and programs modular. Imagine a School Management App: students can view their class (through a getter), but only the administration system can update it (using a setter). This story highlights how encapsulation mirrors real-world roles. Encapsulation ensures data safety and modular design.","og:url":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/","og:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/1.jpg","og:image:secure_url":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/1.jpg","og:image:width":1920,"og:image:height":1080,"article:published_time":"2025-01-22T04:00:00+00:00","article:modified_time":"2026-03-17T14:02:55+00:00","article:publisher":"https:\/\/web.facebook.com\/abmanduri\/","twitter:card":"summary_large_image","twitter:site":"@DrAFZALBADSHAH","twitter:title":"Encapsulation in C++: A Beginner Guide - Afzal Badshah, PhD","twitter:description":"Encapsulation in object-oriented programming is a core principle. It helps keep data safe and programs modular. Imagine a School Management App: students can view their class (through a getter), but only the administration system can update it (using a setter). This story highlights how encapsulation mirrors real-world roles. Encapsulation ensures data safety and modular design.","twitter:creator":"@DrAFZALBADSHAH","twitter:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/1.jpg","twitter:label1":"Written by","twitter:data1":"Afzal Badshah, PhD","twitter:label2":"Est. reading time","twitter:data2":"4 minutes"},"aioseo_meta_data":{"post_id":"7126","title":null,"description":null,"keywords":null,"keyphrases":{"focus":{"keyphrase":"","score":0,"analysis":{"keyphraseInTitle":{"score":0,"maxScore":9,"error":1}}},"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":{"faqs":[],"keyPoints":[],"titles":[],"descriptions":[],"socialPosts":{"email":[],"linkedin":[],"twitter":[],"facebook":[],"instagram":[]}},"created":"2025-09-11 08:26:50","updated":"2026-03-17 14:07:54","focus_keyword":null,"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\tEncapsulation in C++: A Beginner Guide\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":"Encapsulation in C++: A Beginner Guide","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/encapsulation-in-c-a-beginner-guide\/"}],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/1.jpg?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-1QW","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/7126","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=7126"}],"version-history":[{"count":2,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/7126\/revisions"}],"predecessor-version":[{"id":42837,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/7126\/revisions\/42837"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/7130"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=7126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=7126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=7126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}