{"id":5119,"date":"2017-01-12T13:02:34","date_gmt":"2017-01-12T13:02:34","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/objectoriented-generic-and-functional-programming\/"},"modified":"2017-01-12T13:02:34","modified_gmt":"2017-01-12T13:02:34","slug":"objectoriented-generic-and-functional-programming","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/objectoriented-generic-and-functional-programming\/","title":{"rendered":"Object-Oriented, Generic, and Functional Programming"},"content":{"rendered":"<p>C++ is not a functional programming language. C++ has its roots in procedural and object-oriented programming. So it&#8217;s pretty surprising that programming in a functional style becomes increasingly important in C++. That is not only true for C++. That also holds for Python, which has many functional features, and even for Java. Now Java has lambda functions.<\/p>\n<p>&nbsp;<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<h2>In the beginning, there are the questions<\/h2>\n<p>In the beginning, there are many questions about functional programming in C++:<\/p>\n<ul>\n<li>What functional features does C++ have?<\/li>\n<li>Why are pure functional languages like Haskell such influential?<\/li>\n<li>Which direction is C++ headed?<\/li>\n<li>What are the benefits of functional programming?<\/li>\n<li>What is functional programming?<\/li>\n<li>What are the characteristics of functional programming?<\/li>\n<\/ul>\n<p>Quite a lot of questions that I can not answer in one post. Therefore, I will answer the questions in subsequent posts.<\/p>\n<p>But let me start with a non-asked question. Which programming paradigm does C++ support?<\/p>\n<\/p>\n<h2>A strong simplification<\/h2>\n<p>40 years is a long time in software development. Therefore, it is no big surprise that C++ underwent many metamorphoses.<\/p>\n<p>C began in the early 70th of the last century. 1998 the first C++ standard was published. 13 years later, the area of modern C++ began with C++11. More interesting than the raw numbers is that each of these three steps stands for a different way of solving problems. In C, you think in procedures and structures. C++ introduces object orientation and generic programming, a new kind of abstraction. With C++11, we got the functional programming style.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5118\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/ObjectOrientedGenericFunctional.png\" alt=\"ObjectOrientedGenericFunctional\" width=\"700\" height=\"454\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/ObjectOrientedGenericFunctional.png 908w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/ObjectOrientedGenericFunctional-300x195.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/01\/ObjectOrientedGenericFunctional-768x498.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Before I only write about functional programming, I will sketch the ideas of object-oriented, generic, and functional programming.<\/p>\n<h3>Object-oriented programming<\/h3>\n<p>Object-oriented programming is based on the three concepts of encapsulation, inheritance, and polymorphism.<\/p>\n<dl>\n<dt>Encapsulation<\/dt>\n<dd>\n<p style=\"padding-left: 30px;\">An object encapsulates its attributes and methods and provides them via an interface to the outside world. This property that an object hides its implementation is often called <em>data hiding.<\/em> <em><\/em><\/p>\n<\/dd>\n<dt>Inheritance<\/dt>\n<dd>\n<p style=\"padding-left: 30px;\">A derived class gets all characteristics from its base class. You can use an instance of a derived class as an instance of its base class. We often speak about <em>code reuse<\/em> because the derived class automatically gets all characteristics of the base class.<\/p>\n<\/dd>\n<dt>Polymorphism<\/dt>\n<dd>\n<p style=\"padding-left: 30px;\">Polymorphism is the ability to present the same interface for differing underlying data types. The term is Greek and stands for many forms.<\/p>\n<\/dd>\n<\/dl>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">HumanBeing<\/span>{\r\npublic:\r\n  HumanBeing(std::stringn):name(n){}\r\n  <span style=\"color: #0000ff;\">virtual<\/span> std::string getName() <span style=\"color: #0000ff;\">const<\/span>{\r\n    <span style=\"color: #0000ff;\">return<\/span> name;\r\n  }\r\nprivate:\r\n  std::string name;\r\n};\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Man<\/span>: <span style=\"color: #0000ff;\">public<\/span> HumanBeing{};\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Woman<\/span>: <span style=\"color: #0000ff;\">public<\/span> HumanBeing{}; \r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In the example, you only get the name of <span style=\"font-family: courier new,courier;\">HumanBeing<\/span> by using the method <span style=\"font-family: courier new,courier;\">getName <\/span>in line 4 (encapsulation)<span style=\"font-family: courier new,courier;\">. <\/span>In addition, <span style=\"font-family: courier new,courier;\">getName<\/span> is declared as virtual. Therefore, derived classes can change the behavior of their methods and therefore change the behavior of their objects (polymorphism). <span style=\"font-family: courier new,courier;\">Man<\/span> and <span style=\"font-family: courier new,courier;\">Woman<\/span> are derived from <span style=\"font-family: courier new,courier;\">HumanBeing.<\/span><\/p>\n<h3>Generic programming<\/h3>\n<p>The key idea of generic programming or programming with templates is to define families of functions or classes. You automatically get a function or class for this type by providing the concrete type. Generic programming provides a similar abstraction to object-oriented programming. A big difference is that the polymorphism of object-oriented programming will happen at runtime; that polymorphism of generic programming will happen in C++ at compile time. That is the reason why polymorphism at runtime is often called dynamic polymorphism but polymorphism at compile is often called static polymorphism.<\/p>\n<p>By using the function template, I can exchange arbitrary objects.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">typename<\/span> T&gt; <span style=\"color: #2b91af;\">void<\/span> xchg(T&amp; x, T&amp; y){   \r\n  T t= x;\r\n  x= y;\r\n  y= t;\r\n};\r\n<span style=\"color: #2b91af;\">int<\/span> i= 10;\r\n<span style=\"color: #2b91af;\">int<\/span> j= 20;\r\nMan huber;\r\nMan maier;\r\n\r\nxchg(i,j);\r\nxchg(huber,maier);\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;It doesn&#8217;t matter for the function template if I exchange numbers or men (lines 11 and 12). In addition, I have not specified the type parameter (line) because the compiler can derive it from the function arguments (lines 11 and 12).<\/p>\n<p>The automatic type deduction of function templates will not hold for class templates. In the concrete case, I must specify the type parameter T and the non-type parameter N (line 1).<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">template<\/span> &lt;<span style=\"color: #0000ff;\">typename<\/span> T, <span style=\"color: #2b91af;\">int<\/span> N&gt;\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Array<\/span>{\r\npublic:\r\n  <span style=\"color: #2b91af;\">int<\/span> getSize() <span style=\"color: #0000ff;\">const<\/span>{\r\n    <span style=\"color: #0000ff;\">return<\/span> N;\r\n  }\r\nprivate:\r\n  T elem[N];\r\n};\r\n \r\nArray&lt;<span style=\"color: #2b91af;\">double<\/span>,10&gt; doubleArray;\r\nstd::cout &lt;&lt; doubleArray.getSize() &lt;&lt; std::endl;\r\n\r\nArray&lt;Man,5&gt; manArray;\r\nstd::cout &lt;&lt; manArray.getSize() &lt;&lt; std::endl;\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Accordingly, applying the class template <span style=\"font-family: courier new,courier;\">Array<\/span> is independent of whether I use <span style=\"font-family: courier new,courier;\">doubles<\/span> or <span style=\"font-family: courier new,courier;\">men.<\/span><\/p>\n<h3>Functional programming<\/h3>\n<p>I will only say a few words about functional programming because I will and can not explain the concept of functional programming in a short remark. Only that much. I use the code snippet of the pendants in C++ for the typical functions in functional programming: map, filter, and reduce. These are the functions <span style=\"font-family: courier new,courier;\">std::transform,<\/span>&nbsp;<span style=\"font-family: courier new,courier;\">std::remove_if,<\/span> and <span style=\"font-family: courier new,courier;\">std::accumulate<\/span>.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\">std::vector&lt;<span style=\"color: #2b91af;\">int<\/span>&gt; vec{1,2,3,4,5,6,7,8,9};\r\nstd::vector&lt;std::string&gt; str{<span style=\"color: #a31515;\">\"Programming\"<\/span>,<span style=\"color: #a31515;\">\"in\"<\/span>,<span style=\"color: #a31515;\">\"a\"<\/span>,<span style=\"color: #a31515;\">\"functional\"<\/span>,<span style=\"color: #a31515;\">\"style.\"<\/span>};\r\n\r\nstd::transform(vec.begin(),vec.end(),vec.begin(),\r\n              [](<span style=\"color: #2b91af;\">int<\/span> i){ <span style=\"color: #0000ff;\">return<\/span> i*i; }); <span style=\"color: #008000;\">\/\/ {1,4,9,16,25,36,49,64,81}<\/span>\r\n\r\n<span style=\"color: #0000ff;\">auto<\/span> it= std::remove_if(vec.begin(),vec.end(),\r\n                        [](<span style=\"color: #2b91af;\">int<\/span> i){ <span style=\"color: #0000ff;\">return<\/span> ((i &lt; 3) or (i &gt; 8)) }); <span style=\"color: #008000;\">\/\/ {3,4,5,6,7,8}<\/span>\r\n<span style=\"color: #0000ff;\">auto<\/span> it2= std::remove_if(str.begin(),str.end(),\r\n                         [](string s){ <span style=\"color: #0000ff;\">return<\/span> (std::lower(s[0])); }); <span style=\"color: #008000;\">\/\/ \"Programming\"<\/span>\r\n\r\n\r\nstd::accumulate(vec.begin(),vec.end(),[](<span style=\"color: #2b91af;\">int<\/span> a,<span style=\"color: #2b91af;\">int<\/span> b){<span style=\"color: #0000ff;\">return<\/span> a*b;}); <span style=\"color: #008000;\">\/\/ 362880<\/span>\r\nstd::accumulate(str.begin(),str.end(),\r\n                [](std::string a,std::string b){<span style=\"color: #0000ff;\">return<\/span> a + <span style=\"color: #a31515;\">\":\"<\/span>+ b;});\r\n                <span style=\"color: #008000;\">\/\/ \"Programming:in:a:functional:style.\"<\/span>\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I apply in the code snippet two powerful features of functional programming. Both are mainstream in modern C++: automatic type deduction with <span style=\"font-family: courier new,courier;\">auto<\/span> and lambda functions.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>Which functional features does C++ have? Which one will C++ get with C++17 and C++20? These are the question I will answer in the <a href=\"https:\/\/www.modernescpp.com\/index.php\/functional-in-c-98\">next post<\/a> and the subsequent ones.<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ is not a functional programming language. C++ has its roots in procedural and object-oriented programming. So it&#8217;s pretty surprising that programming in a functional style becomes increasingly important in C++. That is not only true for C++. That also holds for Python, which has many functional features, and even for Java. Now Java has [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":5118,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[365],"tags":[],"class_list":["post-5119","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-functional"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5119"}],"version-history":[{"count":0,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5119\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5118"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}