{"id":124,"date":"2026-05-21T04:45:10","date_gmt":"2026-05-21T04:45:10","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=124"},"modified":"2026-05-22T11:13:25","modified_gmt":"2026-05-22T11:13:25","slug":"constructors-and-destructors","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=constructors-and-destructors","title":{"rendered":"Constructors and Destructors"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Constructors and destructors are special member functions in C++ classes. They are automatically called when objects are created and destroyed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Constructor?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A constructor is a special function that automatically runs when an object of a class is created. It is mainly used to initialize object data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Features of Constructor<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Same name as the class<\/li>\n\n\n\n<li>No return type<\/li>\n\n\n\n<li>Called automatically<\/li>\n\n\n\n<li>Used for initialization<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Constructor<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class ClassName {<br><br>public:<br><br>    ClassName() {<br><br>        \/\/ constructor code<br>    }<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Constructor<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>class Student {<br><br>public:<br><br>    Student() {<br><br>        cout &lt;&lt; \"Constructor called\" &lt;&lt; endl;<br>    }<br>};<br><br>int main() {<br><br>    Student s1;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Constructor called<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Constructors<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Default Constructor<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A constructor without parameters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Demo {<br><br>public:<br><br>    Demo() {<br><br>        cout &lt;&lt; \"Default Constructor\";<br>    }<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2. Parameterized Constructor<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A constructor that accepts parameters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>class Student {<br><br>public:<br><br>    string name;<br><br>    Student(string n) {<br><br>        name = n;<br>    }<br><br>    void display() {<br><br>        cout &lt;&lt; name;<br>    }<br>};<br><br>int main() {<br><br>    Student s1(\"Ali\");<br><br>    s1.display();<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Ali<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Destructor?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A destructor is a special function that automatically runs when an object is destroyed. It is mainly used to free resources and clean up memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Features of Destructor<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Same name as class with <code>~<\/code><\/li>\n\n\n\n<li>No return type<\/li>\n\n\n\n<li>No parameters<\/li>\n\n\n\n<li>Called automatically when object ends<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Destructor<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>class ClassName {<br><br>public:<br><br>    ~ClassName() {<br><br>        \/\/ destructor code<br>    }<br>};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Destructor<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>class Student {<br><br>public:<br><br>    Student() {<br><br>        cout &lt;&lt; \"Constructor called\" &lt;&lt; endl;<br>    }<br><br>    ~Student() {<br><br>        cout &lt;&lt; \"Destructor called\" &lt;&lt; endl;<br>    }<br>};<br><br>int main() {<br><br>    Student s1;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Constructor called<br>Destructor called<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Constructors and Destructors Work<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Constructor runs when object is created<\/li>\n\n\n\n<li>Object performs tasks<\/li>\n\n\n\n<li>Destructor runs automatically when object is destroyed<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Constructor vs Destructor<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Constructor<\/th><th>Destructor<\/th><\/tr><\/thead><tbody><tr><td>Initializes object<\/td><td>Cleans resources<\/td><\/tr><tr><td>Called at object creation<\/td><td>Called at object destruction<\/td><\/tr><tr><td>Can have parameters<\/td><td>Cannot have parameters<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of entering and leaving a classroom:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Constructor = Opening classroom and preparing environment<\/li>\n\n\n\n<li>Destructor = Closing classroom and cleaning everything<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Constructors<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Automatic initialization<\/li>\n\n\n\n<li>Cleaner code<\/li>\n\n\n\n<li>Reduces errors<\/li>\n\n\n\n<li>Improves object setup<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Destructors<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Automatic cleanup<\/li>\n\n\n\n<li>Prevents memory leaks<\/li>\n\n\n\n<li>Manages resources efficiently<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Constructors and Destructors are Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">They are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Automate object management<\/li>\n\n\n\n<li>Improve memory handling<\/li>\n\n\n\n<li>Support efficient programming<\/li>\n\n\n\n<li>Increase program reliability<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Applications<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Constructors and destructors are widely used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Memory management<\/li>\n\n\n\n<li>File handling<\/li>\n\n\n\n<li>Database connections<\/li>\n\n\n\n<li>Resource allocation<\/li>\n\n\n\n<li>Object-oriented applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Constructors and destructors in C++ are special functions that automatically manage object creation and destruction. Constructors initialize objects, while destructors clean resources. Together, they improve memory management, code reliability, and program efficiency.<audio autoplay=\"\"><\/audio><\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced C++ > Object-Oriented Programming > Constructors and Destructors<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":38,"template":"","class_list":["post-124","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Constructors and Destructors - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ constructors and destructors with examples for object initialization, cleanup, memory management, and OOP programming concepts.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Constructors and Destructors - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ constructors and destructors with examples for object initialization, cleanup, memory management, and OOP programming concepts.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T11:13:25+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=constructors-and-destructors\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Constructors and Destructors - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T04:45:10+00:00\",\"dateModified\":\"2026-05-22T11:13:25+00:00\",\"description\":\"Learn C++ constructors and destructors with examples for object initialization, cleanup, memory management, and OOP programming concepts.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced C++ > Object-Oriented Programming > Constructors and Destructors\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Constructors and Destructors - Learn C++Language with GiGz.PK","description":"Learn C++ constructors and destructors with examples for object initialization, cleanup, memory management, and OOP programming concepts.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Constructors and Destructors - Learn C++Language with GiGz.PK","og_description":"Learn C++ constructors and destructors with examples for object initialization, cleanup, memory management, and OOP programming concepts.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T11:13:25+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=constructors-and-destructors","url":"https:\/\/gigz.pk\/","name":"Constructors and Destructors - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T04:45:10+00:00","dateModified":"2026-05-22T11:13:25+00:00","description":"Learn C++ constructors and destructors with examples for object initialization, cleanup, memory management, and OOP programming concepts.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Advanced C++ > Object-Oriented Programming > Constructors and Destructors"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}