{"id":18,"date":"2019-04-05T15:43:52","date_gmt":"2019-04-05T15:43:52","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=18"},"modified":"2020-08-06T09:51:44","modified_gmt":"2020-08-06T09:51:44","slug":"02-c-basic-syntax","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/02-c-basic-syntax\/","title":{"rendered":"C++ Basic Syntax"},"content":{"rendered":"<p>In this chapter, we would learn the basic syntax of the C++ programming language.<\/p>\n<p>We would cover the following:<\/p>\n<ol>\n<li><a href=\"#t1\">Basic OOP Terms<\/a><\/li>\n<li><a href=\"#t2\">Structure of C++ Program<\/a><\/li>\n<li><a href=\"#t3\">Runing a C++ Program in Eclipse<\/a><\/li>\n<li><a href=\"#t4\">Code Blocks and Lines<\/a><\/li>\n<li><a href=\"#t5\">Identifiers in C++<\/a><\/li>\n<li><a href=\"#t6\">C++ Reserved Words<\/a><\/li>\n<li><a href=\"#t7\">Trigraphs<\/a><\/li>\n<li><a href=\"#t8\">Whitespe Characters in C++<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Basic OOP Terms<\/strong><\/h4>\n<p>OOP means Object Oriented Programming. Some OOP terms are given below:<\/p>\n<p>Let\u2019s reviews some of the important OOP terms.<\/p>\n<p><strong>Class<\/strong> \u2013 A template for creating new objects. A class specifies the attributes and behaviors of an object.<\/p>\n<p><strong>Class Variable<\/strong> \u2013 This is a variable that is shared by all objects that are created from the class. They are reference using the name of the class<\/p>\n<p><strong>Data Member<\/strong> \u2013 Variables defined in the class. They are used to hold data associated with the class<\/p>\n<p><strong>Instance Variable<\/strong> \u2013 These is a variable that is defined in a class but belongs only to the particular object of the class.<\/p>\n<p><strong>Instance<\/strong>\u00a0\u2013 An object created from a class<\/p>\n<p><strong>Instantiation<\/strong> \u2013 The process of creating an instance from a class<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Structure of C++ Program<\/strong><\/h4>\n<p>Let&#8217;s write a simple Hello World program and then try to understand it. This is given below:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\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;\">\/\/Program execution begins here<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Welcome to C++\"<\/span>; <span style=\"color: #888888;\">\/\/ displays Welcome to C++<\/span>\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>&nbsp;<\/p>\n<p>Let&#8217;s now see what the various componets are:<\/p>\n<p>The first line<em><strong> #include &lt;iostream&gt;<\/strong><\/em> is known as preprocessor directive. It indicates that the file iostream is needed to compile this file<\/p>\n<p>The second line <em><strong>using namespace std<\/strong><\/em> tell the compiler to use the namespace std. For now, think of a namespace as a collection of useful objects.<\/p>\n<p>The line that begins in \/\/ is a comment. It is ignored by the compiler<\/p>\n<p>The line <em><strong>int main()<\/strong><\/em> defines a function main. This is where the program execution actually starts<\/p>\n<p><em><strong>cout &lt;&lt;&#8220;Welcome to C++&#8221;,<\/strong><\/em> print the text to the screen<\/p>\n<p>The line<em><strong> return 0<\/strong><\/em> terminates teh main function<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Runing a C++ Program in Eclipse<\/strong><\/h4>\n<p>If you have correctly installled Eclipse and Set up the MinGW compiler as outlined in <a href=\"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-installation-and-setup\/\">Lesson 2,<\/a> then open Eclipse, click on file and choose C++ project.<\/p>\n<p>I recommend you watch the video to get clarification.<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/t45bfb5nLlU\" width=\"100%\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><span data-mce-type=\"bookmark\" style=\"display: inline-block; width: 0px; overflow: hidden; line-height: 0;\" class=\"mce_SELRES_start\">\ufeff<\/span><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Code Blocks and Lines<\/strong><\/h4>\n<p>Notice that statements in C++ are terminated by a semicolon (;). It indicates the end of the line. For example:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">a <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>;\r\nb <span style=\"color: #333333;\">=<\/span> b <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>;\r\nsum(a, b);\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Also, a block of statements are enclosed in curly braces. For example:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span>() {\r\n   cout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Welcome to C++\"<\/span>; <span style=\"color: #888888;\">\/\/ displays Welcome to C++<\/span>\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>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t5\">5. Identifiers in C++<\/strong><\/h4>\n<p>As a programmer, you have the power to choose names of identifiers. Identifies are simply names given to variables.<\/p>\n<p>However, there are some rule to follow when choosing names of identifiers.<\/p>\n<ul>\n<li>Identifier names must start with letters A to Z or a to z or an underscore(_)<\/li>\n<li>Special characters like %, !, * and others cannot be used in identifier names<\/li>\n<li>Identifiers can contain numbers (but not as the first character)<\/li>\n<li>Names in C++ are case-sensitive<\/li>\n<\/ul>\n<p>Examples to valid identifiers are: eze, book1, _score, one2four, x, z, a1<\/p>\n<p>Examples of invalid identifiers are: 45house, love.me,\u00a0 pass@word, 1996<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t6\">6. C++ Reserved Words<\/strong><\/h4>\n<p>Reserved words are also known as keywords. They are words reserved for use by the language. Therefore you can&#8217;t choose them as identifiers. They are listed below:<\/p>\n<table class=\"table table-bordered\" width=\"100%\" align=\"center\">\n<tbody>\n<tr>\n<td>asm<\/td>\n<td>else<\/td>\n<td>new<\/td>\n<td>this<\/td>\n<\/tr>\n<tr>\n<td>auto<\/td>\n<td>enum<\/td>\n<td>operator<\/td>\n<td>throw<\/td>\n<\/tr>\n<tr>\n<td>bool<\/td>\n<td>explicit<\/td>\n<td>private<\/td>\n<td>true<\/td>\n<\/tr>\n<tr>\n<td>break<\/td>\n<td>export<\/td>\n<td>protected<\/td>\n<td>try<\/td>\n<\/tr>\n<tr>\n<td>case<\/td>\n<td>extern<\/td>\n<td>public<\/td>\n<td>typedef<\/td>\n<\/tr>\n<tr>\n<td>catch<\/td>\n<td>false<\/td>\n<td>register<\/td>\n<td>typeid<\/td>\n<\/tr>\n<tr>\n<td>char<\/td>\n<td>float<\/td>\n<td>reinterpret_cast<\/td>\n<td>typename<\/td>\n<\/tr>\n<tr>\n<td>class<\/td>\n<td>for<\/td>\n<td>return<\/td>\n<td>union<\/td>\n<\/tr>\n<tr>\n<td>const<\/td>\n<td>friend<\/td>\n<td>short<\/td>\n<td>unsigned<\/td>\n<\/tr>\n<tr>\n<td>const_cast<\/td>\n<td>goto<\/td>\n<td>signed<\/td>\n<td>using<\/td>\n<\/tr>\n<tr>\n<td>continue<\/td>\n<td>if<\/td>\n<td>sizeof<\/td>\n<td>virtual<\/td>\n<\/tr>\n<tr>\n<td>default<\/td>\n<td>inline<\/td>\n<td>static<\/td>\n<td>void<\/td>\n<\/tr>\n<tr>\n<td>delete<\/td>\n<td>int<\/td>\n<td>static_cast<\/td>\n<td>volatile<\/td>\n<\/tr>\n<tr>\n<td>do<\/td>\n<td>long<\/td>\n<td>struct<\/td>\n<td>wchar_t<\/td>\n<\/tr>\n<tr>\n<td>double<\/td>\n<td>mutable<\/td>\n<td>switch<\/td>\n<td>while<\/td>\n<\/tr>\n<tr>\n<td>dynamic_cast<\/td>\n<td>namespace<\/td>\n<td>template<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t7\">7. Trigraphs<\/strong><\/h4>\n<p>Though you may not use trigraphs often, it is a concept you should know. It is also called a trigraph sequence.<\/p>\n<p>It is made up of three characters. These three characters represents a single character equivalent. It always begins with as question mark.<\/p>\n<p>If you include a trigraph in a string, then when the program executes, it is replaced by the single-character equivalent.<\/p>\n<p>Some trigraphs are given below:<\/p>\n<table class=\"table table-bordered\" width=\"100%\" align=\"center\">\n<tbody>\n<tr>\n<th>Trigraph<\/th>\n<th>Single-character equivalent<\/th>\n<\/tr>\n<tr>\n<td>??=<\/td>\n<td>#<\/td>\n<\/tr>\n<tr>\n<td>??\/<\/td>\n<td>\\<\/td>\n<\/tr>\n<tr>\n<td>??&#8217;<\/td>\n<td>^<\/td>\n<\/tr>\n<tr>\n<td>??(<\/td>\n<td>[<\/td>\n<\/tr>\n<tr>\n<td>??)<\/td>\n<td>]<\/td>\n<\/tr>\n<tr>\n<td>??!<\/td>\n<td>|<\/td>\n<\/tr>\n<tr>\n<td>??&lt;<\/td>\n<td>{<\/td>\n<\/tr>\n<tr>\n<td>??&gt;<\/td>\n<td>}<\/td>\n<\/tr>\n<tr>\n<td>??-<\/td>\n<td>~<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>However, trigraphs are not supported by all compilers. So it is not generally used by programmers.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t8\">8. Whitespace Characters in C+++<\/strong><\/h4>\n<p>Whitespaces are ignored by the C++ compiler. This could also be a blank line, newline and tab.<\/p>\n<p>However, you should use whitespaces to seperate parts of your code. This makes your code more-readable.<\/p>\n<p>Let&#8217;s take an example<\/p>\n<p>&nbsp;<\/p>\n<p>Code written without whitespaces<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ not very good code <\/span>\r\na <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>; b <span style=\"color: #333333;\">=<\/span> b <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>; sum(a, b);\r\n<span style=\"color: #008800; font-weight: bold;\">if<\/span>(a <span style=\"color: #333333;\">==<\/span> b) { c <span style=\"color: #333333;\">=<\/span> e <span style=\"color: #333333;\">+<\/span> ; }\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Code written with whitepaces<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/ not very good code <\/span>\r\na <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">10<\/span>; \r\nb <span style=\"color: #333333;\">=<\/span> b <span style=\"color: #333333;\">+<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>; \r\nsum(a, b);\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">if<\/span>(a <span style=\"color: #333333;\">==<\/span> b) {\r\n    c <span style=\"color: #333333;\">=<\/span> e <span style=\"color: #333333;\">+<\/span> ; \r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this chapter, we would learn the basic syntax of the C++ programming language. We would cover the following: Basic OOP Terms Structure of C++ &hellip; <\/p>\n","protected":false},"author":1,"featured_media":21,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-tutorials"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/18","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=18"}],"version-history":[{"count":4,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":46,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/18\/revisions\/46"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media\/21"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}