{"id":2935,"date":"2022-11-07T05:37:40","date_gmt":"2022-11-07T00:07:40","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2935"},"modified":"2022-11-07T05:37:40","modified_gmt":"2022-11-07T00:07:40","slug":"why-learn-c","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2022\/11\/07\/why-learn-c\/","title":{"rendered":"Why Learn C++"},"content":{"rendered":"<p>This is the third and the last article in the series <em><strong>\u201cWhy Learn Language-XYZ?\u201d<\/strong><\/em>. I talked about <a href=\"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/06\/why-learn-prolog\/\" target=\"_blank\" rel=\"noopener\"><em><strong>Prolog<\/strong><\/em><\/a> and <a href=\"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/20\/why-learn-lisp\/\" target=\"_blank\" rel=\"noopener\"><em><strong>Lisp<\/strong><\/em><\/a> earlier. The present article is on <em><strong>C++<\/strong><\/em>.<\/p>\n<p>Once you have gained reasonable proficiency with <em><strong>Prolog<\/strong><\/em> and <em><strong>Lisp<\/strong><\/em>, you are ready to learn <em><strong>C++<\/strong><\/em>! In my view, <em><strong>C++<\/strong><\/em> is a complex language and requires sufficient maturity (and patience) to master. But the effort is well worth it.<\/p>\n<p>So what is special about <em><strong>C++<\/strong><\/em>?<\/p>\n<h3>Strongly Typed<\/h3>\n<p>Unlike <em><strong>Prolog<\/strong><\/em> and <em><strong>Lisp<\/strong><\/em>, <em><strong>C++<\/strong><\/em> is a statically-typed language requiring the programmer to specify the type of variables and function signatures (the <em><strong>\u201cauto\u201d<\/strong><\/em> keyword permits automatic type inferencing by the compiler in some cases) when they are defined. It is also <em><strong>strongly-typed,<\/strong><\/em> with clear rules to check compatibility of types when types are mixed. This allows the compiler to generate efficient code, and also detects potential bugs before the code is executed.<\/p>\n<h3>Continuously Evolving<\/h3>\n<p><em><strong>C++<\/strong><\/em> has a rich <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/history\" target=\"_blank\" rel=\"noopener\"><em><strong>heritage<\/strong><\/em><\/a> of over 40 years! Its creator <em><strong>Bjarne Stroustrup<\/strong><\/em> implemented <em><strong>\u201cC with Classes\u201d<\/strong><\/em> in 1979 and the language has since then been growing steadily. It was first standardized in 1998. Several updates and enhancements have been made after that at fairly regular intervals. The last official release was <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/20\" target=\"_blank\" rel=\"noopener\"><em><strong>\u201cC++ 20\u201d<\/strong> <\/em><\/a>and the next one, <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/23\" target=\"_blank\" rel=\"noopener\"><em><strong>\u201cC++ 23\u201d<\/strong><\/em> <\/a>is expected next year. Every release adds interesting features to the language without sacrificing what <em><strong>C++<\/strong> <\/em>is known for: efficiency!<\/p>\n<h3>Multiparadigm<\/h3>\n<p><em><strong>C++<\/strong><\/em> is a multiparadigm language. It supports <em><strong>Procedural<\/strong><\/em> and <em><strong>Object-oriented<\/strong><\/em> paradigms (both single and multiple inheritance). Although it is not a <em><strong>Functional<\/strong><\/em> programming language in the sense of <em><strong>Lisp<\/strong><\/em>, it is possible to write elegant functional-style programs in it [1]. A knowledgeable <em><strong>C++<\/strong><\/em> programmer can use the right combination of these features to craft a program that is reusable and maintainable, without sacrificing performance.<\/p>\n<h3>Generic Programming<\/h3>\n<p><em><strong>\u201cTemplates\u201d<\/strong><\/em> in <em><strong>C++<\/strong><\/em> are quite versatile. They allow writing <em><strong>generic<\/strong><\/em> code that is type-safe at the same time. And because they are <a href=\"https:\/\/matt.might.net\/articles\/c++-template-meta-programming-with-lambda-calculus\/\" target=\"_blank\" rel=\"noopener\"><em><strong>Turing Complete<\/strong><\/em><\/a>, it is possible to do exciting <em><strong>\u201cmetaprogramming\u201d<\/strong><\/em> [2]. In practice, combining <em><strong>Templates<\/strong><\/em> and <em><strong>Inheritance<\/strong><\/em> allows one to model abstractions elegantly.<\/p>\n<h3>Support for Concurrency<\/h3>\n<p>The <em><strong>Standard Library<\/strong><\/em> has excellent support for asynchronous, concurrent and parallel programming. There are both sequential and parallel versions of many of the standard operations such as <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/search\" target=\"_blank\" rel=\"noopener\"><em><strong>\u201csearch\u201d<\/strong><\/em><\/a>,<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/sort\" target=\"_blank\" rel=\"noopener\"><em><strong> \u201csort\u201d<\/strong><\/em><\/a>,<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/transform\" target=\"_blank\" rel=\"noopener\"><em><strong> \u201ctransform\u201d<\/strong><\/em><\/a>, etc. When used with care, concurrency can yield significant performance improvement.<\/p>\n<h3>Obsession with Efficiency<\/h3>\n<p>One of the main reasons to choose <em><strong>C++<\/strong><\/em> is the control one has over writing <em><strong>efficient code<\/strong><\/em> (efficient algorithm comes before code efficiency). The built-in primitive data types allow the developer to define and use variables that are just right for the context, and thereby controlling the amount of memory used by the program at runtime. Function calls can be <em><strong>\u201cinlined\u201d<\/strong><\/em> as appropriate, to eliminate call overheads. Automatic garbage collection is not part of <em><strong>C++<\/strong><\/em> and hence the developer controls the lifetime of every object. It is even possible to take over memory management by defining custom allocators as needed. Although it is generally considered unsafe, <em><strong>\u201cpointer\u201d<\/strong><\/em> gives direct handle to raw memory. Additionally, the <em><strong>\u201calignas\u201d<\/strong><\/em> keyword and the recently introduced <em><strong>\u201c[[no_unique_address]]\u201d<\/strong><\/em> attribute help the compiler optimize memory usage of composite structures. Finally, many compilers allow embedding of <em><strong>assembly language<\/strong><\/em> code within a <em><strong>C++<\/strong><\/em> program, giving greater control over the code generated, in addition to supporting a wide variety of optimization <em><strong>switches<\/strong><\/em>.<\/p>\n<p>For the record, C++ follows the <a href=\"https:\/\/isocpp.org\/wiki\/faq\/big-picture#zero-overhead-principle\" target=\"_blank\" rel=\"noopener\"><em><strong>\u201cZero-overhead\u201d principle<\/strong><\/em><\/a>:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">\u201cThe zero-overhead principle is a guiding principle for the design of C++. It states that: What you don\u2019t use, you don\u2019t pay for (in time or space) and further: What you do use, you couldn\u2019t hand code any better.\u201d<\/span><\/p><\/blockquote>\n<p>To support writing such high-quality code, <em><strong>C++<\/strong><\/em> development environments typically support a host of tools such <em><strong>Debugger<\/strong><\/em>, <em><strong>Profiler<\/strong><\/em> and <em><strong>Code Coverage Analyzer<\/strong><\/em>. And this is one of the main reasons to use <em><strong>C++<\/strong><\/em> in performance-intensive applications.<\/p>\n<h3>As Close to Hardware As Possible<\/h3>\n<p><em><strong>C++<\/strong><\/em> allows developers to write <em><strong>low-level<\/strong> <\/em>code that can interact with the underlying hardware and hence it is an ideal language to write device drivers and operating systems. It is also widely used to program micro-controllers. Why is this so? There are several language features that make this possible. A broad range of integral types encourages the use of appropriate types to match the underlying <em><strong>CPU<\/strong><\/em> architecture. <em><strong>\u201cBit-fields\u201d<\/strong><\/em> permit structure member packing to optimize memory and to map to hardware ports. <em><strong>\u201cPointers\u201d<\/strong><\/em> come in handy when directly accessing raw memory. Of course, being able to embed assembly code and efficient code generation by compilers are additional benefits!<\/p>\n<h3>Rich Libraries<\/h3>\n<p>The <em><strong>Standard Template Library (STL)<\/strong><\/em> that is part of the <em><strong>C++<\/strong><\/em> language is quite feature rich and keeps growing with every release of the standard. In addition, for almost every task you can think of, there are commercial and\/or open-source libraries.<\/p>\n<p>Here is one of my favourite quotes about C++:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">\u201cC++ is biased towards programmers who think for themselves\u201d<\/span><br \/>\n<span style=\"color: #0000ff;\">&#8211; Andrew Koenig [3]<\/span><\/p><\/blockquote>\n<p>Once you gain a good understanding and working proficiency in <em><strong>Prolog<\/strong><\/em>, <em><strong>Lisp<\/strong><\/em> and <em><strong>C++, <\/strong><\/em>you are ready to<em><strong> &#8220;take on&#8221; <\/strong><\/em>the world! You will be a lot wiser and learning other languages will be considerably easier. Since new languages are being designed regularly, it is essential for the working professional to become familiar with as many languages as is needed for survival. I feel it is not that important to be focused on the \u201c<em><strong>Top 10 Languages\u201d<\/strong><\/em> that appear periodically on the internet. They key thing is to <em><strong>enjoy<\/strong><\/em> the experience of learning and using a new language. I have always done that.<\/p>\n<p>Have a great week!<\/p>\n<h3 style=\"text-align: center;\">References<\/h3>\n<p>1) Ivan Cukik, \u201cFunctional Programming in C++: How to improve your C++ programs using functional techniques\u201d, Manning Publications, 2019.<br \/>\n2) David Vandevoorde, Nicolai M. Josuttis, Douglas Gregor, \u201cC++ Templates: The Complete Guide\u201d, Addison-Wesley, 2018.<br \/>\n3) Andrew Koenig, \u201cRuminations on C++\u201d, Addison-wesley, 1997.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the third and the last article in the series \u201cWhy Learn Language-XYZ?\u201d. I talked about Prolog and Lisp earlier. The present article is on C++. Once you have gained reasonable proficiency with Prolog and Lisp, you are ready to learn C++! In my view, C++ is a complex language and requires sufficient maturity [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[49,17],"tags":[67],"class_list":["post-2935","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-Ll","jetpack-related-posts":[{"id":3555,"url":"https:\/\/www.rangakrish.com\/index.php\/2024\/11\/22\/multiobjective-optimization-moo-in-lisp-and-prolog\/","url_meta":{"origin":2935,"position":0},"title":"Multiobjective Optimization (MOO) in Lisp and Prolog","author":"admin","date":"November 22, 2024","format":false,"excerpt":"Recently I came across a nice article\u00a0by Jose Crespo, where the author stresses that the future of programming revolves around the application of math concepts such as Functor, Monads, Folds, etc. In addition, he argues that familiarity with C\/C++ is essential in this modern AI age. The author goes through\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Lisp Code for Pareto Front","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2024\/11\/lisp-code-249x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2927,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/20\/why-learn-lisp\/","url_meta":{"origin":2935,"position":1},"title":"Why Learn Lisp?","author":"admin","date":"October 20, 2022","format":false,"excerpt":"In the last article, I had shared my views on why programmers should learn Prolog, preferably as the first language. What language should one learn next? I strongly pitch for Lisp, to be precise, \u201cCommon Lisp\u201d. Lisp happens to be the second oldest (1958) programming language, only after Fortran (1957)!\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2366,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/03\/28\/implementing-ilexicon-using-litedb\/","url_meta":{"origin":2935,"position":2},"title":"Implementing iLexicon using LiteDB","author":"admin","date":"March 28, 2021","format":false,"excerpt":"iLexicon is an \"intelligent\" dictionary that can be used to build Natural Language applications. I have two implementations, one in Lisp and another in Prolog. Both implementations are memory-based, in order to speed up performance. I have written several articles referencing it, for example see this. \u00a0 LiteDB is a\u2026","rel":"","context":"In &quot;Natural Language Processing&quot;","block_context":{"text":"Natural Language Processing","link":"https:\/\/www.rangakrish.com\/index.php\/category\/natural-language-processing\/"},"img":{"alt_text":"Sample Commands","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/03\/Session1.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2832,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/12\/definite-clause-grammars-in-lisp-part-4\/","url_meta":{"origin":2935,"position":3},"title":"Definite Clause Grammars in Lisp &#8211; Part 4","author":"admin","date":"June 12, 2022","format":false,"excerpt":"In a series of articles\u00a0written earlier, I had shown how it is possible to model Definite Clause Grammars (DCG) in LispWorks Lisp (Enterprise Edition). We use defgrammar\u00a0in Common Prolog (available as part of KnowledgeWorks package) to define our grammar rules. Here is a toy English grammar represented using defgrammar: This\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"DCG Using Defgrammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/defgrammar-version-300x177.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/defgrammar-version-300x177.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/defgrammar-version-300x177.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2922,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/10\/06\/why-learn-prolog\/","url_meta":{"origin":2935,"position":4},"title":"Why Learn Prolog?","author":"admin","date":"October 6, 2022","format":false,"excerpt":"There are several programming languages in use today and a simple google search will throw up interesting recommendations of a subset of these languages to learn, usually based on popularity ranking. As is expected, the popularity of a programming language varies over time and hence a language that was in\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":534,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/05\/22\/definite-clause-grammars-dcg-in-lisp\/","url_meta":{"origin":2935,"position":5},"title":"Definite Clause Grammars (DCG) in Lisp","author":"admin","date":"May 22, 2017","format":false,"excerpt":"Definite Clause Grammars (DCG) are an elegant formalism for specifying context free grammars, and part of their popularity is due to their support in the Prolog language. Most books on Natural Language processing usually include a brief coverage of DCGs, even though Natural languages are not context-free. Because of the\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"DCG Grammar","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/05\/DCG-Grammar.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/05\/DCG-Grammar.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/05\/DCG-Grammar.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=2935"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2935\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}