{"id":49,"date":"2017-02-06T22:46:07","date_gmt":"2017-02-07T03:46:07","guid":{"rendered":"http:\/\/commons.trincoll.edu\/trinityrobotics\/?p=49"},"modified":"2017-02-06T22:46:07","modified_gmt":"2017-02-07T03:46:07","slug":"intricacies-with-c-and-c-oop-week-3","status":"publish","type":"post","link":"https:\/\/commons.trincoll.edu\/trinityrobotics\/intricacies-with-c-and-c-oop-week-3\/","title":{"rendered":"Intricacies with C++ and C++ OOP &#8211; Week 3"},"content":{"rendered":"<p><em>Note: Both C++ and OOP in C++ are enormous topics, so the mechanisms\/concepts discussed here will be a small, limited subset of the subject. Also, general programming\/OOP concepts are not addressed.<\/em><\/p>\n<p><strong>General Intricacies of C++<\/strong><\/p>\n<ul>\n<li>C++ is wholly compatible with C, which means that you can compile and run C programs as C++ programs, with some changes with the library\/header names. The C libraries in C++ are prefixed with c, i.e. the\u00a0<code>string.h<\/code> is <code>cstring<\/code> in C++.<\/li>\n<li>Previously considered &#8220;lacking&#8221; in conveniences previously, C++ had &#8220;recently&#8221; (2014) gone through a major standard change called C++11. Features added are..\n<ul>\n<li>Standard Template Library (often abbreviated STL). STL allows for a generic (supporting multiple classes) data structure like vectors (which are arrays (linked-list implementation) with dynamic (changeable) size), queues, stacks, etc.\n<ul>\n<li>STL uses things like:\n<ul>\n<li>Iterators (essentially indexes)<\/li>\n<li><code>auto<\/code> (deduces type, often used for iterators due to their long name), e.g.\u00a0<code>auto i = numVector.begin();<\/code> assigns the iterator type to i.<\/li>\n<\/ul>\n<\/li>\n<li>Templates are often specified like template<code>&lt;classname T&gt;.<\/code>\u00a0So, if you see a function\u00a0specified with such brackets, you can infer that the function is designed to be used with multiple classes.<\/li>\n<\/ul>\n<\/li>\n<li>There is now\u00a0<code>nullptr<\/code> to indicate null pointers (<code>NULL<\/code> is known to cause issues in error handling)<\/li>\n<\/ul>\n<\/li>\n<li>Nor do C++ have many of the practical features implemented, so many\/most C++ programmers complement the language with a library called Boost, which provides features for Serialization, I\/O streams (often used for parsing input\/data), and Math.<\/li>\n<li>C++ print to standard out using\u00a0<code>std::cout &lt;&lt; \"string\" &lt;&lt; endl;<\/code> and standard error\u00a0<code>std::cerr &lt;&lt; \"string\" &lt;&lt; endl;<\/code>, eschewing commonly used print statement.<\/li>\n<li>Like C, the default in C++ is that values passed into functions are <strong>copied<\/strong>, rather than via reference like Java &#8211; which can cause performance issues. That is the reason why so many functions have\u00a0<code>&amp;<\/code> notation next to the parameters, indicating that the parameters are passed via\u00a0<em>&#8220;reference&#8221;,\u00a0<\/em>or more accurately, via <em>pointers<\/em> (unfortunately, I can&#8217;t go into pointers here).<\/li>\n<li>You can have default arguments\/paramters in C++.<\/li>\n<li>C++ have no garbage collection, which means that you have to destroy everything that has been created <em>dynamically\u00a0<\/em>with the <code>new<\/code> operator with <code>delete<\/code>. Use of C&#8217;s <code>malloc<\/code> and <code>free<\/code> is discouraged.<\/li>\n<\/ul>\n<p><strong>OOP intricacies with C++<\/strong><\/p>\n<ul>\n<li>C++ is more flexible with overriding things in classes in general, allowing the overriding of things operators (+, &lt;&lt;), destructors (things that destroy class constructor).<\/li>\n<li>C++ have something called\u00a0<strong>namespaces\u00a0<\/strong>(which is also used in ROS) to organize things variables and functions without the structure of a class.\n<ul>\n<li>Namespaces are used to remediate the problem of global variables. Global variables are <strong>evil<\/strong>, as you could unintentionally call global variables from other places of the project without knowing &#8211; i.e. if you declare a global variable\u00a0with a generic name of\u00a0<em>speed,\u00a0<\/em>the variable can now be used\/changed everywhere.\n<ul>\n<li>Alternatives to global variables are enums.<\/li>\n<\/ul>\n<\/li>\n<li>Namespaces are called\u00a0<code>name::variable<\/code> and declared\u00a0<code>namespace name { auto variable = 0; }<\/code>\n<ul>\n<li>Class functions and variables, too, are called as \u00a0<code>class::variable.<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>You indicate public, protected, private parts of class using notations\u00a0<code>public:, private:, protected:<\/code><\/li>\n<li>Objects\/Instances of the class will be destroyed once the\u00a0<em>scope\u00a0<\/em>of the instance is exited, e.g. with\u00a0<code>void init() { Dog d = Dog(); },<\/code> Dog d\u00a0will be destroyed once init function ends. You can avoid this using the\u00a0<code>new<\/code> operator discussed earlier.<\/li>\n<\/ul>\n<p>Feel free to inform me if there is any part of C++ code that is confusing &#8211; I&#8217;ll add onto the list.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: Both C++ and OOP in C++ are enormous topics, so the mechanisms\/concepts discussed here will be a small, limited subset of the subject. Also, general programming\/OOP concepts are not addressed. General Intricacies of C++ C++ is wholly compatible with C, which means that you can compile and run C programs as C++ programs, with &hellip; <a href=\"https:\/\/commons.trincoll.edu\/trinityrobotics\/intricacies-with-c-and-c-oop-week-3\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Intricacies with C++ and C++ OOP &#8211; Week 3&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1933,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/posts\/49"}],"collection":[{"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/users\/1933"}],"replies":[{"embeddable":true,"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":2,"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/posts\/49\/revisions\/51"}],"wp:attachment":[{"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/commons.trincoll.edu\/trinityrobotics\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}