{"id":224,"date":"2020-08-31T17:46:49","date_gmt":"2020-08-31T17:46:49","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=224"},"modified":"2020-08-31T17:46:49","modified_gmt":"2020-08-31T17:46:49","slug":"c-oop-constructors","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-oop-constructors\/","title":{"rendered":"C++ OOP &#8211; Constructors"},"content":{"rendered":"<p>In this lesson, we will cover Constructors and Copy Constructors in C++ under the following topics:<\/p>\n<ol>\n<li><a href=\"#t1\">What is a Constructor?<\/a><\/li>\n<li><a href=\"#t2\">Parameterized Constructors<\/a><\/li>\n<li><a href=\"#t3\">Copy Constructor<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. What is a Constructor?<\/strong><\/h4>\n<p>A constructor in C++ is special\u00a0 member function of a class that is automatically called whenever a new object is created.<\/p>\n<p>A constructor has the following properties:<\/p>\n<ul>\n<li>the same name as the class<\/li>\n<li>does not have a return type<\/li>\n<\/ul>\n<p><strong>Default constructor:<\/strong> C++ provides a default constructor. This is a constructor that accepts no parameters. For example<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">class<\/span>  <span style=\"color: #bb0066; font-weight: bold;\">Circle<\/span>{\r\n   <span style=\"color: #997700; font-weight: bold;\">public:<\/span>\r\n\r\n    <span style=\"color: #888888;\">\/\/ create a constructor<\/span>\r\n    Circle() {\r\n        <span style=\"color: #888888;\">\/\/ code<\/span>\r\n    }\r\n};\r\n<\/pre>\n<p>Here, the function Circle() is a constructor of the class Circle. The program below show the use of default constructor<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ Program to demonstrate default constructor<\/span>\r\n<span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #888888;\">\/\/ declare a class, circle<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span>  <span style=\"color: #bb0066; font-weight: bold;\">Circle<\/span> {\r\n\r\n  <span style=\"color: #997700; font-weight: bold;\">private:<\/span>\r\n       <span style=\"color: #333399; font-weight: bold;\">double<\/span> radius;\r\n\r\n   <span style=\"color: #997700; font-weight: bold;\">public:<\/span>\r\n    <span style=\"color: #888888;\">\/\/ create a constructor<\/span>\r\n    Circle() {\r\n\r\n        <span style=\"color: #888888;\">\/\/ initialize private variables<\/span>\r\n        radius <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">7.5<\/span>;\r\n\r\n        cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Creating a circle.\"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n        cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Radius = \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> radius <span style=\"color: #333333;\">&lt;&lt;<\/span> endl;\r\n    }\r\n};\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n\r\n    <span style=\"color: #888888;\">\/\/ create an object<\/span>\r\n    Circle circle;\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>The output is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Creating a circle.\r\nRadius = 7.5\r\n<\/pre>\n<p>However, not that if we fail to provide a default constructor, C++ compiler will provide one implicitly.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Parameterized Constructor<\/strong><\/h4>\n<p>A constructor that accepts parameters is called a parameterized constructor. This is the preferred approach to initializing data members.<\/p>\n<p>The program below illustrates parameterized constructor;<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #557799;\">#include &lt;math.h&gt;       <\/span><span style=\"color: #888888;\">\/* pow *\/<\/span>\r\n\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #888888;\">\/\/ declare a class, circle<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">class<\/span>  <span style=\"color: #bb0066; font-weight: bold;\">Cylinder<\/span> {\r\n\r\n  <span style=\"color: #997700; font-weight: bold;\">private:<\/span>\r\n       <span style=\"color: #333399; font-weight: bold;\">double<\/span> radius;\r\n       <span style=\"color: #333399; font-weight: bold;\">double<\/span> height;\r\n\r\n   <span style=\"color: #997700; font-weight: bold;\">public:<\/span>\r\n    <span style=\"color: #888888;\">\/\/ create a parameterized constructor<\/span>\r\n    Cylinder(<span style=\"color: #333399; font-weight: bold;\">double<\/span> r, <span style=\"color: #333399; font-weight: bold;\">double<\/span> h) {\r\n    \t<span style=\"color: #888888;\">\/\/ Initialise the member variables<\/span>\r\n    \tradius <span style=\"color: #333333;\">=<\/span> r;\r\n    \theight <span style=\"color: #333333;\">=<\/span> h;\r\n    }\r\n\r\n    <span style=\"color: #333399; font-weight: bold;\">double<\/span> calculateVolume(){\r\n    \t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">22<\/span><span style=\"color: #333333;\">\/<\/span><span style=\"color: #0000dd; font-weight: bold;\">7<\/span> <span style=\"color: #333333;\">*<\/span> pow(radius,<span style=\"color: #0000dd; font-weight: bold;\">2<\/span>) <span style=\"color: #333333;\">*<\/span> height;\r\n    }\r\n};\r\n\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n\r\n    <span style=\"color: #888888;\">\/\/ create a cylinder with parameters<\/span>\r\n    Cylinder cylinder1(<span style=\"color: #6600ee; font-weight: bold;\">10.2<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">5.3<\/span>);\r\n    Cylinder cylinder2(<span style=\"color: #6600ee; font-weight: bold;\">20.5<\/span>, <span style=\"color: #6600ee; font-weight: bold;\">2.5<\/span>);\r\n\r\n    cout <span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Volume 1: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> cylinder1.calculateVolume() <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n    cout <span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Volume 2: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> cylinder2.calculateVolume() <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>Output of the program<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Volume 1: 1654.24\r\nVolume 2: 3151.88\r\n<\/pre>\n<p>Here we created a parameterized constructor Cylinder() with two parameters: radius and height. The values of these parameters are use to initialized the member variables when the Cylinder is instantiated.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Copy Constructor<\/strong><\/h4>\n<p>This is a constructor that instantiates an object by copying data from an existing object of the same class.<\/p>\n<p>A copy constructor\u00a0 is used for the following:<\/p>\n<ul>\n<li>Initialize on object from another object<\/li>\n<li>copy an object to pass as an argument to a function<\/li>\n<li>copy an object to return from a function<\/li>\n<\/ul>\n<p>Similar to normal constructor, if we don&#8217;t define a copy constructor, then the C++ compiler will define one implicitly.<\/p>\n<p>A copy constructor for\u00a0 our cylinder object is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ copy constructor with a Cylinder object as parameter<\/span>\r\nCylinder(Cylinder <span style=\"color: #333333;\">&amp;<\/span>obj) {\r\n    <span style=\"color: #888888;\">\/\/ initialize private variables<\/span>\r\n    radius <span style=\"color: #333333;\">=<\/span> obj.radius;\r\n    height <span style=\"color: #333333;\">=<\/span> obj.height;\r\n}\r\n<\/pre>\n<p>Notice in the code that the parameter to the copy constructor has the address of an object of the Cylinder class.<\/p>\n<p><strong>Final note<\/strong>: Although a constructor is used to initialize an object, they can also be used to perform certain operations when an object is created.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we will cover Constructors and Copy Constructors in C++ under the following topics: What is a Constructor? Parameterized Constructors Copy Constructor &nbsp; &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,35],"tags":[37],"class_list":["post-224","post","type-post","status-publish","format-standard","hentry","category-c-tutorials","category-oop","tag-constructors"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=224"}],"version-history":[{"count":1,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/224\/revisions"}],"predecessor-version":[{"id":225,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/224\/revisions\/225"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}