{"id":1575,"date":"2024-10-28T18:08:27","date_gmt":"2024-10-28T18:08:27","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=1575"},"modified":"2025-01-20T20:08:59","modified_gmt":"2025-01-20T20:08:59","slug":"assignment-1b-simple-c-features","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/assignments\/assignment-1\/assignment-1b-simple-c-features\/","title":{"rendered":"Assignment 1b: Simple C++ Features"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Simple_C_Features\"><\/span>Simple C++ Features<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>This part of the assignment is intended to illustrate some of the simpler C++ features, and to show some of the differences between C and C++.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_1\"><\/span>Step 1<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Write a\u00a0<em><strong>single<\/strong><\/em>\u00a0program to determine the\u00a0<code><strong>sizeof<\/strong><\/code>\u00a0each of the built-in types for C\/C++ (<code><strong>char, short, int, long, float, double, long double<\/strong><\/code>). <\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong><code>sizeof(long)\nsizeof(char)<\/code><\/strong><\/code><\/pre>\n\n\n\n<p>and also the sizes of&nbsp;<em>literals<\/em>&nbsp;(<strong><em>not variables<\/em><\/strong>) of those types:<\/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<figure class=\"wp-block-table dark-header is-style-stripes\"><table style=\"border-width:2px\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Type<\/th><th class=\"has-text-align-left\" data-align=\"left\"><strong>Example of literal of that type<\/strong><\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>char<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>'a'<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>short<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><em>&lt;There is no constant of type short&gt;<\/em><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>int<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>35<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>long<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>79L<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>float<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>1.3F<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>double<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\"><code>93.4<\/code><\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>long double<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">37.9L<\/td><\/tr><\/tbody><\/table><\/figure>\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>for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong><code>sizeof(1.3F)\nsizeof('a')<\/code><\/strong><\/code><\/pre>\n\n\n\n<p>In this latter case (of sizes of literals) it should not be necessary to use any variables for that part of the code.<\/p>\n\n\n\n<p><strong><u>Note:<\/u><\/strong>&nbsp;I don&#8217;t want to see two different programs.&nbsp; I want to see a&nbsp;<em>single program<\/em>, identical in all respects for both C and C++.&nbsp; The only difference is how you indicate to the compiler that it&#8217;s a C or a C++ program.<\/p>\n\n\n\n<p>Compile and run the program\u00a0<strong><em>using a C compiler, and then with a C++ compiler on the same platform<\/em><\/strong><em>.\u00a0<\/em>(If your C++ compiler is capable of acting as a plain C compiler, then use its plain C mode).<\/p>\n\n\n\n<p><strong>Hint:<\/strong>&nbsp;Many different C++&nbsp;compilers use a very simple device to determine whether a program is written in C or in C++:&nbsp; it looks at the <em>filetype<\/em>.&nbsp; If the program has a&nbsp;<code><strong>.c<\/strong><\/code>&nbsp;filetype, then it assumes it must be a C program;&nbsp; if the program has a&nbsp;<code><strong>.cpp<\/strong><\/code>&nbsp;filetype, then it assumes it must be C++.<\/p>\n\n\n\n<p><em>Unix systems<\/em>&nbsp;(most likely using the GNU C++ compiler), employ a similar convention.&nbsp; C programs usually have a&nbsp;<code><strong>.c<\/strong><\/code>&nbsp;filetype;&nbsp; C++ programs have a&nbsp;<code><strong>.C<\/strong><\/code>&nbsp;filetype (that is, an&nbsp;<em>uppercase C<\/em>&nbsp;&#8212; remember, everything on Unix is case-sensitive).&nbsp; However, in that environment, you may find that you may have to use different compilers for C and C++.<\/p>\n\n\n\n<p>In either situation, you will likely have to create a single&nbsp;<code><strong>.c<\/strong><\/code>&nbsp;file, and then, once you have it working satisfactorily, copy that file to one with a&nbsp;<code><strong>.cpp<\/strong><\/code>&nbsp;filetype (MS VC++) or a&nbsp;<code><strong>.C<\/strong><\/code>&nbsp;filetype (Unix), maintaining the same filename.&nbsp; Then use the appropriate compiler to re-compile it in C++ and determine the differences.<\/p>\n\n\n\n<p>What differences do you observe? Explain them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_2\"><\/span>Step 2<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here is a legal ANSI C program. Your task is to convert it to C++.<\/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-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=\"\/\/\n\/\/  friends.c\n\/\/  Assignment 1b\n\/\/\n\/\/  Created by Bryan Higgs on 10\/28\/24.\n\/\/\n\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nenum sex { UNKNOWN, MALE, FEMALE };\n\nstruct friend\n{\n  char     *name;\n  int       age;\n  enum sex  sex;\n};\n\nvoid print_friends(struct friend this[])\n{\n  static char *class[] = { &quot;?&quot;, &quot;M&quot;, &quot;F&quot; };\n  int i;\n\n  for (i = 0; this[i].name != 0; i++)\n  {\n    printf(&quot; %s, Age: %3d, %s\\n&quot;,\n    this[i].name,\n    this[i].age,\n    class[this[i].sex]);\n  }\n}\n\nint main(int argc, const char * argv[])\n{\n  struct friend friends[] =\n  {\n    { &quot;Mabel Smith&quot;, 23, 2 },\n    { &quot;Fred Jones&quot;, 42, 1 },\n    { 0, 0, 0 }\n  };\n  struct friend new[] =\n  {\n    { &quot;Clarence Brown&quot;, 25, 1 },\n    { &quot;Florence Tutwiler&quot;, 56, 2 },\n    { 0, 0, 0 }\n  };\n\n  printf(&quot;I have the following friends:\\n&quot;);\n  print_friends(friends);\n  printf(&quot;I just met the following new people:\\n&quot;);\n  print_friends(new);\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\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  friends.c<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Assignment 1b<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 10\/28\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/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>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;stdlib.h&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">enum<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">sex<\/span><span style=\"color: #000000\"> { <\/span><span style=\"color: #0070C1\">UNKNOWN<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0070C1\">MALE<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0070C1\">FEMALE<\/span><span style=\"color: #000000\"> };<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">struct<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">friend<\/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\">char<\/span><span style=\"color: #000000\">     *name;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">       age;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">enum<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">sex<\/span><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">sex<\/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: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">print_friends<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">struct<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">friend<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">this<\/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\">static<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> *<\/span><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\">[] = { <\/span><span style=\"color: #A31515\">&quot;?&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;M&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #A31515\">&quot;F&quot;<\/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\"> i;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (i = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">; <\/span><span style=\"color: #0000FF\">this<\/span><span style=\"color: #000000\">[i].<\/span><span style=\"color: #001080\">name<\/span><span style=\"color: #000000\"> != <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">; i++)<\/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\">printf<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot; <\/span><span style=\"color: #001080\">%s<\/span><span style=\"color: #A31515\">, Age: <\/span><span style=\"color: #001080\">%3d<\/span><span style=\"color: #A31515\">, <\/span><span style=\"color: #001080\">%s<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">this<\/span><span style=\"color: #000000\">[i].<\/span><span style=\"color: #001080\">name<\/span><span style=\"color: #000000\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">this<\/span><span style=\"color: #000000\">[i].<\/span><span style=\"color: #001080\">age<\/span><span style=\"color: #000000\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #001080\">class<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #0000FF\">this<\/span><span style=\"color: #000000\">[i].<\/span><span style=\"color: #001080\">sex<\/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>\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: #000000\"> <\/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\">struct<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">friend<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">friends<\/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: #A31515\">&quot;Mabel Smith&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">23<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\"> },<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    { <\/span><span style=\"color: #A31515\">&quot;Fred Jones&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">42<\/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: #098658\">0<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">0<\/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>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">struct<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">friend<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">new<\/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: #A31515\">&quot;Clarence Brown&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">25<\/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: #A31515\">&quot;Florence Tutwiler&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">56<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\"> },<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    { <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">0<\/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>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">printf<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;I have the following friends:<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">print_friends<\/span><span style=\"color: #000000\">(friends);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">printf<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;I just met the following new people:<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">print_friends<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #AF00DB\">new<\/span><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\"> <\/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<ol class=\"wp-block-list\">\n<li>First, compile and run it under plain C, check that it works, and observe what it outputs.<\/li>\n\n\n\n<li>Then copy the\u00a0<strong><code>friends.c<\/code><\/strong>\u00a0file to\u00a0<strong><code>friends.cpp<\/code><\/strong>\u00a0(or whatever is appropriate for your C++ compiler) and try to compile it using C++.<\/li>\n\n\n\n<li>Determine the cause of each problem you encounter, fix it,\u00a0<em>add a comment to the source code at that point, explaining what you did<\/em>, and try again.\u00a0<br><em>To maintain your sanity, I strongly suggest that you make only one change at a time, rather than a wholesale set of changes!<\/em><\/li>\n\n\n\n<li>Repeat the above step until done.<\/li>\n\n\n\n<li>Document each change you make, in your source code, and explain why you had to make that change. Include in the description part of your submission a description of the actions you took (including any blind alleys you went down) to fix the problems you encountered.\u00a0<\/li>\n\n\n\n<li>Eventually, you should be able to compile the program\u00a0<strong>with no errors, warnings,\u00a0<em>or informationals<\/em><\/strong>, and be able to run it to produce the exact same results as the original program.<\/li>\n\n\n\n<li>Document each change you make, in comments in your source code, and explain why you had to make that change. <br>Include in the description part of your submission a description of the actions you took (including any blind alleys you went down) to fix the problems you encountered.\u00a0<\/li>\n\n\n\n<li>Eventually, you should be able to compile the program\u00a0<strong>with no errors, warnings,\u00a0<em>or informationals<\/em><\/strong>, and be able to run it to produce the exact same results as the original program.<\/li>\n\n\n\n<li>Now convert the C-style I\/O to C++ stream I\/O, to produce\u00a0<em>exactly\u00a0<\/em>the same output.\u00a0<em><strong>Do not do this until you have completed all the other steps. I want to see a separate version with just the I\/O changes.<\/strong><\/em><\/li>\n<\/ol>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_3\"><\/span>Step 3<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The following C++ function uses parameters which are of type\u00a0<strong><em>pointer to char<\/em><\/strong>, and uses pointer dereferencing to implement a circular shift of three characters:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>\/* shift.cpp *\/\n#include \"shift.h\"\n\nvoid shift(char *a, char *b, char *c)\n{\n    char temp = *a;\n    *a = *b;\n    *b = *c;\n    *c = temp;\n}<\/code><\/pre>\n\n\n\n<p>The header file\u00a0<strong><code>shift.h<\/code><\/strong> contains the function prototype for the\u00a0<strong><code>shift()<\/code><\/strong> function:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>\/* shift.h *\/\n\nvoid shift(char *a, char *b, char *c);<\/code><\/pre>\n\n\n\n<p>Convert the shift function into one that uses\u00a0<strong><em>reference parameters<\/em><\/strong>\u00a0instead of pointers, and rewrite the\u00a0shift.h\u00a0file to match this new form of the function.<\/p>\n\n\n\n<p>To test that your function works correctly, write a\u00a0<strong><em>separately compiled<\/em><\/strong><em>\u00a0(that is, the main function will be in a separate .cpp file from the above function)<\/em>\u00a0C++ test program. It will print the before and after values of a shift.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_4\"><\/span>Step 4<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here are some C++ structure declarations for\u00a0<strong>Person<\/strong>, for\u00a0<strong>Fruit<\/strong>, and for\u00a0<strong>Building<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>\/* person.h *\/\n\nenum gender { UNKNOWN, MALE, FEMALE };\nstruct Person\n{\n    char    *name;\n    int      age;\n    gender   sex;\n};<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>\/* fruit.h *\/\n\nenum fruit_type { BANANA, APPLE };\nenum hue { BROWN, GREEN, YELLOW };\nstruct Fruit\n{\n    fruit_type type;\n    hue        color;\n    double     weight;\n};<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>\/* building.h *\/\n\nstruct Building\n{\n    char *name;\n    int   floors;\n    int   rooms;\n};<\/code><\/pre>\n\n\n\n<p>Write three C++ (non-member) functions, called\u00a0<strong><code>Print()<\/code><\/strong>, each of which accepts a parameter of type\u00a0<em>reference to<\/em>\u00a0<strong><code>Person<\/code><\/strong>, <strong><code>Fruit<\/code><\/strong>, or\u00a0<strong><code>Building<\/code><\/strong>, respectively. Each\u00a0<strong><code>Print()<\/code><\/strong> function will print out information about the contents of the structure passed to it.\u00a0<\/p>\n\n\n\n<p><strong><em>Ensure that all of the functions can coexist in the same compilation, and in the same program.<\/em><\/strong><\/p>\n\n\n\n<p>The\u00a0<strong><code>Person<\/code><\/strong>,\u00a0<strong><code>Fruit<\/code><\/strong>, and\u00a0<strong><code>Building<\/code><\/strong>\u00a0types are totally independent of each other.\u00a0<em>Therefore, each<\/em> <strong><code>Print()<\/code> <\/strong><em>function will be placed in its own separate source file, and\u00a0separately compiled<\/em>.\u00a0<em>For each type, there will be a header file that contains the structure declaration, and all other publicly visible interfaces (including the function prototype for the<\/em> <strong><code>Print()<\/code><\/strong> <em>function)<\/em>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>NOTE: Use the <strong><code>printf()<\/code><\/strong> function rather than C++ stream I\/O. You won&#8217;t have covered stream I\/O at this point, yet.<\/p>\n<\/blockquote>\n\n\n\n<p>[<strong>Hint<\/strong>: Use\u00a0<em>function name overloading<\/em> with\u00a0<em>regular functions<\/em>,\u00a0<em>not<\/em> member functions.]<\/p>\n\n\n\n<p>The following\u00a0<strong><code>main()<\/code><\/strong>\u00a0function should work correctly, when linked against your code:<\/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-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=\"\/\/\n\/\/  main.cpp\n\/\/  Assignment 1b\n\/\/\n\/\/  Created by Bryan Higgs on 10\/28\/24.\n\/\/\n\n#include &quot;person.h&quot;\n#include &quot;fruit.h&quot;\n#include &quot;building.h&quot;\n\nint main(int argc, const char * argv[])\n{\n  Person p = { &quot;Archie Bunker&quot;, 55, MALE };\n  Fruit f = { BANANA, YELLOW, 3.5 };\n  Building b = { &quot;World Trade Center&quot;, 110, 15000 };\n  Print(p);\n  Print(f);\n  Print(b);\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\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  main.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Assignment 1b<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 10\/28\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;person.h&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;fruit.h&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;building.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: #000000\"> <\/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\">  Person p = { <\/span><span style=\"color: #A31515\">&quot;Archie Bunker&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">55<\/span><span style=\"color: #000000\">, MALE };<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Fruit f = { BANANA, YELLOW, <\/span><span style=\"color: #098658\">3.5<\/span><span style=\"color: #000000\"> };<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Building b = { <\/span><span style=\"color: #A31515\">&quot;World Trade Center&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">110<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">15000<\/span><span style=\"color: #000000\"> };<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Print<\/span><span style=\"color: #000000\">(p);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Print<\/span><span style=\"color: #000000\">(f);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Print<\/span><span style=\"color: #000000\">(b);<\/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\">\n<p>It should output the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>Person:\n  Name:   Archie Bunker\n  Age:    55\n  Sex:    Male\nFruit:\n  Type:   Banana\n  Color:  Yellow\n  Weight: 3.5\nBuilding:\n  Name:   World Trade Center\n  Floors: 110\n  Rooms:  15000<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Simple C++ Features This part of the assignment is intended to illustrate some of the simpler C++ features, and to show some of the differences between C and C++. Step 1 Write a\u00a0single\u00a0program to determine the\u00a0sizeof\u00a0each of the built-in types for C\/C++ (char, short, int, long, float, double, long double). For example: and also the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":30,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1575","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1575","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=1575"}],"version-history":[{"count":4,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1575\/revisions"}],"predecessor-version":[{"id":1592,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1575\/revisions\/1592"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/30"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=1575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}