{"id":36,"date":"2024-08-01T18:35:48","date_gmt":"2024-08-01T18:35:48","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalC%2B%2BProgramming\/?page_id=36"},"modified":"2025-01-20T21:03:58","modified_gmt":"2025-01-20T21:03:58","slug":"assignment-6","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/assignments\/assignment-6\/","title":{"rendered":"Assignment 6"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Vector_A_Dynamic_Array_Class\"><\/span><strong>Vector<\/strong>: A Dynamic Array Class<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The standard Java class library contains a useful class called&nbsp;<strong><code>Vector<\/code><\/strong>. It is actually a kind of dynamic array. <\/p>\n\n\n\n<p>This assignment involves writing a C++ class, called&nbsp;<strong><code>DynArray<\/code><\/strong>, that emulates a subset of the Java&nbsp;<strong><code>Vector<\/code><\/strong>&nbsp;class (<a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Vector.html\" target=\"_blank\" rel=\"noreferrer noopener\">see here<\/a>). In addition, it involves writing other, related, classes, and is a useful exercise in the use of class inheritance and virtual functions.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Concepts\"><\/span><em>Concepts<\/em><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>To implement such a dynamic class, it is necessary to use arrays of pointers, and to use new and delete to allocate and de-allocate memory for these arrays of pointers. Perhaps a picture would help:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"547\" height=\"311\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/wp-content\/uploads\/2024\/11\/Vector.gif\" alt=\"\" class=\"wp-image-1612\"\/><\/figure>\n\n\n\n<p>Here, the first five elements of the array (on the left) point to elements, while the remainder are empty (i.e., are <strong><code>NULL<\/code><\/strong> pointers). The topmost element is at index 0, the next at index 1, etc.<\/p>\n\n\n\n<p>When an element is &#8216;added&#8217; to the array, a pointer to that element is entered into the next available pointer slot. If there is no space left in the pointer array, then the array is automatically expanded by&nbsp;<strong><code>capacityIncrement<\/code><\/strong>&nbsp;elements to make space. This expansion is accomplished by:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Determining how large the pointer array needs to become, and performing a new to allocate that amount of space for the array (<strong>not<\/strong>&nbsp;what the array elements point to),<\/li>\n\n\n\n<li>Copying the entries of the existing pointer array into the new pointer array, and zeroing out the remaining entries in the new pointer array,<\/li>\n\n\n\n<li>Deleting the old pointer array.<\/li>\n\n\n\n<li>Updating all the necessary internal data in the class to reflect the newly expanded array.<\/li>\n<\/ol>\n\n\n\n<p>When an element is &#8216;removed&#8217; from the array, the corresponding array pointer entry is zeroed, and then all the array pointer entries above it are moved down one, to fill in the gap (which means that their index values also change). All internal data in the class is updated to reflect the new state of the array.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Strategy\"><\/span><em>Strategy<\/em><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Our&nbsp;<strong><code>DynArray<\/code><\/strong>&nbsp;is an array of &#8230; what? On the one hand, we&#8217;d like to implement a dynamic array that would be generally useful &#8212; an array of&nbsp;<strong><code>Widget<\/code><\/strong>s, an array of&nbsp;<strong><code>Person<\/code><\/strong>s, an array of&nbsp;<strong><code>Vehicle<\/code><\/strong>s, &#8212; whatever. On the other hand, how do we do this without writing a new kind of array for each type of element we might want to store into it?<\/p>\n\n\n\n<p>We can solve this problem by using inheritance; we will implement a&nbsp;<strong><code>DynArray<\/code><\/strong>&nbsp;class that can store instances of any class derived from an abstract base class called&nbsp;<strong>DynArrayElement<\/strong>.<\/p>\n\n\n\n<p>So far, so good. But think about the problem some more. Wouldn&#8217;t it be a good idea for an instance of each class derived from <strong><code>DynArrayElement<\/code><\/strong> to be informed when it is being added to a <strong><code>DynArray<\/code><\/strong>? and when it is being removed from a&nbsp;<strong><code>DynArray<\/code><\/strong>? For one thing, it would be nice if an element could determine whether it currently is in a&nbsp;<strong><code>DynArray<\/code><\/strong>, before it silently disappears as a result of being destroyed. <\/p>\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\">\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(1 * 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=\"DynArray myArray(100, 10);    \/\/ initialCapacity 100,\n\t                            \/\/ capacityIncrement 10\nMyElement *myElement = new MyElement;\n\nmyArray.AddElement(*myElement);\n\ndelete myElement;\t\/\/ What happens to the array pointer?\" 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: #267F99\">DynArray<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">myArray<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">100<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">10<\/span><span style=\"color: #000000\">);<\/span><span style=\"color: #008000\">    \/\/ initialCapacity 100,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\t                            \/\/ capacityIncrement 10<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">MyElement *myElement = <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> MyElement;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #001080\">myArray<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(*myElement);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">delete<\/span><span style=\"color: #000000\"> myElement;<\/span><span style=\"color: #008000\">\t\/\/ What happens to the array pointer?<\/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>Clearly, we would like to catch this error as early as possible. We can add code to the element&#8217;s destructor to make us aware of this situation, if the element is in a&nbsp;<strong><code>DynArray<\/code><\/strong>.<\/p>\n\n\n\n<p>How can the element tell that it&#8217;s stored in a&nbsp;<strong><code>DynArray<\/code><\/strong>? Actually, the element may be in several&nbsp;<strong><code>DynArray<\/code><\/strong>s simultaneously, or even in the same&nbsp;<strong><code>DynArray<\/code><\/strong>&nbsp;more than once! So, it makes sense to keep a count of how many arrays each element is currently in; if the count is not zero at the time its destructor is called, it&#8217;s an ERROR!<\/p>\n<\/div>\n<\/div>\n\n\n\n<p>How might we accomplish this? Well, let&#8217;s require that every class derived from&nbsp;<strong><code>DynArrayElement<\/code><\/strong>&nbsp;must implement a standard set of virtual functions. We can do that by writing our&nbsp;<strong><code>DynArrayElement<\/code><\/strong>&nbsp;class to include them as pure virtual functions:<\/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(1 * 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=\"class DynArrayElement\n{\n  public:\n    virtual ~DynArrayElement();  \/\/ Why virtual?\n    \/\/ Virtual functions to be implemented\n    \/\/ by all derived classes\n    virtual void OnInsertion(DynArray &amp;array) = 0;\n    virtual void OnRemoval(DynArray &amp;array) = 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: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">DynArrayElement<\/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\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">virtual<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">~DynArrayElement<\/span><span style=\"color: #000000\">();<\/span><span style=\"color: #008000\">  \/\/ Why virtual?<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">    \/\/ Virtual functions to be implemented<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">    \/\/ by all derived classes<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">virtual<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">OnInsertion<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #267F99\">DynArray<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">array<\/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: #0000FF\">virtual<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">OnRemoval<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #267F99\">DynArray<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">array<\/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>Function&nbsp;<strong><code>OnInsertion()<\/code><\/strong>&nbsp;is called by the&nbsp;<strong><code>DynArray<\/code>&nbsp;<\/strong>code when it inserts an element into itself, and function&nbsp;<strong><code>OnRemoval()<\/code><\/strong>&nbsp;is called by the&nbsp;<strong><code>DynArray<\/code>&nbsp;<\/strong>code when it removes it from itself. In each case, the function provides a reference to the&nbsp;<strong><code>DynArray<\/code>&nbsp;<\/strong>instance to which the element is being added, in case the element needs to make use of that information. What each function does is totally dependent on the derived class. Indeed, different derived classes may do totally different things. These functions may keep a count of the number of arrays the instance is in, or they may perform more complex housekeeping. What they do, or should do, is the business of that class, and not of the&nbsp;<strong><code>DynArray<\/code><\/strong>&nbsp;class. So we&nbsp;<strong><em>delegate<\/em><\/strong>&nbsp;those functions to the element class, where it belongs.<\/p>\n<\/div>\n<\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Note<\/strong>: Since we always use the&nbsp;<strong><code>DynArrayElement<\/code><\/strong>&nbsp;class with the&nbsp;<strong><code>DynArray<\/code>&nbsp;<\/strong>class, we will place its declarations in the&nbsp;<code>dynarray.h<\/code><br>file. The implementation of the <strong><code>DynArray<\/code><\/strong> class will be in the&nbsp;<code>dynarray.cpp<\/code> file.<\/p>\n<\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Use_of_Exceptions\"><\/span><em>Use of Exceptions<\/em><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Several of the member functions of the&nbsp;<strong><code>DynArray<\/code>&nbsp;<\/strong>class have to deal with exceptional conditions. For example, an attempt to access an element via an index, when there is no element at that index, should be invalid. In this case, the function will throw an exception &#8212; specifically an&nbsp;<strong><code>ArrayOutOfBoundsException<\/code><\/strong>.<\/p>\n\n\n\n<p>To make our exceptions simpler to deal with, we organize them into a class hierarchy:<\/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=\"class Exception\n{\n  public:\n    virtual ~Exception() {}\n};\n\nclass OutOfMemoryError : public Exception\n{\n};\n\nclass ArrayOutOfBoundsException : public Exception\n{\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: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Exception<\/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\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">virtual<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">~Exception<\/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\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">OutOfMemoryError<\/span><span style=\"color: #000000\"> : <\/span><span style=\"color: #0000FF\">public<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Exception<\/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\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">ArrayOutOfBoundsException<\/span><span style=\"color: #000000\"> : <\/span><span style=\"color: #0000FF\">public<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Exception<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Since we are using these exceptions only for the&nbsp;<strong><code>DynArray<\/code>&nbsp;<\/strong>class, we will place their declarations in the&nbsp;<code>dynarray.h<\/code> file.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>We do this so that we can catch a specific one of these exceptions by doing something like:<\/p>\n\n\n\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(1 * 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=\"  try\n  {\n    \/\/ code that may throw an exception\n  }\n  catch (ArrayOutOfBoundsException &amp;e)\n  {\n    \/\/ Do something...\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: #000000\">  <\/span><span style=\"color: #AF00DB\">try<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">    \/\/ code that may throw an exception<\/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\">catch<\/span><span style=\"color: #000000\"> (ArrayOutOfBoundsException &amp;e)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">    \/\/ Do something...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Alternatively, we can catch any of these exceptions by doing something like:<\/p>\n\n\n\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(1 * 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=\"  try\n  {\n    \/\/ code that may throw an exception\n  }\n  catch (Exception &amp;e)\n  {\n    \/\/ Do something...\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: #000000\">  <\/span><span style=\"color: #AF00DB\">try<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">    \/\/ code that may throw an exception<\/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\">catch<\/span><span style=\"color: #000000\"> (Exception &amp;e)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">    \/\/ Do something...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Requirements_for_the_DynArray_class\"><\/span><em>Requirements for the&nbsp;<\/em><code>DynArray<\/code><em>&nbsp;class<\/em><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Here are the requirements for this&nbsp;<strong><code>DynArray<\/code><\/strong>&nbsp;class:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A constructor that accepts two arguments: <strong><code>int initialCapacity<\/code><\/strong>, the initial storage capacity of the array, and <strong><code>int capacityIncrement<\/code><\/strong>, how much to increase the element&#8217;s capacity on each expansion. The default value for each of these arguments will be 10: <strong>DynArray(int initialCapacity = 10, int capacityIncrement = 10);<\/strong><\/li>\n\n\n\n<li>The copy constructor and the assignment operator will be disabled (at least for the moment): <br><strong>DynArray(const DynArray &amp;array);<\/strong> <br><strong>DynArray &amp;operator=(const DynArray &amp;array);<\/strong><\/li>\n\n\n\n<li>A destructor that performs the necessary cleanup for the array: <strong>~DynArray();<\/strong> What does &#8216;necessary cleanup&#8217; mean here?&nbsp;<strong>[Hint: It means more than a simple&nbsp;delete<\/strong><strong>!]<\/strong><\/li>\n\n\n\n<li>An access method to return the number of elements currently stored in the array: <strong>int ElementCount() const ;<\/strong><\/li>\n\n\n\n<li>An access method to return the current capacity of the array: <strong>int Capacity() const;<\/strong><\/li>\n\n\n\n<li>An access method to return the array&#8217;s capacity increment: <strong>int CapacityIncrement() const;<\/strong><\/li>\n\n\n\n<li>A method to ensure that the array has at least the specified capacity. If it doesn&#8217;t, then the array is expanded appropriately: <strong>void EnsureCapacity(int minCapacity);<\/strong> If the function fails to expand the array, it throws&nbsp;<strong>OutOfMemoryError<\/strong>.<\/li>\n\n\n\n<li>A method to trim the array&#8217;s capacity down to size (i.e. reduce its capacity to the actual number of elements currently stored): <strong>void TrimToSize();<\/strong> If the function fails to contract the array, it throws&nbsp;<strong>OutOfMemoryError<\/strong>.<\/li>\n\n\n\n<li>A method to determine whether the array is empty or not: <strong>bool IsEmpty() const;<\/strong> Returns a Boolean value; true if the array has no elements, false otherwise.<\/li>\n\n\n\n<li>A method to determine whether the specified element is in the array: <strong>bool Contains(const DynArrayElement &amp;element) const;<\/strong> Returns a Boolean value; true if element is in the array, false otherwise. Note that it is possible for an element to be in the array more than once.<br><em><strong>Hint<\/strong>: This may be implemented by delegating most of its work to another member function in this class.<\/em><\/li>\n\n\n\n<li>A method to add an element to the end of the array: <strong>void AddElement(const DynArrayElement &amp;element);<\/strong> <br>Note that, if there is insufficient capacity for this element to be added, the array of pointers will be automatically extended. If the function fails to expand the array, it throws&nbsp;<strong>OutOfMemoryError<\/strong>. An element may be placed in the same array more than once.<\/li>\n\n\n\n<li>A method to remove an element from an array: <strong>bool RemoveElement(const DynArrayElement &amp;element);<\/strong> Returns a Boolean value; true if the element was found and removed, false otherwise. If an element occurs more than once in the array, only the first occurrence is removed.<\/li>\n\n\n\n<li>A method to remove an element from an array, using its index: <strong>void RemoveElementAt(int index);<\/strong> If there is no element with that index, the function throws&nbsp;<strong>ArrayOutOfBoundsException<\/strong>.<\/li>\n\n\n\n<li>A method to remove all elements from an array, from last to first: <strong>void RemoveAllElements();<\/strong> <br>(Think about how one might do this efficiently.)<\/li>\n\n\n\n<li>A method to set an element into the array at a particular index, replacing the element that was previously there: <strong>void SetElementAt(const DynArrayElement &amp;element, int index);<\/strong> The index must be within the range:&nbsp;<strong>(0&nbsp;<\/strong>to<strong>&nbsp;ElementCount()-1)<\/strong>, else the function throws an&nbsp;<strong>ArrayOutOfBoundsException<\/strong>. The previous element at that index is discarded (removed from the array).<\/li>\n\n\n\n<li>A method to insert an element into the array at a particular index: <strong>void InsertElementAt(const DynArrayElement &amp;element, int index);<\/strong> The index must be within the range:&nbsp;<strong>(0&nbsp;<\/strong>to<strong>&nbsp;ElementCount()-1)<\/strong>, else the function throws an&nbsp;<strong>ArrayOutOfBoundsException<\/strong>. Elements with an index greater than or equal to the current index are shifted up to make room.<\/li>\n\n\n\n<li>A method to return the index of an element in the array: <strong>int IndexOf(const DynArrayElement &amp;element, int indexStart = 0) const;<\/strong> Searches the array for the specified element, starting from indexStart. Returns the index of the element, if it is in the array; -1 if it is not in the array. If the element is in the array more than once, the index of the first occurrence encountered is returned.<\/li>\n\n\n\n<li>An overloaded&nbsp;<code><strong>operator[]<\/strong><\/code>&nbsp;:<strong>DynArrayElement &amp;operator[](int index);<\/strong> Allows the user to access&nbsp;<strong>DynArrayElement<\/strong>s stored in a&nbsp;<strong>DynArray<\/strong>&nbsp;using subscript syntax (for example&nbsp;<code><strong>da[5]<\/strong><\/code>).&nbsp;<\/li>\n\n\n\n<li>An overloaded&nbsp;<strong><code>operator[]<\/code><\/strong>&nbsp;:<strong>const DynArrayElement &amp;operator[](int index) const;<\/strong>Allows the user to access&nbsp;<strong>DynArrayElement<\/strong>s stored in a&nbsp;<strong>const DynArray<\/strong>&nbsp;using subscript syntax.<\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Note that the overloaded <strong><code>operator[]<\/code><\/strong> operators do&nbsp;<em>not<\/em>&nbsp;do the equivalent of <strong><code>SetElementAt<\/code><\/strong>, or <code><strong>InsertElementAt<\/strong><\/code>, or <strong><code>RemoveElementAt<\/code><\/strong>.&nbsp; Do you understand why?<\/p>\n<\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Requirements_for_testing\"><\/span><em>Requirements for testing<\/em><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>You will need to test your&nbsp;<strong>DynArray&nbsp;<\/strong>class, and so you&#8217;ll need to create a non-abstract class that can be used in such an array. In other words, it must be derived from&nbsp;<strong>DynArrayElement<\/strong>, and must implement the&nbsp;OnInsertion()&nbsp;and&nbsp;OnRemoval()&nbsp;virtual member functions. Let&#8217;s use something really simple: a&nbsp;<strong>Double<\/strong>&nbsp;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-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=\"class Double : public DynArrayElement\n{\n  public:\n    Double(double d);\n    Double(const Double &amp;d);\n    Double &amp;operator=(const Double &amp;d);\n    virtual ~Double();\n    double DoubleValue() const;  \/\/ return m_value\n\n    virtual void OnInsertion(DynArray &amp;array);\n    virtual void OnRemoval(DynArray &amp;array);\n  private:\n    double m_value;     \/\/ The encapsulated value\n    int m_inArrayCount; \/\/ Count of array memberships\n                        \/\/ (initialized to 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: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Double<\/span><span style=\"color: #000000\"> : <\/span><span style=\"color: #0000FF\">public<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">DynArrayElement<\/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\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">Double<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">d<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #795E26\">Double<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">d<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #267F99\">Double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #AF00DB\">operator=<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">d<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">virtual<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">~Double<\/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\"> <\/span><span style=\"color: #795E26\">DoubleValue<\/span><span style=\"color: #000000\">() <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #008000\">  \/\/ return m_value<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">virtual<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">OnInsertion<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #267F99\">DynArray<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">array<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">virtual<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">OnRemoval<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #267F99\">DynArray<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">array<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><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\"> m_value;<\/span><span style=\"color: #008000\">     \/\/ The encapsulated value<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> m_inArrayCount;<\/span><span style=\"color: #008000\"> \/\/ Count of array memberships<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">                        \/\/ (initialized to 0)<\/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>Here&#8217;s what you&#8217;ll need to implement the&nbsp;<strong>Double&nbsp;<\/strong>class<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The constructors. These should be self-explanatory (but don&#8217;t forget to use initializer lists!)<\/li>\n\n\n\n<li>The assignment operator. Again, this should be simple (but don&#8217;t forget the assignment to self problem!)<\/li>\n\n\n\n<li>The&nbsp;<strong>DoubleValue()<\/strong>&nbsp;access method. Trivial!<\/li>\n\n\n\n<li>The&nbsp;<strong>OnInsertion()<\/strong>&nbsp;function. It should simply increment the <strong><code>m_inArrayCount<\/code><\/strong>.<\/li>\n\n\n\n<li>The&nbsp;<strong>OnRemoval()<\/strong>&nbsp;function. It should simply decrement the <strong>m_inArrayCount<\/strong>.<\/li>\n\n\n\n<li>The destructor. This is very slightly trickier &#8212; you will need to test whether&nbsp;<strong>m_inArrayCount<\/strong>&nbsp;is zero or not. Use the&nbsp;<strong>assert<\/strong>&nbsp;macro to assert that its value should be zero. (See&nbsp;<strong>assert.h<\/strong>)<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<p>Note that it is a good idea to &#8216;instrument&#8217; your code with stuff like:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>cout &lt;&lt; \"Double element inserted into DynArray\" &lt;&lt; endl;<\/strong><\/code><\/pre>\n\n\n\n<p>so you can trace what&#8217;s happening during execution.<\/p>\n\n\n\n<p>Write yourself a test program to test that everything works. Be thorough!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Part_2_Enhancements\"><\/span><em>Part 2: Enhancements <\/em><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Change your&nbsp;<strong>DynArray&nbsp;<\/strong>class so that it is derived from&nbsp;<strong>DynArrayElement<\/strong>. Why might this be a good idea?<\/p>\n\n\n\n<p>Does your class now compile? What do you have to do to make it compile?<\/p>\n\n\n\n<p>Here is a function you can use to test this out:<\/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=\"void TestIt(void)\n{\n  DynArray parent;\n  DynArray child1, child2, child3;\n\n  parent.AddElement(child1);\n  parent.AddElement(child2);\n  parent.AddElement(child3);\n\n  Double d1(1.0), d2(2.0), d3(3.0);\n\n  child1.AddElement(d1);\n  child1.AddElement(d2);\n  child1.AddElement(d3);\n\n  Double d4(4.0), d5(5.0), d6(6.0);\n\n  child2.AddElement(d4);\n  child2.AddElement(d5);\n  child2.AddElement(d6);\n\n  Double d7(7.0), d8(8.0), d9(9.0);\n\n  child3.AddElement(d7);\n  child3.AddElement(d8);\n  child3.AddElement(d9);\n  cout &lt;&lt; &quot; &quot;;\n  for (int k = 0; k &lt; child1.ElementCount(); k++)\n  {\n    cout &lt;&lt; &quot;[&quot; &lt;&lt; k &lt;&lt; &quot;] &quot;;\n  }\n  cout &lt;&lt; endl;\n  for (int i = 0; i &lt; parent.ElementCount(); i++)\n  {\n    DynArray &amp;child = (DynArray &amp;)(parent[i]);\n    cout &lt;&lt; &quot;[&quot; &lt;&lt; i &lt;&lt; &quot;]: &quot;;\n    for (int j = 0; j &lt; child.ElementCount(); j++)\n    {\n      Double &amp;d = (Double &amp;)(child[j]);\n      cout &lt;&lt; d.DoubleValue() &lt;&lt; &quot; &quot;;\n  }\n    cout &lt;&lt; endl;\n  }\n\n  child3.RemoveAllElements();\n  child2.RemoveAllElements();\n  child1.RemoveAllElements();\n  parent.RemoveAllElements();\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: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">TestIt<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">void<\/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\">  DynArray parent;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  DynArray child1, child2, child3;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">parent<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(child1);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">parent<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(child2);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">parent<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(child3);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Double <\/span><span style=\"color: #795E26\">d1<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">1.0<\/span><span style=\"color: #000000\">), <\/span><span style=\"color: #795E26\">d2<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">2.0<\/span><span style=\"color: #000000\">), <\/span><span style=\"color: #795E26\">d3<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">3.0<\/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\">child1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(d1);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">child1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(d2);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">child1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(d3);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Double <\/span><span style=\"color: #795E26\">d4<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">4.0<\/span><span style=\"color: #000000\">), <\/span><span style=\"color: #795E26\">d5<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">5.0<\/span><span style=\"color: #000000\">), <\/span><span style=\"color: #795E26\">d6<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">6.0<\/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\">child2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(d4);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">child2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(d5);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">child2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(d6);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Double <\/span><span style=\"color: #795E26\">d7<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">7.0<\/span><span style=\"color: #000000\">), <\/span><span style=\"color: #795E26\">d8<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">8.0<\/span><span style=\"color: #000000\">), <\/span><span style=\"color: #795E26\">d9<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">9.0<\/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\">child3<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(d7);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">child3<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(d8);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">child3<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">AddElement<\/span><span style=\"color: #000000\">(d9);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; &quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> k = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">; k &lt; <\/span><span style=\"color: #001080\">child1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">ElementCount<\/span><span style=\"color: #000000\">(); k++)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;[&quot;<\/span><span style=\"color: #000000\"> &lt;&lt; k &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;] &quot;<\/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\">  cout &lt;&lt; endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> i = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">; i &lt; <\/span><span style=\"color: #001080\">parent<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">ElementCount<\/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\">    DynArray &amp;child = (DynArray &amp;)(<\/span><span style=\"color: #001080\">parent<\/span><span style=\"color: #000000\">[i]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;[&quot;<\/span><span style=\"color: #000000\"> &lt;&lt; i &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;]: &quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> j = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">; j &lt; <\/span><span style=\"color: #001080\">child<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">ElementCount<\/span><span style=\"color: #000000\">(); j++)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">      Double &amp;d = (Double &amp;)(<\/span><span style=\"color: #001080\">child<\/span><span style=\"color: #000000\">[j]);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">      cout &lt;&lt; <\/span><span style=\"color: #001080\">d<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">DoubleValue<\/span><span style=\"color: #000000\">() &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; &quot;<\/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\">    cout &lt;&lt; endl;<\/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: #001080\">child3<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">RemoveAllElements<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">child2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">RemoveAllElements<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">child1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">RemoveAllElements<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">parent<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">RemoveAllElements<\/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>Before you get too concerned about the amount of work you might have to do for this assignment, here&#8217;s some statistics from my implementation, including the enhancements:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>dynarray.h:<\/code>&nbsp;approximately 95 lines of code, including whitespace and comments<\/li>\n\n\n\n<li><code>dynarray.cpp<\/code><strong>:<\/strong>&nbsp;approximately 240 lines of code, including whitespace and comments.<\/li>\n\n\n\n<li>longest function: 38 lines of code, including whitespace and comments.<\/li>\n\n\n\n<li>most functions 10 lines or less, including whitespace and comments.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-large-font-size\"><strong>Happy coding!<\/strong><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Vector: A Dynamic Array Class The standard Java class library contains a useful class called&nbsp;Vector. It is actually a kind of dynamic array. This assignment involves writing a C++ class, called&nbsp;DynArray, that emulates a subset of the Java&nbsp;Vector&nbsp;class (see here). In addition, it involves writing other, related, classes, and is a useful exercise in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":29,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-36","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/36","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=36"}],"version-history":[{"count":9,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/36\/revisions"}],"predecessor-version":[{"id":1697,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/36\/revisions\/1697"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/29"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}