{"id":8378,"date":"2025-09-29T07:33:49","date_gmt":"2025-09-29T02:33:49","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=8378"},"modified":"2025-09-29T07:33:51","modified_gmt":"2025-09-29T02:33:51","slug":"constants-in-oop-c","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-c\/","title":{"rendered":"Constants in OOP &#8211; C++"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In our daily life, there are some values that <strong>never change<\/strong>. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>number of days in a week<\/strong> is always 7.<\/li>\n\n\n\n<li>The <strong>speed of light<\/strong> in a vacuum is constant.<\/li>\n\n\n\n<li>A <strong>student\u2019s roll number<\/strong> assigned once in a class does not change.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These values remain fixed regardless of circumstances. Similarly, in C++, if we want to create a variable whose value should <strong>not be altered during the program execution<\/strong>, we declare it as <strong>constant<\/strong> using the <code>const<\/code> keyword. <a href=\"https:\/\/afzalbadshah.com\/index.php\/object-oriented-programming-in-c-a-comprehensive-tutorial\/\" target=\"_blank\" rel=\"noopener\" title=\"\">The detailed tutorial can be visited here. <\/a><\/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\">A constant in C++ is a variable whose value cannot be modified after initialization.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Why Do We Need Constants?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Constants are essential because they represent <strong>fixed values<\/strong> and prevent accidental modifications.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Data Safety:<\/strong> Ensures values do not change mistakenly during program execution.<\/li>\n\n\n\n<li><strong>Readability:<\/strong> Makes the code clearer by indicating that a value is fixed.<\/li>\n\n\n\n<li><strong>Maintainability:<\/strong> If a constant changes in the future (e.g., tax rate), we only update it in one place.<\/li>\n\n\n\n<li><strong>Reliability:<\/strong> Prevents bugs that may occur due to unintended modification of important values.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Declaring Constants<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In C++, constants can be declared using the <code>const<\/code> keyword.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const int daysInWeek = 7;\nconst float PI = 3.14159;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once declared, their values cannot be changed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example 1: Basic Constant Usage<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n    const float PI = 3.14159;   \/\/ Constant declaration\n    float radius = 5;\n\n    float area = PI * radius * radius;\n    cout &lt;&lt; \"Area of circle = \" &lt;&lt; area &lt;&lt; endl;\n\n    \/\/ PI = 3.14;   \/\/ \u274c Error: cannot modify a const variable\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Area of circle = 78.5397\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>PI<\/code> remains fixed throughout the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Constants in Classes<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We can also use constants inside classes. They are useful for defining values that are common for all objects but should not change, like maximum marks or fixed tax rates.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Student {\npublic:\n    const int rollNumber;   \/\/ constant data member\n\n    Student(int r)  {\n      rollNumber = r;\/\/ Initialization using constructor\n    }\n\n    void show() {\n        cout &lt;&lt; \"Roll Number: \" &lt;&lt; rollNumber &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Student s1(101);\n    Student s2(102);\n\n    s1.show();\n    s2.show();\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Roll Number: 101\nRoll Number: 102\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>rollNumber<\/code> is constant\u2014once assigned through the constructor, it cannot change.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Const Member Functions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A member function can also be declared as <code>const<\/code>.<br>This means it cannot modify any member variables of the class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Demo {\n    int value;\npublic:\n    Demo(int v) {\n      value = v;\n    }\n\n    int getValue() const {   \/\/ const function\n        return value;\n    }\n};\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you try to change <code>value<\/code> inside <code>getValue()<\/code>, the compiler will give an error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pointers and Constants<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Constants can also be used with pointers in three different ways. To make it simple, think of a <strong>pointer<\/strong> as an arrow that shows the address of a variable, while <strong>const<\/strong> controls whether the arrow itself can move or whether the value it points to can change.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Pointer to Constant Data<\/strong><br>Here the data is fixed, but the pointer can point somewhere else.<br>Daily life example: You have a <em>book<\/em> that you can only read but not edit. However, you can put down this book and pick up another. <code>const int x = 10; const int* ptr = &amp;x; \/\/ data is constant, pointer can change<\/code><\/li>\n\n\n\n<li><strong>Constant Pointer<\/strong><br>Here the pointer cannot move to another address, but the data at that location can still change.<br>Daily life example: Imagine you are tied to a single chair. You cannot move to another chair, but you can still adjust the cushion on your chair. <code>int y = 20; int* const ptr2 = &amp;y; \/\/ pointer is constant, data can change<\/code><\/li>\n\n\n\n<li><strong>Constant Pointer to Constant Data<\/strong><br>Here both the pointer and the data are fixed.<br>Daily life example: You are tied to a chair, and the cushion on that chair is also locked\u2014you can neither move to another chair nor adjust the cushion. <code>const int z = 30; const int* const ptr3 = &amp;z; \/\/ both pointer and data are constant<\/code><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Daily Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of <strong>days in a week<\/strong>.<br>No matter where you live, you always have 7 days in a week.<br>This is just like a constant variable: it is <strong>defined once and never changes<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Or consider a <strong>roll number<\/strong> in a classroom\u2014assigned once and fixed for the student.<br>That\u2019s how constants work in C++.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Comparison: Normal vs Constant vs Static Members<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Aspect<\/th><th>Normal Variable<\/th><th>Constant Variable<\/th><th>Static Variable<\/th><\/tr><\/thead><tbody><tr><td>Value<\/td><td>Can change anytime<\/td><td>Fixed, cannot be modified<\/td><td>Shared among all objects<\/td><\/tr><tr><td>Initialization<\/td><td>Can be later<\/td><td>Must be at declaration\/constructor<\/td><td>Declared once, defined outside class<\/td><\/tr><tr><td>Usage<\/td><td>For dynamic values<\/td><td>For fixed values<\/td><td>For shared\/persistent values<\/td><\/tr><tr><td>Safety<\/td><td>Risk of accidental changes<\/td><td>Protected against changes<\/td><td>Prevents duplication, ensures persistence<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Constants ensure stability and prevent accidental modification.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Exercises<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Write a program that uses a constant <code>PI<\/code> to calculate the area of a circle.<\/li>\n\n\n\n<li>Create a class with a constant roll number that is initialized via constructor.<\/li>\n\n\n\n<li>Write a program to demonstrate a constant pointer to constant data.<\/li>\n\n\n\n<li>Make a constant member function that returns the value of a private variable.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our daily life, there are some values that never change. For example: These values remain fixed regardless of circumstances. Similarly, in C++, if we want to create a variable whose value should not be altered during the program execution, we declare it as constant using the const keyword. The detailed tutorial can be visited here. A constant in C++ is a variable whose value cannot be modified after initialization. Why Do We Need Constants? Constants are essential because they&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-c\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":8384,"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,721,720,603],"class_list":["post-8378","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oop-with-c","tag-c","tag-constant","tag-constant-in-c","tag-oop"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"In our daily life, there are some values that never change. For example: The number of days in a week is always 7. The speed of light in a vacuum is constant. A student\u2019s roll number assigned once in a class does not change. These values remain fixed regardless of circumstances. Similarly, in C++, if\" \/>\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++,constant,constant in c++,oop,oop with c++\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-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=\"Constants in OOP \u2013 C++ - Afzal Badshah, PhD\" \/>\n\t\t<meta property=\"og:description\" content=\"In our daily life, there are some values that never change. For example: The number of days in a week is always 7. The speed of light in a vacuum is constant. A student\u2019s roll number assigned once in a class does not change. These values remain fixed regardless of circumstances. Similarly, in C++, if\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-c\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/Access-Modifers-in-OOP-Java.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/Access-Modifers-in-OOP-Java.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-09-29T02:33:49+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2025-09-29T02:33:51+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=\"Constants in OOP \u2013 C++ - Afzal Badshah, PhD\" \/>\n\t\t<meta name=\"twitter:description\" content=\"In our daily life, there are some values that never change. For example: The number of days in a week is always 7. The speed of light in a vacuum is constant. A student\u2019s roll number assigned once in a class does not change. These values remain fixed regardless of circumstances. Similarly, in C++, if\" \/>\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\/Access-Modifers-in-OOP-Java.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\\\/09\\\/29\\\/constants-in-oop-c\\\/#blogposting\",\"name\":\"Constants in OOP \\u2013 C++ - Afzal Badshah, PhD\",\"headline\":\"Constants in OOP &#8211; 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\\\/2025\\\/09\\\/Access-Modifers-in-OOP-Java.jpg?fit=1920%2C1080&ssl=1\",\"width\":1920,\"height\":1080},\"datePublished\":\"2025-09-29T07:33:49+05:00\",\"dateModified\":\"2025-09-29T07:33:51+05:00\",\"inLanguage\":\"en-GB\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/09\\\/29\\\/constants-in-oop-c\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/09\\\/29\\\/constants-in-oop-c\\\/#webpage\"},\"articleSection\":\"OOP with C++, c++, constant, Constant in c++, OOP\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/09\\\/29\\\/constants-in-oop-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\\\/09\\\/29\\\/constants-in-oop-c\\\/#listItem\",\"name\":\"Constants in OOP &#8211; C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/category\\\/courses\\\/#listItem\",\"name\":\"Courses\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/09\\\/29\\\/constants-in-oop-c\\\/#listItem\",\"position\":4,\"name\":\"Constants in OOP &#8211; 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\\\/09\\\/29\\\/constants-in-oop-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\\\/09\\\/29\\\/constants-in-oop-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\\\/09\\\/29\\\/constants-in-oop-c\\\/#webpage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/09\\\/29\\\/constants-in-oop-c\\\/\",\"name\":\"Constants in OOP \\u2013 C++ - Afzal Badshah, PhD\",\"description\":\"In our daily life, there are some values that never change. For example: The number of days in a week is always 7. The speed of light in a vacuum is constant. A student\\u2019s roll number assigned once in a class does not change. These values remain fixed regardless of circumstances. Similarly, in C++, if\",\"inLanguage\":\"en-GB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/09\\\/29\\\/constants-in-oop-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\\\/2025\\\/09\\\/Access-Modifers-in-OOP-Java.jpg?fit=1920%2C1080&ssl=1\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/09\\\/29\\\/constants-in-oop-c\\\/#mainImage\",\"width\":1920,\"height\":1080},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/09\\\/29\\\/constants-in-oop-c\\\/#mainImage\"},\"datePublished\":\"2025-09-29T07:33:49+05:00\",\"dateModified\":\"2025-09-29T07:33:51+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":"Constants in OOP \u2013 C++ - Afzal Badshah, PhD","description":"In our daily life, there are some values that never change. For example: The number of days in a week is always 7. The speed of light in a vacuum is constant. A student\u2019s roll number assigned once in a class does not change. These values remain fixed regardless of circumstances. Similarly, in C++, if","canonical_url":"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-c\/","robots":"max-image-preview:large","keywords":"c++,constant,constant in c++,oop,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\/09\/29\/constants-in-oop-c\/#blogposting","name":"Constants in OOP \u2013 C++ - Afzal Badshah, PhD","headline":"Constants in OOP &#8211; 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\/2025\/09\/Access-Modifers-in-OOP-Java.jpg?fit=1920%2C1080&ssl=1","width":1920,"height":1080},"datePublished":"2025-09-29T07:33:49+05:00","dateModified":"2025-09-29T07:33:51+05:00","inLanguage":"en-GB","mainEntityOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-c\/#webpage"},"isPartOf":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-c\/#webpage"},"articleSection":"OOP with C++, c++, constant, Constant in c++, OOP"},{"@type":"BreadcrumbList","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-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\/09\/29\/constants-in-oop-c\/#listItem","name":"Constants in OOP &#8211; C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/#listItem","name":"Courses"}},{"@type":"ListItem","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-c\/#listItem","position":4,"name":"Constants in OOP &#8211; 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\/09\/29\/constants-in-oop-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\/09\/29\/constants-in-oop-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\/09\/29\/constants-in-oop-c\/#webpage","url":"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-c\/","name":"Constants in OOP \u2013 C++ - Afzal Badshah, PhD","description":"In our daily life, there are some values that never change. For example: The number of days in a week is always 7. The speed of light in a vacuum is constant. A student\u2019s roll number assigned once in a class does not change. These values remain fixed regardless of circumstances. Similarly, in C++, if","inLanguage":"en-GB","isPartOf":{"@id":"https:\/\/afzalbadshah.com\/#website"},"breadcrumb":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-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\/2025\/09\/Access-Modifers-in-OOP-Java.jpg?fit=1920%2C1080&ssl=1","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-c\/#mainImage","width":1920,"height":1080},"primaryImageOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-c\/#mainImage"},"datePublished":"2025-09-29T07:33:49+05:00","dateModified":"2025-09-29T07:33:51+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":"Constants in OOP \u2013 C++ - Afzal Badshah, PhD","og:description":"In our daily life, there are some values that never change. For example: The number of days in a week is always 7. The speed of light in a vacuum is constant. A student\u2019s roll number assigned once in a class does not change. These values remain fixed regardless of circumstances. Similarly, in C++, if","og:url":"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-c\/","og:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/Access-Modifers-in-OOP-Java.jpg","og:image:secure_url":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/Access-Modifers-in-OOP-Java.jpg","og:image:width":1920,"og:image:height":1080,"article:published_time":"2025-09-29T02:33:49+00:00","article:modified_time":"2025-09-29T02:33:51+00:00","article:publisher":"https:\/\/web.facebook.com\/abmanduri\/","twitter:card":"summary_large_image","twitter:site":"@DrAFZALBADSHAH","twitter:title":"Constants in OOP \u2013 C++ - Afzal Badshah, PhD","twitter:description":"In our daily life, there are some values that never change. For example: The number of days in a week is always 7. The speed of light in a vacuum is constant. A student\u2019s roll number assigned once in a class does not change. These values remain fixed regardless of circumstances. Similarly, in C++, if","twitter:creator":"@DrAFZALBADSHAH","twitter:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/Access-Modifers-in-OOP-Java.jpg","twitter:label1":"Written by","twitter:data1":"Afzal Badshah, PhD","twitter:label2":"Est. reading time","twitter:data2":"4 minutes"},"aioseo_meta_data":{"post_id":"8378","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-29 02:02:37","updated":"2025-09-29 02:40:14","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\tConstants in OOP \u2013 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":"Constants in OOP &#8211; C++","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/09\/29\/constants-in-oop-c\/"}],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2025\/09\/Access-Modifers-in-OOP-Java.jpg?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-2b8","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/8378","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=8378"}],"version-history":[{"count":5,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/8378\/revisions"}],"predecessor-version":[{"id":8400,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/8378\/revisions\/8400"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/8384"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=8378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=8378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=8378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}