{"id":42205,"date":"2025-01-22T09:05:00","date_gmt":"2025-01-22T04:05:00","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=42205"},"modified":"2026-02-17T17:32:37","modified_gmt":"2026-02-17T12:32:37","slug":"constructors-in-c-object-oriented-programming","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/","title":{"rendered":"Constructors in C++ (Object-Oriented Programming)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When we create an object in C++, we expect it to start in a valid and usable state. In real life, a student record is not useful unless the university has stored the student\u2019s name and roll number. Similarly, a car in a showroom is not meaningful unless it has a model and an engine number. In programming, the mechanism that ensures this proper initial state of an object is called a constructor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A constructor guarantees that as soon as an object is created, it is properly initialized and ready to be used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> A special member function of a class that is automatically called\nwhen an object is created. Its main purpose is to initialize the object.\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A constructor has the following characteristics:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 It has the same name as the class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 It has no return type (not even void).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 It executes automatically when an object is created.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u2022 It is mainly used to initialize data members of the class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Need for Constructors<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In C++, when memory is allocated for an object, the data members may contain garbage values if they are not explicitly initialized. Using such uninitialized data can cause unpredictable behavior in programs.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consider a student management system. If a Student object is created without initializing the name and roll number, the system may display incorrect or meaningless information. Therefore, initialization at the time of object creation is essential.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We can summarize this idea as:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Object Creation = Memory Allocation + Initialization<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Constructors ensure that initialization happens immediately after memory allocation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Default Constructor<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A default constructor is a constructor that does not take any parameters. It assigns default or safe values to the data members.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A constructor that takes no parameters and initializes an object with default values.\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Student Class with Default Constructor<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Student {\npublic:\n    string name;\n    int rollNo;\n\n    Student() {\n        name = \"Unknown\";\n        rollNo = 0;\n    }\n\n    void display() {\n        cout &lt;&lt; \"Name: \" &lt;&lt; name &lt;&lt; endl;\n        cout &lt;&lt; \"Roll No: \" &lt;&lt; rollNo &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Student s1;\n    s1.display();\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When the statement <code>Student s1;<\/code> is executed, the constructor runs automatically and initializes the data members. As a result, the object is created in a consistent and predictable state.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parameterized Constructor<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In many situations, we want to initialize objects with specific values at the time of creation. For example, when registering a student, the university already knows the student\u2019s name and roll number.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A constructor that accepts parameters to initialize an object with specific values.\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Student Class with Parameterized Constructor<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Student {\npublic:\n    string name;\n    int rollNo;\n\n    Student(string n, int r) {\n        name = n;\n        rollNo = r;\n    }\n\n    void display() {\n        cout &lt;&lt; \"Name: \" &lt;&lt; name &lt;&lt; \", Roll No: \" &lt;&lt; rollNo &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Student s1(\"Ali\", 101);\n    Student s2(\"Sara\", 102);\n\n    s1.display();\n    s2.display();\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, each object is initialized with meaningful data at the time of creation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Constructor Overloading<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A class may require multiple ways to create objects. For example, sometimes we may want to create a student with default values, and sometimes with complete information.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Defining multiple constructors in the same class\nwith different parameter lists.\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Overloaded Constructors<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student {\npublic:\n    string name;\n    int rollNo;\n\n    Student() {\n        name = \"Unknown\";\n        rollNo = 0;\n    }\n\n    Student(string n, int r) {\n        name = n;\n        rollNo = r;\n    }\n};\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now objects can be created in two different ways:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Student s1;                \/\/ Default constructor\nStudent s2(\"Hassan\", 205); \/\/ Parameterized constructor\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This improves flexibility and usability of the class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the <code>this<\/code> Keyword in Constructors<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes the parameter names are the same as the data member names. In such cases, the <code>this<\/code> pointer is used to refer to the current object.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>A pointer that refers to the calling object of the class.\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class Student {\npublic:\n    string name;\n    int rollNo;\n\n    Student(string name, int rollNo) {\n        this-&gt;name = name;\n        this-&gt;rollNo = rollNo;\n    }\n};\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>this-&gt;name<\/code> refers to the data member of the class, while <code>name<\/code> refers to the constructor parameter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Constructor and Encapsulation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In well-designed programs, data members are usually kept private. Constructors help initialize private data properly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Car Class with Private Data<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Car {\nprivate:\n    string model;\n    string engineNo;\n\npublic:\n    Car(string m, string e) {\n        model = m;\n        engineNo = e;\n    }\n\n    void display() {\n        cout &lt;&lt; \"Model: \" &lt;&lt; model &lt;&lt; endl;\n        cout &lt;&lt; \"Engine No: \" &lt;&lt; engineNo &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Car c1(\"Toyota Corolla\", \"ENG12345\");\n    c1.display();\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this design, the constructor ensures that every Car object has valid model and engine number information at creation time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Constructors in Inheritance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When inheritance is used, the constructor of the base (parent) class is executed before the constructor of the derived (child) class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Order of Constructor Execution:\nBase Class Constructor \u2192 Derived Class Constructor\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Vehicle {\npublic:\n    Vehicle() {\n        cout &lt;&lt; \"Vehicle constructor called\" &lt;&lt; endl;\n    }\n};\n\nclass Car : public Vehicle {\npublic:\n    Car() {\n        cout &lt;&lt; \"Car constructor called\" &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Car c1;\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When the Car object is created, the Vehicle constructor executes first, followed by the Car constructor.<\/p>\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=\"Constructors in OOP\" src=\"https:\/\/www.canva.com\/design\/DAGWbOXB9EA\/rASWo-19JgJAiYaOnp_Z_A\/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\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we create an object in C++, we expect it to start in a valid and usable state. In real life, a student record is not useful unless the university has stored the student\u2019s name and roll number. Similarly, a car in a showroom is not meaningful unless it has a model and an engine number. In programming, the mechanism that ensures this proper initial state of an object is called a constructor. A constructor guarantees that as soon as&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":5087,"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,741,603],"class_list":["post-42205","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oop-with-c","tag-c","tag-constructors","tag-oop"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.1.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"When we create an object in C++, we expect it to start in a valid and usable state. In real life, a student record is not useful unless the university has stored the student\u2019s name and roll number. Similarly, a car in a showroom is not meaningful unless it has a model and an engine\" \/>\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++,constructors,oop,oop with c++\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.1.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=\"Constructors in C++ (Object-Oriented Programming) - Afzal Badshah, PhD\" \/>\n\t\t<meta property=\"og:description\" content=\"When we create an object in C++, we expect it to start in a valid and usable state. In real life, a student record is not useful unless the university has stored the student\u2019s name and roll number. Similarly, a car in a showroom is not meaningful unless it has a model and an engine\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/CONSTRUCTOR.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/CONSTRUCTOR.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-22T04:05:00+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2026-02-17T12:32:37+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=\"Constructors in C++ (Object-Oriented Programming) - Afzal Badshah, PhD\" \/>\n\t\t<meta name=\"twitter:description\" content=\"When we create an object in C++, we expect it to start in a valid and usable state. In real life, a student record is not useful unless the university has stored the student\u2019s name and roll number. Similarly, a car in a showroom is not meaningful unless it has a model and an engine\" \/>\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\/CONSTRUCTOR.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=\"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\\\/constructors-in-c-object-oriented-programming\\\/#blogposting\",\"name\":\"Constructors in C++ (Object-Oriented Programming) - Afzal Badshah, PhD\",\"headline\":\"Constructors in C++ (Object-Oriented Programming)\",\"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\\\/CONSTRUCTOR.png?fit=1920%2C1080&ssl=1\",\"width\":1920,\"height\":1080},\"datePublished\":\"2025-01-22T09:05:00+05:00\",\"dateModified\":\"2026-02-17T17:32:37+05:00\",\"inLanguage\":\"en-GB\",\"commentCount\":1,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/constructors-in-c-object-oriented-programming\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/constructors-in-c-object-oriented-programming\\\/#webpage\"},\"articleSection\":\"OOP with C++, c++, Constructors, OOP\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/constructors-in-c-object-oriented-programming\\\/#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\\\/constructors-in-c-object-oriented-programming\\\/#listItem\",\"name\":\"Constructors in C++ (Object-Oriented Programming)\"},\"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\\\/constructors-in-c-object-oriented-programming\\\/#listItem\",\"position\":4,\"name\":\"Constructors in C++ (Object-Oriented Programming)\",\"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\\\/constructors-in-c-object-oriented-programming\\\/#personImage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/wp-content\\\/litespeed\\\/avatar\\\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787755418\",\"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\\\/constructors-in-c-object-oriented-programming\\\/#authorImage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/wp-content\\\/litespeed\\\/avatar\\\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787755418\",\"width\":96,\"height\":96,\"caption\":\"Afzal Badshah, PhD\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/constructors-in-c-object-oriented-programming\\\/#webpage\",\"url\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/constructors-in-c-object-oriented-programming\\\/\",\"name\":\"Constructors in C++ (Object-Oriented Programming) - Afzal Badshah, PhD\",\"description\":\"When we create an object in C++, we expect it to start in a valid and usable state. In real life, a student record is not useful unless the university has stored the student\\u2019s name and roll number. Similarly, a car in a showroom is not meaningful unless it has a model and an engine\",\"inLanguage\":\"en-GB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/constructors-in-c-object-oriented-programming\\\/#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\\\/CONSTRUCTOR.png?fit=1920%2C1080&ssl=1\",\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/constructors-in-c-object-oriented-programming\\\/#mainImage\",\"width\":1920,\"height\":1080},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/afzalbadshah.com\\\/index.php\\\/2025\\\/01\\\/22\\\/constructors-in-c-object-oriented-programming\\\/#mainImage\"},\"datePublished\":\"2025-01-22T09:05:00+05:00\",\"dateModified\":\"2026-02-17T17:32:37+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":"Constructors in C++ (Object-Oriented Programming) - Afzal Badshah, PhD","description":"When we create an object in C++, we expect it to start in a valid and usable state. In real life, a student record is not useful unless the university has stored the student\u2019s name and roll number. Similarly, a car in a showroom is not meaningful unless it has a model and an engine","canonical_url":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/","robots":"max-image-preview:large","keywords":"c++,constructors,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\/01\/22\/constructors-in-c-object-oriented-programming\/#blogposting","name":"Constructors in C++ (Object-Oriented Programming) - Afzal Badshah, PhD","headline":"Constructors in C++ (Object-Oriented Programming)","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\/CONSTRUCTOR.png?fit=1920%2C1080&ssl=1","width":1920,"height":1080},"datePublished":"2025-01-22T09:05:00+05:00","dateModified":"2026-02-17T17:32:37+05:00","inLanguage":"en-GB","commentCount":1,"mainEntityOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/#webpage"},"isPartOf":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/#webpage"},"articleSection":"OOP with C++, c++, Constructors, OOP"},{"@type":"BreadcrumbList","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/#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\/constructors-in-c-object-oriented-programming\/#listItem","name":"Constructors in C++ (Object-Oriented Programming)"},"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\/constructors-in-c-object-oriented-programming\/#listItem","position":4,"name":"Constructors in C++ (Object-Oriented Programming)","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\/constructors-in-c-object-oriented-programming\/#personImage","url":"https:\/\/afzalbadshah.com\/wp-content\/litespeed\/avatar\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787755418","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\/constructors-in-c-object-oriented-programming\/#authorImage","url":"https:\/\/afzalbadshah.com\/wp-content\/litespeed\/avatar\/27d3d5e33aa81b3152e368c66871f22f.jpg?ver=1787755418","width":96,"height":96,"caption":"Afzal Badshah, PhD"}},{"@type":"WebPage","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/#webpage","url":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/","name":"Constructors in C++ (Object-Oriented Programming) - Afzal Badshah, PhD","description":"When we create an object in C++, we expect it to start in a valid and usable state. In real life, a student record is not useful unless the university has stored the student\u2019s name and roll number. Similarly, a car in a showroom is not meaningful unless it has a model and an engine","inLanguage":"en-GB","isPartOf":{"@id":"https:\/\/afzalbadshah.com\/#website"},"breadcrumb":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/#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\/CONSTRUCTOR.png?fit=1920%2C1080&ssl=1","@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/#mainImage","width":1920,"height":1080},"primaryImageOfPage":{"@id":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/#mainImage"},"datePublished":"2025-01-22T09:05:00+05:00","dateModified":"2026-02-17T17:32:37+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":"Constructors in C++ (Object-Oriented Programming) - Afzal Badshah, PhD","og:description":"When we create an object in C++, we expect it to start in a valid and usable state. In real life, a student record is not useful unless the university has stored the student\u2019s name and roll number. Similarly, a car in a showroom is not meaningful unless it has a model and an engine","og:url":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/","og:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/CONSTRUCTOR.png","og:image:secure_url":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/CONSTRUCTOR.png","og:image:width":1920,"og:image:height":1080,"article:published_time":"2025-01-22T04:05:00+00:00","article:modified_time":"2026-02-17T12:32:37+00:00","article:publisher":"https:\/\/web.facebook.com\/abmanduri\/","twitter:card":"summary_large_image","twitter:site":"@DrAFZALBADSHAH","twitter:title":"Constructors in C++ (Object-Oriented Programming) - Afzal Badshah, PhD","twitter:description":"When we create an object in C++, we expect it to start in a valid and usable state. In real life, a student record is not useful unless the university has stored the student\u2019s name and roll number. Similarly, a car in a showroom is not meaningful unless it has a model and an engine","twitter:creator":"@DrAFZALBADSHAH","twitter:image":"https:\/\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/CONSTRUCTOR.png","twitter:label1":"Written by","twitter:data1":"Afzal Badshah, PhD","twitter:label2":"Est. reading time","twitter:data2":"4 minutes"},"aioseo_meta_data":{"post_id":"42205","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":"2026-02-17 12:10:26","updated":"2026-02-17 12:35:00","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\tConstructors in C++ (Object-Oriented Programming)\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":"Constructors in C++ (Object-Oriented Programming)","link":"https:\/\/afzalbadshah.com\/index.php\/2025\/01\/22\/constructors-in-c-object-oriented-programming\/"}],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/11\/CONSTRUCTOR.png?fit=1920%2C1080&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-aYJ","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/42205","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=42205"}],"version-history":[{"count":1,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/42205\/revisions"}],"predecessor-version":[{"id":42206,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/42205\/revisions\/42206"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/5087"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=42205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=42205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=42205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}