{"id":920,"date":"2024-08-09T20:19:27","date_gmt":"2024-08-09T20:19:27","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=920"},"modified":"2025-01-26T18:21:30","modified_gmt":"2025-01-26T18:21:30","slug":"c-classes","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/classes\/c-classes\/","title":{"rendered":"What Are Classes?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Are_Classes\"><\/span>What Are Classes?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are the basics of C++ Classes. In particular:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Declarations and Definitions<\/li>\n\n\n\n<li>Member Functions <\/li>\n\n\n\n<li>Constructors and Destructors<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Class_Declarations\"><\/span>Class Declarations<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here\u2019s a simple <em>class declaration<\/em> for a complex number:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ complex.h\nclass Complex \n{ \npublic:    \n  \/\/ sets values    \n  void set(const double r, \n           const double i);\n\n  \/\/ prints values\n  void print();\n\nprivate:    \n    double real, imag; \n};\t\t\/\/ end of class declaration (semicolon required)\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ complex.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{ <\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ sets values    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">set<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\">, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">           <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ prints values<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real, imag; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><span style=\"color: #008000\">\t\t\/\/ end of class declaration (semicolon required)<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>It consists of:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <em>class name<\/em>, <strong>Complex<\/strong><\/li>\n\n\n\n<li>Several <em>class members<\/em>:<\/li>\n\n\n\n<li><em>data members<\/em> <strong>real<\/strong> and <strong>imag<\/strong>, and&nbsp;<\/li>\n\n\n\n<li><em>member functions<\/em> <strong>set<\/strong> and <strong>print<\/strong><\/li>\n\n\n\n<li><em>Access specifiers<\/em> <strong>private<\/strong> and <strong>public<\/strong><\/li>\n<\/ul>\n\n\n\n<p><strong>NOTE:<\/strong> A <em>class declaration<\/em> is typically <em>placed in a header file<\/em>.<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>And here\u2019s its corresponding <em>class definition<\/em>:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ complex.cpp\n#include &lt;iostream&gt;\n#include &quot;complex.h&quot;\t\/\/ Note!\n\nvoid Complex::set(const double r, \n                  const double i)    \n{ \n  real = r; \n  imag = i; \n}    \n\nvoid Complex::print()\n{\n  std::cout &lt;&lt; '(' &lt;&lt; real\n            &lt;&lt; ',' &lt;&lt; imag &lt;&lt; ')'\n            &lt;&lt; std::endl;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ complex.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;complex.h&quot;<\/span><span style=\"color: #0000FF\">\t<\/span><span style=\"color: #008000\">\/\/ Note!<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">set<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\">, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                  <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\">)    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{ <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  real = r; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  imag = i; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}    <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&#39;(&#39;<\/span><span style=\"color: #000000\"> &lt;&lt; real<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">            &lt;&lt; <\/span><span style=\"color: #A31515\">&#39;,&#39;<\/span><span style=\"color: #000000\"> &lt;&lt; imag &lt;&lt; <\/span><span style=\"color: #A31515\">&#39;)&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">            &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>It <em>must <\/em><strong><em>#include<\/em><\/strong><em> the class header file<\/em>.<\/li>\n\n\n\n<li>It contains the <em>implementation<\/em> of the <em>class member functions<\/em>.<\/li>\n<\/ul>\n\n\n\n<p><strong>NOTE:<\/strong>&nbsp; A class definition is typically <em>placed in one or more <\/em><strong><em>.cpp<\/em><\/strong><em> files<\/em>.<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>Here\u2019s how the class might be used:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ testComplex.cpp\n#include &lt;iostream&gt;\n#include &quot;complex.h&quot;\t\/\/ Note!\n\nint main() \n{ \n  Complex a;\n\n  a.set(27.5, 13.4); \n  a.print();\n\n  return 0; \n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ testComplex.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;complex.h&quot;<\/span><span style=\"color: #0000FF\">\t<\/span><span style=\"color: #008000\">\/\/ Note!<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">() <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{ <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Complex a;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">a<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">set<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">27.5<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">13.4<\/span><span style=\"color: #000000\">); <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">a<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li><strong>NOTE:<\/strong>\n<ul class=\"wp-block-list\">\n<li>It must <strong>#include<\/strong> the class header file.<\/li>\n\n\n\n<li>In this case, the <strong>Complex a;<\/strong> produces a <em>local, auto instance<\/em> of class <strong>Complex<\/strong>.<\/li>\n\n\n\n<li>In this case, it calls both of the member functions of class <strong>Complex<\/strong>, <strong>set<\/strong> and <strong>print<\/strong>, applied to <strong>a<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>It outputs:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>(27.5,13.4)<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"class_versus_struct\"><\/span>class versus struct<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A <strong>class<\/strong> is similar to a C <strong>struct<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It has a similar syntax.<\/li>\n\n\n\n<li>It can contain data members.<\/li>\n\n\n\n<li>It is an extension of the same concept.<\/li>\n<\/ul>\n\n\n\n<p>A <strong>class<\/strong> differs from a C <strong>struct<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It uses the keyword class.<\/li>\n\n\n\n<li>It can contain member functions as well as data members.<\/li>\n\n\n\n<li>It can contain access specifiers (<strong>public<\/strong> and <strong>private<\/strong>).<\/li>\n\n\n\n<li>Its <em>usage<\/em> does not require the keyword <strong>struct<\/strong> (or <strong>class<\/strong>).<\/li>\n<\/ul>\n\n\n\n<p>In C++, a <strong>class<\/strong> and a <strong>struct<\/strong> are both extensions of a C <strong>struct<\/strong>;&nbsp; they differ only in one respect (see later).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"inline_Member_Functions\"><\/span>inline Member Functions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>When you create many member functions which contain very little code, and call them often (a common occurrence in object-oriented programming), run-time efficiency often becomes an issue.<\/p>\n\n\n\n<p>In C, the only solution was to use a <em>macro<\/em>, which has some potentially serious side-effects.<\/p>\n\n\n\n<p>In C++, you have a much better alternative:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can use <strong>inline<\/strong> member functions.<\/li>\n<\/ul>\n\n\n\n<p>There are two ways to specify an <strong>inline<\/strong> member function:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Place the function body inside the class declaration,\n<ul class=\"wp-block-list\">\n<li>This combines (part of) the class definition with the class declaration, which means you are exposing implementation details of the class to your clients. (It violates encapsulation)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>or:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Define the function explicitly <strong>inline<\/strong> in the class header file.\n<ul class=\"wp-block-list\">\n<li>This still has some encapsulation issues.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Placing_the_function_body_inside_the_class_declaration\"><\/span>Placing the function body inside the class declaration:<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ complex.h\nclass Complex \n{ \nprivate:    \n  double real, imag; \npublic:    \n  \/\/ sets values\n  void set(const double r, \n           const double i)\n  { \n    real = r; \n    imag = i; \n\t}\n  void print();\t\/\/ prints values\n}; \/\/ end of class declaration (semicolon required)\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ complex.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{ <\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real, imag; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ sets values<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">set<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\">, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">           <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  { <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    real = r; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    imag = i; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t}<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><span style=\"color: #008000\">\t\/\/ prints values<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><span style=\"color: #008000\"> \/\/ end of class declaration (semicolon required)<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>This <em>implicitly<\/em> makes the member function <strong>inline<\/strong>.<\/p>\n<\/div>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Defining_the_function_explicitly_inline_in_the_class_header_file\"><\/span>Defining the function <em>explicitly<\/em> <strong>inline<\/strong> in the class header file:<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ complex.h\nclass Complex \n{ \nprivate:    \n  double real, imag; \npublic:    \n  void set(const double r, \n           const double i); \/\/ sets values\n  void print();\t\/\/ prints values\n}; \/\/ end of class declaration (semicolon required)\n\ninline void Complex::set(const double r, \n                         const double i)\n{ \n\treal = r; \n\timag = i; \n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ complex.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{ <\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real, imag; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">set<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\">, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">           <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\">);<\/span><span style=\"color: #008000\"> \/\/ sets values<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><span style=\"color: #008000\">\t\/\/ prints values<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><span style=\"color: #008000\"> \/\/ end of class declaration (semicolon required)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">inline<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">set<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\">, <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">                         <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">{ <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">\treal = r; <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">\timag = i; <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong>Question:<\/strong> <em>Why does it have to be in the header file?<\/em><\/p>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"To_inline_or_Not_To_inline\"><\/span>To inline or Not To inline?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>Beware!<\/strong> Making a function <strong>inline<\/strong> <em>exposes that part of the class implementation to its clients, which can lead to problems.<\/em>&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p>When should one make a member function <strong>inline<\/strong>?<\/p>\n\n\n\n<p>when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The member function is <em>truly trivial<\/em>, and <em>is likely to remain so<\/em>.<\/li>\n<\/ul>\n\n\n\n<p><em>and<\/em> when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Making the member function <strong>inline<\/strong> is <em>not detrimental in other ways<\/em>.<\/li>\n<\/ul>\n\n\n\n<p>For example, we could have done the following:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ complex.h\n#include &lt;iostream&gt;\t\/\/ Large!!\n\nclass Complex \n{ \nprivate:    \n  double real, imag; \npublic:    \n  void set(const double r, \n           const double i)\n  { real = r; imag = i; }. \/\/ sets values\n  void print()\t\t         \/\/ prints values\n  {\n    std::cout &lt;&lt; \u2018(\u2018 &lt;&lt; real \n              &lt;&lt; \u2018.\u2019 &lt;&lt; imag &lt;&lt; \u2018)\u2019\n              &lt;&lt; std::endl;\n  }\n}; \/\/ end of class declaration (semicolon required)\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ complex.h<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><span style=\"color: #0000FF\">\t<\/span><span style=\"color: #008000\">\/\/ Large!!<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{ <\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real, imag; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">set<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\">, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">           <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  { real = r; imag = i; }.<\/span><span style=\"color: #008000\"> \/\/ sets values<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">()<\/span><span style=\"color: #008000\">\t\t         \/\/ prints values<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; \u2018(\u2018 &lt;&lt; real <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">              &lt;&lt; \u2018.\u2019 &lt;&lt; imag &lt;&lt; \u2018)\u2019<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">              &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><span style=\"color: #008000\"> \/\/ end of class declaration (semicolon required)<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>But then, all clients would be forced to include &lt;iostream&gt;, which slows down compilation considerably!&nbsp; Besides, in this case, the savings won\u2019t be great.<\/p>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Class_Scope\"><\/span>Class Scope<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C has the following scopes:\n<ul class=\"wp-block-list\">\n<li><em>local<\/em> (or <em>block<\/em>) scope<\/li>\n\n\n\n<li><em>function<\/em> scope (labels only)<\/li>\n\n\n\n<li><em>global<\/em> (or <em>file<\/em>) scope<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>To these, C++ adds:\n<ul class=\"wp-block-list\">\n<li><em>class<\/em> scope\n<ul class=\"wp-block-list\">\n<li>The name of a class member is local to its class<\/li>\n\n\n\n<li>Access to class members is controlled<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><em>namespace<\/em> scope<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Class_Scope_and_Access_Specifiers\"><\/span>Class Scope and Access Specifiers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <em>member functions<\/em> of a class have unrestricted access to all the class members of that class.<\/li>\n\n\n\n<li>Functions that are <em>not member functions of that class<\/em> have access <em>only to the public members of that class<\/em>.&nbsp;<\/li>\n\n\n\n<li><strong>private<\/strong> <em>disallows access to a class member from outside the class<\/em>.<\/li>\n\n\n\n<li><em>Any number<\/em> of access specifiers is allowed, and <em>no particular order<\/em> is required.\n<ul class=\"wp-block-list\">\n<li>However, note that this may affect the order in which the members are allocated in memory.&nbsp;<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>The <em>default access<\/em>\n<ul class=\"wp-block-list\">\n<li>in a <em>class<\/em> is <strong>private<\/strong>&nbsp;&nbsp;&nbsp;<\/li>\n\n\n\n<li>in a <em>struct<\/em> is <strong>public<\/strong><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This is the only difference between a <strong>class<\/strong> and a <strong>struct<\/strong> in C++.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Class_versus_Struct\"><\/span>Class versus Struct<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A C-style <strong>struct<\/strong> is, in C++, a <strong>class<\/strong> with all <strong>public<\/strong> members.<\/p>\n\n\n\n<p>For example, you could do:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"#include &lt;iostream.h&gt;\nstruct Complex \n{    \n\tdouble real, imag;    \n\tvoid set(const double r, \n           const double i); \n\tvoid print();\n};\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream.h&gt;<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">struct<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real, imag;    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">set<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\">, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">           <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\">); <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p><strong><em>but don\u2019t!&nbsp;&nbsp;&nbsp;<\/em><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use a <strong>class<\/strong> <em>when there are member functions<\/em>, and&nbsp;<\/li>\n\n\n\n<li>Use a <strong>struct<\/strong> <em>when there are only data members<\/em>.&nbsp;&nbsp;<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Class_Constructors\"><\/span>Class Constructors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A class can be told how to create instances of itself.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ complex.h\nclass Complex\n{\npublic:\n  Complex(const double r,\n          const double i)\n  { real = r; imag = i; }\n\n  void print();\n\nprivate:\n  double real, imag;\n};\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ complex.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\">,<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">          <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  { real = r; imag = i; }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real, imag;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li><strong><em>A constructor is a special member function that is called whenever an instance of its class is created:<\/em><\/strong><\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ complex.h\nclass Complex\n{\nprivate:\n  double real, imag;\npublic:\n  Complex()\n  { real = 0.0; imag = 0.0; }\n\n  Complex(const double r)\n  { real = r; imag = 0.0; }\n\n  Complex(const double r, \n          const double i)\n  { real = r; imag = i; }\n\n  void print();\n};\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ complex.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real, imag;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  { real = <\/span><span style=\"color: #098658\">0.0<\/span><span style=\"color: #000000\">; imag = <\/span><span style=\"color: #098658\">0.0<\/span><span style=\"color: #000000\">; }<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  { real = r; imag = <\/span><span style=\"color: #098658\">0.0<\/span><span style=\"color: #000000\">; }<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\">, <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">          <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  { real = r; imag = i; }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>A <em>constructor<\/em> always <em>has the same name as its class<\/em>.<\/li>\n\n\n\n<li>It may <em>not<\/em> have a <em>return type<\/em>, <em>nor may it return a value<\/em>.<\/li>\n\n\n\n<li><em>Constructors<\/em> can be (and usually are) overloaded (see example code)<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Constructor_Usage\"><\/span>Constructor Usage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A constructor is <em>guaranteed to be called for the creation of an instance of its class<\/em> (even a static instance!)<\/p>\n\n\n\n<p>Note that we have replaced the <strong>set<\/strong> method with a constructor.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>More appropriate in C++, because of the above guarantee.<\/li>\n\n\n\n<li>Of course, we could have both, if we wish.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Explicit_Constructor_Usage\"><\/span>Explicit Constructor Usage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here are some examples of explicit constructor usage:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ testComplex.cpp\n#include &quot;complex.h&quot;\n\nstatic Complex g(54.6);\n\nint main()\n{\n  Complex x;\n  Complex y(34.5);\n  Complex z(27.5, 13.4);\n\n  g.print();\n  x.print();\n  y.print();\n  z.print();\n\n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ testComplex.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;complex.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">static<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">g<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">54.6<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  Complex x;<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  Complex <\/span><span style=\"color: #795E26\">y<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">34.5<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  Complex <\/span><span style=\"color: #795E26\">z<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">27.5<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">13.4<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">g<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">x<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">y<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">z<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Constructors_and_Default_Arguments\"><\/span>Constructors and Default Arguments<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can reduce the proliferation of constructors by using default arguments:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ complex.h\nclass Complex\n{\npublic:\n  Complex(const double r = 0.0, \n         const double i = 0.0)\n  { real = r; imag = i; }\n\n  void print();\n\nprivate:\n  double real, imag;\n};\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ complex.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\"> = <\/span><span style=\"color: #098658\">0.0<\/span><span style=\"color: #000000\">, <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">         <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\"> = <\/span><span style=\"color: #098658\">0.0<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  { real = r; imag = i; }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real, imag;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Default_Constructors\"><\/span>Default Constructors<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A <em>default constructor<\/em> is one that can be called without an argument.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Note<\/strong>: A default constructor may or may not have zero arguments.&nbsp; It depends on whether its declaration contains default arguments.<\/li>\n\n\n\n<li>For this reason, using the term <em>no-arg constructor<\/em> is <em>incorrect<\/em>.<\/li>\n<\/ul>\n\n\n\n<p>Here\u2019s how a default constructor can be used:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ testComplex.cpp\n#include &quot;complex.h&quot;\n\nstatic Complex g; \t\/\/ Calls default constructor\n\nint main()\n{\n  Complex x; \t\t\/\/ Calls default constructor\n  Complex a[10];\t\/\/ Calls default constructor 10 times\n\n\/\/ ...\n\nreturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ testComplex.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;complex.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #0000FF\">static<\/span><span style=\"color: #000000\"> Complex g;<\/span><span style=\"color: #008000\"> \t\/\/ Calls default constructor<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  Complex x;<\/span><span style=\"color: #008000\"> \t\t\/\/ Calls default constructor<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  Complex <\/span><span style=\"color: #001080\">a<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">10<\/span><span style=\"color: #000000\">];<\/span><span style=\"color: #008000\">\t\/\/ Calls default constructor 10 times<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ ...<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Generated_Default_Constructors\"><\/span>Generated Default Constructors<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><em>If you declare no constructors<\/em> for a class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The C++ compiler will <em>automatically generate<\/em> a <em>default constructor<\/em> for that class.<\/li>\n<\/ul>\n\n\n\n<p>If you declare <em>any<\/em> constructors for a class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The C++ compiler will <em>not<\/em> generate a <em>default constructor<\/em> for that class.<\/li>\n\n\n\n<li>In that case, if you want a default constructor for that class, you <em>must declare (and write) it explicitly<\/em>.<\/li>\n\n\n\n<li>You will encounter classes which will require you to&nbsp;write default constructors explicitly.<\/li>\n<\/ul>\n\n\n\n<p>For example:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:60%\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ complex.h\nclass Complex\n{\npublic:\n  Complex(const double r, \n          const double i)\n  { real = r; imag = i; }\n\n  void print();\n\nprivate:\n  double real, imag;\n};\n\n\/\/ testComplex.cpp\n#include &quot;complex.h&quot;\n\nint main()\n{\n  Complex x;  \/\/ error C2512: \n              \/\/ 'Complex' : no appropriate default constructor available\n  \/\/ ...\n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ complex.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\">, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">          <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  { real = r; imag = i; }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real, imag;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ testComplex.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;complex.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  Complex x;<\/span><span style=\"color: #008000\">  \/\/ error C2512: <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #008000\">              \/\/ &#39;Complex&#39; : no appropriate default constructor available<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:40%\"><\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Constructor_Usage-2\"><\/span>Constructor Usage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can explicitly initialize elements of an array of class instances using constructors:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ testComplex.cpp\n#include &quot;complex.h&quot;\n\nint main()\n{\n  Complex x[5] =\n  {\n    Complex(), \n    Complex(2.5),\n    Complex(2.3, 4.5)\n  };\n\n  \/\/ ...\n\n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ testComplex.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;complex.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Complex <\/span><span style=\"color: #001080\">x<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\">] =<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(), <\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">2.5<\/span><span style=\"color: #000000\">),<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">2.3<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">4.5<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  };<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ ...<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<p>If the initializer list does not contain an entry for every element, then the <em>default constructor<\/em> is used for those that are missing.<\/p>\n\n\n\n<p>If there is no default constructor, then <em>every element must be explicitly initialized<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Type_Conversions\"><\/span>Type Conversions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Consider:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>Complex x(5.4);<\/code><\/pre>\n\n\n\n<p>This amounts to <em>type conversion<\/em> from <strong>double<\/strong> to <strong>Complex<\/strong>.<\/p>\n\n\n\n<p>In general, it would be very convenient if we could accomplish implicit type conversions among instances of different classes.<\/p>\n\n\n\n<p>One common way to accomplish type conversion is to use a <em>constructor with a single argument<\/em> (or one where all arguments but the first are defaulted):<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ testComplex.cpp\n#include &quot;complex.h&quot;\n\nextern void bungle(Complex c);\n\nint main()\n{\n  Complex x(1.0);\t\t\t\/\/ explicit conversion\n\n  bungle( Complex(37.5) );\t\/\/ explicit conversion\n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ testComplex.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;complex.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">extern<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">bungle<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">c<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Complex <\/span><span style=\"color: #795E26\">x<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">1.0<\/span><span style=\"color: #000000\">);<\/span><span style=\"color: #008000\">\t\t\t\/\/ explicit conversion<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">bungle<\/span><span style=\"color: #000000\">( <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">37.5<\/span><span style=\"color: #000000\">) );<\/span><span style=\"color: #008000\">\t\/\/ explicit conversion<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Implicit_Type_Conversions\"><\/span>Implicit Type Conversions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><em>Implicit type conversions<\/em> occur in situations where you may <em>or may not<\/em> want it!<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ testComplex.cpp\n#include &quot;complex.h&quot;\n\nextern void blather(Complex c); \/\/ accepts a Complex\n\nComplex doit() \t\/\/ returns a Complex\n{\n  blather(2.3);\t\/\/ implicit conversion \n  \/\/ Equivalent to blather(Complex(2.3));\n\n  \/\/ ...\n\n  return 3.5;\t\/\/ implicit conversion\n  \/\/ Equivalent to return Complex(3.5);\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ testComplex.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;complex.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">extern<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">blather<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">c<\/span><span style=\"color: #000000\">);<\/span><span style=\"color: #008000\"> \/\/ accepts a Complex<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">doit<\/span><span style=\"color: #000000\">()<\/span><span style=\"color: #008000\"> \t\/\/ returns a Complex<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">blather<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">2.3<\/span><span style=\"color: #000000\">);<\/span><span style=\"color: #008000\">\t\/\/ implicit conversion <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Equivalent to blather(Complex(2.3));<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ ...<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">3.5<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #008000\">\t\/\/ implicit conversion<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Equivalent to return Complex(3.5);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li><strong><em>Be extremely wary of using conversion constructors when you don\u2019t want this to happen!&nbsp;&nbsp;<\/em><\/strong><\/li>\n\n\n\n<li><strong>This is easy to forget, so don&#8217;t say you haven&#8217;t been warned!<\/strong><\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Copy_Constructors\"><\/span>Copy Constructors<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Consider:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>Complex x = 1.0; \/\/ initialization, not assignment!<\/code><\/pre>\n\n\n\n<p>Because of implicit conversions, this can be equivalent to:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>Complex x = Complex(1.0);<\/code><\/pre>\n\n\n\n<p>but then it is often (but not always!) optimized to:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>Complex x(1.0);<\/code><\/pre>\n\n\n\n<p>A <em>copy constructor<\/em> is one that constructs an instance of a class <em>from another instance of that class<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>Complex(const Complex &amp;src); (preferred) <\/code><\/pre>\n\n\n\n<p>or:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>Complex(Complex &amp;src);\n<\/code><\/pre>\n\n\n\n<p><em>Note the reference <\/em>(<strong>&amp;<\/strong>)<em>, which is required<\/em>&nbsp; <\/p>\n\n\n\n<p>The following:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>Complex(const Complex src);<\/code><\/pre>\n\n\n\n<p><em>is <\/em><strong><em>not<\/em><\/strong><em> a copy constructor!<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Generated_Copy_Constructors\"><\/span>Generated Copy Constructors<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><em>If you do not declare<\/em> a copy constructor for a class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The C++ compiler will <em>generate<\/em> a copy constructor for that class.\n<ul class=\"wp-block-list\">\n<li>This is sometimes called a <em>default copy constructor<\/em>, although the term can be confused with the simple default constructor.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>For a simple class, the generated copy constructor will do <em>memberwise initialization<\/em> &#8212; i.e., a <em>\u2018shallow copy\u2019<\/em>.\n<ul class=\"wp-block-list\">\n<li><em>This may, or may not, be what you want for the class.<\/em><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Remember, if you declare no constructors for the class, the compiler will also generate a default constructor for the class.<\/li>\n<\/ul>\n\n\n\n<p><em>If you do declare<\/em> a copy constructor for a class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The C++ compiler will <em>not generate<\/em> one for that class.<\/li>\n\n\n\n<li>Neither will it generate a default constructor for the class.<br>(Because you declared at least one constructor for the class.)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Explicit_Copy_Constructors\"><\/span>Explicit Copy Constructors<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>For example, here\u2019s an explicit copy constructor:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ complex.h\nclass Complex\n{\npublic:\n  Complex(const double r = 0.0,\n          const double i = 0.0)\n  { real = r; imag = i; }\n\n  Complex(const Complex &amp;src)\n  { real = src.real; imag = src.imag; }\n\n  void print();\n\nprivate:\n  double real, imag;\n};\n\n\/\/ testComplex.cpp\n#include &quot;complex.h&quot;\nint main()\n{\n  Complex x(1.0, 4.5);\n  Complex y(x);\t\/\/ Copy constructor\n  \/\/ ...\t\n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ complex.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\"> = <\/span><span style=\"color: #098658\">0.0<\/span><span style=\"color: #000000\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">          <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\"> = <\/span><span style=\"color: #098658\">0.0<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  { real = r; imag = i; }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">src<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  { real = <\/span><span style=\"color: #001080\">src<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">real<\/span><span style=\"color: #000000\">; imag = <\/span><span style=\"color: #001080\">src<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">imag<\/span><span style=\"color: #000000\">; }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real, imag;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ testComplex.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;complex.h&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Complex <\/span><span style=\"color: #795E26\">x<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">1.0<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">4.5<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  Complex <\/span><span style=\"color: #795E26\">y<\/span><span style=\"color: #000000\">(x);<\/span><span style=\"color: #008000\">\t\/\/ Copy constructor<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ ...\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Explicit_or_Generated_Copy_Constructors\"><\/span>Explicit or Generated Copy Constructors?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In some cases, the generated copy constructor is sufficient.<\/p>\n\n\n\n<p>In other cases it is insufficient:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When a class contains a pointer to a non-shared object.<\/li>\n\n\n\n<li>Other situations where it does not make sense to replicate only the contents of the class and not any external context.<\/li>\n<\/ul>\n\n\n\n<p>In some other cases, copying a class is meaningless.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For example, does it make sense to copy a linked list?&nbsp; What does it mean?<\/li>\n\n\n\n<li>In this case, you need to ensure that no copy constructor exists.&nbsp; (How?&nbsp; Later&#8230;)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Class_Destructors\"><\/span>Class Destructors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A class can be told how to destroy an instance of itself.<\/p>\n\n\n\n<p>A <em>destructor<\/em> is a special member function that is called whenever an instance of its class is destroyed:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ complex.h\nclass Complex\n{\npublic:\n  Complex(const double r = 0.0,\n          const double i = 0.0)\n  { real = r; imag = i; }\n\n  ~Complex() {} \/\/ Destructor\n\n  void print();\n\nprivate:\n  double real, imag;\n};\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ complex.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">r<\/span><span style=\"color: #000000\"> = <\/span><span style=\"color: #098658\">0.0<\/span><span style=\"color: #000000\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">          <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\"> = <\/span><span style=\"color: #098658\">0.0<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  { real = r; imag = i; }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">~Complex<\/span><span style=\"color: #000000\">() {}<\/span><span style=\"color: #008000\"> \/\/ Destructor<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">print<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real, imag;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<p>A <em>destructor<\/em> always has <em>the same name as its class, preceded by a tilde (~) character<\/em>.<\/p>\n\n\n\n<p>It may <em>not<\/em> have a <em>return type<\/em>, <em>nor may it return a value<\/em>.<\/p>\n\n\n\n<p>A destructor <em>always takes no arguments<\/em>, so:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It cannot be overloaded.<\/li>\n\n\n\n<li>A class may only have a single destructor.<\/li>\n<\/ul>\n\n\n\n<p>Destructors are invoked implicitly;&nbsp; it is rarely necessary to make an explicit call to a destructor (bad form!).&nbsp;<\/p>\n\n\n\n<p>A destructor is <em>guaranteed to be called for the destruction of an instance of its class<\/em> (even a static instance!), except for free store instances (more later&#8230;).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Implicit_Destructor_Usage\"><\/span>Implicit Destructor Usage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Example:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ testComplex.cpp\n#include &quot;complex.h&quot;\n\nstatic Complex g(54.6);\n\nstatic void doit()\n{\n  Complex x;  \/\/ call constructor for x...\n  \/\/ ...\n}.            \/\/ call destructor for x...\n\nint main()\t\t\/\/ call constructor for g...\n{\n  doit();\n  \/\/ ...\n  return 0;\n}             \/\/ call destructor for g...\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ testComplex.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;complex.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">static<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Complex<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">g<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">54.6<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">static<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">doit<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Complex x;<\/span><span style=\"color: #008000\">  \/\/ call constructor for x...<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #008000\">  \/\/ ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}.<\/span><span style=\"color: #008000\">            \/\/ call destructor for x...<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><span style=\"color: #008000\">\t\t\/\/ call constructor for g...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">doit<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><span style=\"color: #008000\">             \/\/ call destructor for g...<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Generated_Destructors\"><\/span>Generated Destructors<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><em>If you declare no destructor<\/em> for a class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The C++ compiler will <em>automatically generate<\/em> a destructor for that class.<\/li>\n\n\n\n<li>For a simple class, the generated destructor will do essentially nothing.<\/li>\n\n\n\n<li>For derived classes, and classes with class member objects, the generated destructor does more! (Later&#8230;)<\/li>\n<\/ul>\n\n\n\n<p>You can, of course, write your own &#8212; <em>and you must for some classes<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_Does_a_Destructor_Do\"><\/span>What Does a Destructor Do?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In the case of class <strong>Complex<\/strong>, there is nothing to do.<\/p>\n\n\n\n<p>In other situations, a destructor can:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>Deallocate space<\/em> allocated by the instance.<\/li>\n\n\n\n<li><em>Close a file<\/em> associated with the instance.<\/li>\n\n\n\n<li><em>Remove a window<\/em> from the screen that the instance caused to be created.<\/li>\n\n\n\n<li><em>Defrangle a booblefratz<\/em>, that had been previously frangled by the instance.<\/li>\n<\/ul>\n\n\n\n<p>A <em>destructor<\/em> is the complement of a constructor;&nbsp; it typically cleans up after the instance.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"When_a_Destructor_is_NOT_Called\"><\/span>When a Destructor is NOT Called<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Note that <em>no destructor is called<\/em> in the following case:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ testComplex.cpp\n#include &quot;complex.h&quot;\n\nstatic void doit()\n{\n  Complex *ptr = new Complex(1.0);\t\n   \/\/ constructor called for what ptr will point to.\n   \n  \/\/ ...\n}\t\t\t\t\t\t\t\t\/\/ no destructor called!\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ testComplex.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;complex.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">static<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">doit<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Complex *ptr = <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">1.0<\/span><span style=\"color: #000000\">);\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">   \/\/ constructor called for what ptr will point to.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">   <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><span style=\"color: #008000\">\t\t\t\t\t\t\t\t\/\/ no destructor called!<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>If you don\u2019t clean it up with an explicit <strong>delete<\/strong>, it doesn\u2019t get cleaned up!<\/p>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Destructor_Usage\"><\/span>Destructor Usage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here\u2019s an example of where you need an explicit destructor:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ complex.h\n#include &lt;string.h&gt;\n\nclass MyString\n{\npublic:\n  MyString(const char source[])\n  {\n    p = new char[strlen(source) + 1];\n    strcpy(p, source);\n  }\n\n  ~MyString() \n  { \n    delete [] p; \n  }\n  \/\/ ... \n\nprivate:\n  char *p; \n};\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ complex.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;string.h&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">MyString<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">MyString<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">source<\/span><span style=\"color: #000000\">[])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    p = <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #795E26\">strlen<\/span><span style=\"color: #000000\">(source) + <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">];<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">strcpy<\/span><span style=\"color: #000000\">(p, source);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">~MyString<\/span><span style=\"color: #000000\">() <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  { <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">delete<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #AF00DB\">[]<\/span><span style=\"color: #000000\"> p; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ ... <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> *p; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>The constructor allocated an array which must be deallocated (else you will have a memory leak!).<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>Here\u2019s a more complex example &#8212; a <strong>FileStats<\/strong> class:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ FileStats.h\n#include &lt;stdio.h&gt;\t\t\/\/ For FILE\n\nclass FileStats\n{\npublic:\n  FileStats(char *fileName);\n  ~FileStats();\n  void collectStats();\n\nprivate:\n  \/\/ Methods\n  int readChar();\n\n  \/\/ Data\n  char *name;\t\t\t\/\/ File name\n  FILE *ifp;\t\t\t\/\/ File pointer\n\n    \/\/ Counts of characters, words, lines\n  typedef unsigned int count;\n  count \tchars;\n  count \twords;\n  count \tlines;\n};\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ FileStats.h<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;stdio.h&gt;<\/span><span style=\"color: #0000FF\">\t\t<\/span><span style=\"color: #008000\">\/\/ For FILE<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">FileStats<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">FileStats<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #001080\">fileName<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">~FileStats<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">collectStats<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Methods<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">readChar<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Data<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> *name;<\/span><span style=\"color: #008000\">\t\t\t\/\/ File name<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  FILE *ifp;<\/span><span style=\"color: #008000\">\t\t\t\/\/ File pointer<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">    \/\/ Counts of characters, words, lines<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">typedef<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">unsigned<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> count;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  count \tchars;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  count \twords;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  count \tlines;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ FileStats.cpp\n#include &lt;iostream&gt;\n#include &quot;FileStats.h&quot;\n\n\/\/ Constructor\nFileStats::FileStats(char *fileName)\n{\n  ifp = fopen(fileName, &quot;r&quot;); \/\/ Open for reading\n  if (ifp != NULL)\n  {\n    chars = words = lines = 0;\n    name = new char[strlen(fileName) + 1];\n    strcpy(name, fileName);\n  }\n  else\n  {\n    exit(1);\t\/\/ Exit abruptly\n  }\n}\n\n\/\/ Private utility member function\nint FileStats::readChar()\n{\n  int ch;\n  ch = fgetc(ifp);\n  if (ch != EOF)\n    chars++; \/\/ count chars\n  if (ch == '\\n')\n    lines++; \/\/ count lines\n  return ch;\n}\n\n\/\/ Collect File Statistics member function\nvoid FileStats::collectStats()\n{\n  bool in_word = false;\n  int ch;\n  while (EOF != (ch = readChar()))\n  {\n    if (!in_word)\n    {   \t \/\/ not already in word\n      if (isalnum(ch))\n      {  \t\/\/ start of new word\n        words++; \/\/ count words\n        in_word = true;\n      }\n  }\n  else\n  {   \t\/\/ already in a word\n    if (!isalnum(ch))\n    in_word = false; \n        \/\/ end of word\n    }\n  }\n}\n\n\/\/ Destructor\n\/\/ Cleans up and prints statistics\n\/\/\nFileStats::~FileStats()\n{\n  \/\/ We opened the file, so be sure to close it\n  if (ifp != NULL)\n    fclose(ifp);\n\n  \/\/ Output file statistics\n  std::cout &lt;&lt; &quot;File '&quot; &lt;&lt; name\n            &lt;&lt; &quot;' statistics:&quot; &lt;&lt; std::endl\n            &lt;&lt; &quot; chars : &quot; &lt;&lt; chars &lt;&lt; std::endl\n            &lt;&lt; &quot; words : &quot; &lt;&lt; words &lt;&lt; std::endl\n            &lt;&lt; &quot; lines : &quot; &lt;&lt; lines &lt;&lt; std::endl;\n\n  \/\/ Free up allocated space\n  delete [] name;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ FileStats.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;FileStats.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Constructor<\/span><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">FileStats<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">FileStats<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> *fileName)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ifp = <\/span><span style=\"color: #795E26\">fopen<\/span><span style=\"color: #000000\">(fileName, <\/span><span style=\"color: #A31515\">&quot;r&quot;<\/span><span style=\"color: #000000\">);<\/span><span style=\"color: #008000\"> \/\/ Open for reading<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (ifp != <\/span><span style=\"color: #0000FF\">NULL<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    chars = words = lines = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    name = <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #795E26\">strlen<\/span><span style=\"color: #000000\">(fileName) + <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">];<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">strcpy<\/span><span style=\"color: #000000\">(name, fileName);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">else<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">exit<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">);<\/span><span style=\"color: #008000\">\t\/\/ Exit abruptly<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Private utility member function<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">FileStats<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">readChar<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> ch;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  ch = <\/span><span style=\"color: #795E26\">fgetc<\/span><span style=\"color: #000000\">(ifp);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (ch != EOF)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    chars++;<\/span><span style=\"color: #008000\"> \/\/ count chars<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (ch == <\/span><span style=\"color: #A31515\">&#39;<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&#39;<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    lines++;<\/span><span style=\"color: #008000\"> \/\/ count lines<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> ch;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Collect File Statistics member function<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">FileStats<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">collectStats<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">bool<\/span><span style=\"color: #000000\"> in_word = <\/span><span style=\"color: #0000FF\">false<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> ch;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">while<\/span><span style=\"color: #000000\"> (EOF != (ch = <\/span><span style=\"color: #795E26\">readChar<\/span><span style=\"color: #000000\">()))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (!in_word)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    {<\/span><span style=\"color: #008000\">   \t \/\/ not already in word<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">      <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #795E26\">isalnum<\/span><span style=\"color: #000000\">(ch))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">      {<\/span><span style=\"color: #008000\">  \t\/\/ start of new word<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">        words++;<\/span><span style=\"color: #008000\"> \/\/ count words<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">        in_word = <\/span><span style=\"color: #0000FF\">true<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">      }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">else<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><span style=\"color: #008000\">   \t\/\/ already in a word<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (!<\/span><span style=\"color: #795E26\">isalnum<\/span><span style=\"color: #000000\">(ch))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    in_word = <\/span><span style=\"color: #0000FF\">false<\/span><span style=\"color: #000000\">; <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">        \/\/ end of word<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Destructor<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Cleans up and prints statistics<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">FileStats<\/span><span style=\"color: #000000\">::~<\/span><span style=\"color: #795E26\">FileStats<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ We opened the file, so be sure to close it<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (ifp != <\/span><span style=\"color: #0000FF\">NULL<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">fclose<\/span><span style=\"color: #000000\">(ifp);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Output file statistics<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;File &#39;&quot;<\/span><span style=\"color: #000000\"> &lt;&lt; name<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">            &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;&#39; statistics:&quot;<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">            &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; chars : &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; chars &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">            &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; words : &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; words &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">            &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; lines : &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; lines &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Free up allocated space<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">delete<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #AF00DB\">[]<\/span><span style=\"color: #000000\"> name;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>Note that the actual reporting is done in the destructor!<br>This is not necessarily good practice &#8212; it depends on the situation.<br>We&#8217;re doing it here to show that a destructor can potentially do a fair amount of work &#8212; destructors are not necessarily trivial.<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>And now to use this magical class&#8230;<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/ testFileStats.cpp\n#include &lt;iostream&gt;\n#include &quot;FileStats.h&quot;\n\nint main(int argc, const char *argv[])\n{\n  int retVal = 1;\n  if (argc &gt; 1)\n  {\n    FileStats fs(argv[1]);\n    fs.collectStats();\n    retVal = 0;\n  }\n  else\n  {\n    std::cerr &lt;&lt; &quot;No filename specified&quot; \n              &lt;&lt; std::endl;\n }\n  return retVal;\n}\t\t\t\t\t\/\/ FileStats destructor gets called here...\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/\/ testFileStats.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;FileStats.h&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argc<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #001080\">argv<\/span><span style=\"color: #000000\">[])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> retVal = <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (argc &gt; <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    FileStats <\/span><span style=\"color: #795E26\">fs<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">argv<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #001080\">fs<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">collectStats<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    retVal = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">else<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cerr &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;No filename specified&quot;<\/span><span style=\"color: #000000\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">              &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\"> }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> retVal;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><span style=\"color: #008000\">\t\t\t\t\t\/\/ FileStats destructor gets called here...<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><\/div>\n<\/div>\n\n\n\n<p>Try using this <strong>FileStats<\/strong> class (cut and paste it), and have it print out the statistics from some of your files!<\/p>\n\n\n\n<p>It will give you some practice with creating the various files that you typically use to implement a class and make it available to its clients.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"has-medium-font-size\" style=\"margin-top:0px;margin-bottom:0px\">We&#8217;ve talked about quite a few things relating to classes:<\/p>\n\n\n\n<ul style=\"padding-top:0px;padding-bottom:0px\" class=\"wp-block-list\">\n<li class=\"has-medium-font-size\">Class declarations and definitions<\/li>\n\n\n\n<li class=\"has-medium-font-size\">Class member functions<\/li>\n\n\n\n<li class=\"has-medium-font-size\">Class scope<\/li>\n\n\n\n<li class=\"has-medium-font-size\">Class constructors:\n<ul class=\"wp-block-list\">\n<li>Default constructors<\/li>\n\n\n\n<li>Copy constructors<\/li>\n\n\n\n<li>Conversion constructors<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li class=\"has-medium-font-size\">Destructors<\/li>\n<\/ul>\n\n\n\n<p class=\"has-medium-font-size\">But there&#8217;s lots more to learn about classes&#8230;<\/p>\n\n\n\n<div style=\"height:38px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button has-custom-font-size has-medium-font-size\"><a class=\"wp-block-button__link has-palette-color-8-color has-text-color has-link-color wp-element-button\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/conversions\/\">Next: Conversions<\/a><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>What Are Classes? Here are the basics of C++ Classes. In particular: Class Declarations Here\u2019s a simple class declaration for a complex number: It consists of: NOTE: A class declaration is typically placed in a header file. And here\u2019s its corresponding class definition: NOTE:&nbsp; A class definition is typically placed in one or more .cpp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1901,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-920","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/920","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/comments?post=920"}],"version-history":[{"count":15,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/920\/revisions"}],"predecessor-version":[{"id":1912,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/920\/revisions\/1912"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1901"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}