{"id":221,"date":"2020-08-31T16:13:42","date_gmt":"2020-08-31T16:13:42","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=221"},"modified":"2020-08-31T16:13:42","modified_gmt":"2020-08-31T16:13:42","slug":"c-oop-basics","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-oop-basics\/","title":{"rendered":"C++ OOP &#8211; Basics"},"content":{"rendered":"<p>We are not stepping into\u00a0 the world of OOP. Somehow, every modern programming language supports some form of OOP features.<\/p>\n<p>Let&#8217;s first get through the basics of OOP under the following sub-topics:<\/p>\n<ol>\n<li><a href=\"#t1\">What is OOP?<\/a><\/li>\n<li><a href=\"#t2\">Creating Classes in C++<\/a><\/li>\n<li><a href=\"#t3\">Creating Objects from Class<\/a><\/li>\n<li><a href=\"#t4\">Accessing Data Members and Member Functions<\/a><\/li>\n<li><a href=\"#t5\">Benefits of Object Oriented Programming<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. What is OOP?<\/strong><\/h4>\n<p>OOP in C++, or in programming generally, stands for Object Oriented Programming. This is an approach to programming where programs are written based on <em><strong>classes<\/strong> <\/em>and <em><strong>objects<\/strong><\/em>. These are the two key aspect of OOP.<\/p>\n<p>An object is a representation a real object. Just look around you, or just think for a minute! Everything is basically an object: phone, keyboard, mouse, pen, computer, food, camera, banana, apple, bike, car, dog etc.<\/p>\n<p>So we can say an object is made up of <strong><em>attributes<\/em> <\/strong>and <em><strong>methods<\/strong><\/em>.<\/p>\n<p>A class is a blueprint of an object. Basically, we can create objects from classes. In other words, an object is an instance of a class.<\/p>\n<p>The figure below illustrates this:<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/Object-vs-Classes.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-222 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/Object-vs-Classes-300x60.jpg\" alt=\"Object vs Classes\" width=\"300\" height=\"60\" srcset=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/Object-vs-Classes-300x60.jpg 300w, https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-content\/uploads\/sites\/17\/2020\/08\/Object-vs-Classes.jpg 659w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>You can also think of fruits: apple, orange, banana, etc<\/p>\n<p>How does all of this relate to programing? Well, Let&#8217;s see<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Creating Classes\u00a0 C++<\/strong><\/h4>\n<p>Since we now know what classes an object are, we should be able to create them. We define a class in C++ using the class keyword. Then we provide the class name followed by curly braces. Inside, the curly braces, we then specify it&#8217;s attributes and methods.<\/p>\n<p>Here&#8217;s a class that represents a circle<\/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;\">Square<\/span> {\r\n<span style=\"color: #997700; font-weight: bold;\">public:<\/span> \r\n\t<span style=\"color: #333399; font-weight: bold;\">double<\/span> length;\r\n\t<span style=\"color: #333399; font-weight: bold;\">double<\/span> width;\r\n\t\r\n\t<span style=\"color: #333399; font-weight: bold;\">double<\/span> <span style=\"color: #0066bb; font-weight: bold;\">calculateArea<\/span>(){\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> length <span style=\"color: #333333;\">*<\/span> width;\r\n\t}\r\n\t\r\n\t<span style=\"color: #333399; font-weight: bold;\">double<\/span> <span style=\"color: #0066bb; font-weight: bold;\">calculatePerimeter<\/span>(){\r\n\t\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">2<\/span> <span style=\"color: #333333;\">*<\/span> length <span style=\"color: #333333;\">*<\/span> width;\r\n\t}\r\n};\r\n<\/pre>\n<p>So we created a class named Circle.<\/p>\n<p>The variables length and width are the attributes of the class. They are also called <strong><em>data members<\/em><\/strong> or<em><strong> member variables<\/strong><\/em>.<\/p>\n<p>The functions calculateArea() and calculatePerimeter() are known as <em><strong>member functions<\/strong><\/em>.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Creating Objects from Class<\/strong><\/h4>\n<p>As mentioned before, a class is a blueprint for creating objects. Therefore, once you have a class, you can start creating objects from it.<\/p>\n<p>Creating objects from a class in called instantiation. So we can, &#8220;<strong>instantiate a circle<\/strong>&#8220;. In order words, create an instance of a Square or object of type, Square.<\/p>\n<p>Objects are instantiated same way we create <a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-data-structures\/\" target=\"_blank\" rel=\"noopener noreferrer\">Structs<\/a> or declare variables. For example:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Square square1; <span style=\"color: #888888;\">\/\/Declare square1 of type Square<\/span>\r\nSquare square2; <span style=\"color: #888888;\">\/\/Declare square1 of type Square<\/span>\r\n<\/pre>\n<p>This code instantiates two Squares, square1 and square2.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">.4. Accessing Data Members &amp; Member Functions<\/strong><\/h4>\n<p>So if we have created an object, we should be able to access it&#8217;s attributes(data members). Or we may want to call it&#8217;s member functions.<\/p>\n<p>To achieve this, we use the dot(.) operator.<\/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 \"Square.cpp\" <\/span><span style=\"color: #888888;\">\/\/the class is in another file<\/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;\">\/\/ main function<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n\r\n\t<span style=\"color: #888888;\">\/\/Create a new Square, square1<\/span>\r\n\tSquare square1;\r\n\r\n\t<span style=\"color: #888888;\">\/\/assign values to the data members<\/span>\r\n\tsquare1.length <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">20.2<\/span>;\r\n\tsquare1.width <span style=\"color: #333333;\">=<\/span> <span style=\"color: #6600ee; font-weight: bold;\">10.5<\/span>;\r\n\r\n\t<span style=\"color: #888888;\">\/\/Calculate the area and perimeter<\/span>\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Area is: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> square1.calculateArea()<span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span><span style=\"background-color: #fff0f0;\">\"Perimeter is: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> square1.calculatePerimeter() <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 is:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Area is: 212.1\r\nPerimeter is: 424.2\r\n<\/pre>\n<p>one thing I would you to see from the program is that the class is defined in another file, <em>Square.cpp<\/em>. Then it is included in the main file using the <strong>#include<\/strong> directive.<\/p>\n<p>Also note that the data members and member functions are declared a public. This means that they can be accessed from anywhere from the program.<\/p>\n<p>Now\u00a0 we have cover some basics of OOP. But there&#8217;s yet much to learn. So let me end this lesson by mentioning few benefits of OOP<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Benefits of OOP<\/strong><\/h4>\n<ul>\n<li>make the program clearer and more readable<\/li>\n<li>OOP programs are faster in execution<\/li>\n<li>they help to create easier to maintain codes since classes can be reused<\/li>\n<li>less code and shorter development time<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>We are not stepping into\u00a0 the world of OOP. Somehow, every modern programming language supports some form of OOP features. Let&#8217;s first get through the &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":[36],"class_list":["post-221","post","type-post","status-publish","format-standard","hentry","category-c-tutorials","category-oop","tag-oop"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/221","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=221"}],"version-history":[{"count":1,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/221\/revisions"}],"predecessor-version":[{"id":223,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/221\/revisions\/223"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}