{"id":4159,"date":"2015-01-19T11:47:24","date_gmt":"2015-01-19T19:47:24","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=4159"},"modified":"2021-06-17T08:23:20","modified_gmt":"2021-06-17T15:23:20","slug":"a-brief-intro-to-c","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/a-brief-intro-to-c\/","title":{"rendered":"A Brief Intro to C++"},"content":{"rendered":"<p>In previous blog posts, we&#8217;ve programmed mainly in C. But with a blog post about classes coming up, I figured a short post about how C++ works would be helpful for everybody.<\/p>\n<p>&nbsp;<\/p>\n<p>We&#8217;re going to\u00a0start out with the classic first program; &#8220;hello world&#8221;.<\/p>\n<p>&nbsp;<\/p>\n<p>Here&#8217;s &#8220;hello world&#8221; in C code:<br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/helloc.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4173\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/helloc.png\" alt=\"helloc\" width=\"242\" height=\"113\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/helloc.png 242w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/helloc-225x105.png 225w\" sizes=\"auto, (max-width: 242px) 100vw, 242px\" \/><\/a><\/p>\n<p>Comparatively, here&#8217;s &#8220;hello world&#8221; in C++:<br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/hello.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4166\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/hello.png\" alt=\"hello\" width=\"368\" height=\"136\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/hello.png 368w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/hello-225x83.png 225w\" sizes=\"auto, (max-width: 368px) 100vw, 368px\" \/><\/a><\/p>\n<p>The very first line, we can see a difference\u00a0&#8212; that\u00a0<em>stdio.h<\/em> has been replaced by <em>iostream<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<p>We can break into input\/output stream. Input and output are pretty self-explanatory. Stream is new, but not extremely complicated.\u00a0A stream is just a series of characters. For this blog post, all we&#8217;re using are the istream and ostreams, but there are other types of streams.<\/p>\n<p>&nbsp;<\/p>\n<p>The implication of stream means we aren&#8217;t going to use printf anymore. We use cout to print items to the terminal, with &#8220;&gt;&gt;&#8221; between data. Also in the cout line you see that cout and endl both have &#8220;std::&#8221; in front of them. If we put the line &#8220;using namespace std&#8221; below our header definition, we don&#8217;t have to put &#8220;std::&#8221; in front of cout and endl (and similar items). This is considered bad practice, because if we include multiple namespace, calling functions can become ambiguous, and cause problems.<\/p>\n<p>&nbsp;<\/p>\n<p>C++ is an object-oriented programming (OOP) and generic programming language. The implication of object-oriented programming means that C++ has classes, objects, encapsulation, operator overloading, inheritance, and polymorphism. Generic programming focuses on function and class templates. In this blog post, we are going to learn about function and operator overloading, and the difference between C and C++, so classes and other features of C++ will be in a later post.<br \/>\nC++ allows functions to be defined by the same name, as long as they all have different parameters. This is called function overloading.<br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/function.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4183\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/function.png\" alt=\"function\" width=\"396\" height=\"482\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/function.png 396w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/function-185x225.png 185w\" sizes=\"auto, (max-width: 396px) 100vw, 396px\" \/><\/a><br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/function.txt\">function.c<\/a><\/p>\n<p>If you run this code, you will see that the &#8220;print&#8221; calls have different inputs, which then changes what function will be called. We called &#8220;using namespace std&#8221;, so we must be careful to not create any functions that are already in the std library. Another small difference between C and C++ is the &#8220;cin&#8221; function.<\/p>\n<p>&nbsp;<\/p>\n<p>Below is an addition to our previous code (the link has all the code):<\/p>\n<p><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/cin.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4191\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/cin.png\" alt=\"cin\" width=\"288\" height=\"173\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/cin.png 288w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/cin-225x135.png 225w\" sizes=\"auto, (max-width: 288px) 100vw, 288px\" \/><\/a><br \/>\n<a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/cin.txt\">cin.c<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>If you check out\u00a0cin, you see it uses &#8220;&gt;&gt;&#8221;, while cout uses &#8220;&lt;&lt;&#8220;. I recommend typing in some invalid inputs when prompted for an integer (like &#8216;123apple&#8217;) just to see what happens!<\/p>\n<p>&nbsp;<\/p>\n<p>This is just one of the features that C++ has. There are many other differences from C, but if you know how to program in C, C++ is only a small jump away.<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-4159 jlk' data-task='like' data-post_id='4159' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-4159 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-4159 jlk' data-task='unlike' data-post_id='4159' data-nonce='ee750c7abc' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-4159 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-4159 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>In previous blog posts, we&#8217;ve programmed mainly in C. But with a blog post about classes coming up, I figured a short post about how C++ works would be helpful for everybody.<\/p>\n","protected":false},"author":29,"featured_media":4166,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1563],"tags":[144],"ppma_author":[4478],"class_list":["post-4159","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guide","tag-tutorial-2"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/hello.png","authors":[{"term_id":4478,"user_id":29,"is_guest":0,"slug":"josh-woldstad","display_name":"Josh","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/b4b62729a3daba9bb76117db7130e81e?s=96&d=mm&r=g","author_category":"","user_url":"","last_name":"Woldstad","last_name_2":"","first_name":"Josh","first_name_2":"","job_title":"","description":"I love Coding!"}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/4159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/29"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=4159"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/4159\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/4166"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=4159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=4159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=4159"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=4159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}