{"id":477,"date":"2023-02-13T02:12:06","date_gmt":"2023-02-13T02:12:06","guid":{"rendered":"https:\/\/aipsacademy.com\/blogs\/?p=477"},"modified":"2023-02-13T02:12:08","modified_gmt":"2023-02-13T02:12:08","slug":"constructor-and-destructor-in-c-with-example","status":"publish","type":"post","link":"https:\/\/aipsacademy.com\/blogs\/2023\/02\/13\/constructor-and-destructor-in-c-with-example\/","title":{"rendered":"Constructor and Destructor in C++ with Example"},"content":{"rendered":"\n<p><strong>In this topic we will learn best concept of Constructor and Destructor in C++ with example.<\/strong><\/p>\n\n\n\n<p><strong>Constructor<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>It is a special member function which is used to initialize data members of a class.<\/strong><\/li><li><strong>Constructor is different from all other member functions because it\u2019s name matches with class name and automatically called by compiler when object is created.<\/strong><\/li><li><strong>It is similar to other member functions i.e it can be defined inside or outside of the class and can also be passed parameters.<\/strong><\/li><\/ul>\n\n\n\n<p><strong><em>Characteristics of Constructor<\/em><\/strong><strong><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Constructor has the same name as its class name.<\/strong><\/li><li><strong>Constructor should be declared in public section.<\/strong><\/li><li><strong>Constructor does not return any type not even void.<\/strong><\/li><li><strong>Constructor is used to initialize the data member of a class.<\/strong><\/li><li><strong>Constructor is automatically called when object is created.<\/strong><\/li><li><strong>Constructor can be overloaded i.e multiple constructors can be used in a class.<\/strong><\/li><li><strong>It allows passing&nbsp;<\/strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Parameter\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>parameters<\/strong><\/a><strong>.<\/strong><\/li><li><strong>It follows all the access rules which followed by other member functions.<\/strong><\/li><li><strong>It can\u2019t be virtual.<\/strong><\/li><li><strong>if a constructor is not defined in the class then default constructor is automatically generated by the compiler.<\/strong><\/li><\/ul>\n\n\n\n<p><strong>Types of Constructor<\/strong><\/p>\n\n\n\n<p><strong>There are three types of constructors in c++.<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li><strong>Default constructor<\/strong><\/li><li><strong>Parameterized constructor<\/strong><\/li><li><strong>Copy constructor<\/strong><\/li><\/ol>\n\n\n\n<p><strong>Default constructor<\/strong><\/p>\n\n\n\n<p><strong>A constructor without parameter or Argument is known as default constructor.<\/strong><\/p>\n\n\n\n<p><strong>class num<\/strong><\/p>\n\n\n\n<p><strong>{<\/strong><\/p>\n\n\n\n<p><strong>public:<\/strong><\/p>\n\n\n\n<p><strong>num()<\/strong><\/p>\n\n\n\n<p><strong>{<\/strong><\/p>\n\n\n\n<p><strong>\/\/ default constructor<\/strong><\/p>\n\n\n\n<p><strong>}<\/strong><\/p>\n\n\n\n<p><strong>};<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this topic we will learn best concept of Constructor and Destructor in C++ with example. Constructor It is a special member function which is used to initialize data members of a class. Constructor is different from all other member functions because it\u2019s name matches with class name and automatically called by compiler when object [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":478,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,3,9,8],"tags":[],"class_list":["post-477","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-12th-computer-science","category-bca","category-cbse-12th-computer-science","category-jac-12th-computer-science"],"_links":{"self":[{"href":"https:\/\/aipsacademy.com\/blogs\/wp-json\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aipsacademy.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aipsacademy.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aipsacademy.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aipsacademy.com\/blogs\/wp-json\/wp\/v2\/comments?post=477"}],"version-history":[{"count":1,"href":"https:\/\/aipsacademy.com\/blogs\/wp-json\/wp\/v2\/posts\/477\/revisions"}],"predecessor-version":[{"id":479,"href":"https:\/\/aipsacademy.com\/blogs\/wp-json\/wp\/v2\/posts\/477\/revisions\/479"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aipsacademy.com\/blogs\/wp-json\/wp\/v2\/media\/478"}],"wp:attachment":[{"href":"https:\/\/aipsacademy.com\/blogs\/wp-json\/wp\/v2\/media?parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aipsacademy.com\/blogs\/wp-json\/wp\/v2\/categories?post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aipsacademy.com\/blogs\/wp-json\/wp\/v2\/tags?post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}