Answered step by step
Verified Expert Solution
Question
1 Approved Answer
array & c-style string need c++ format Wrapping Arrays In English, the word wrap means to cover or enclose. Programmers often use the term wrap
array & c-style string
need c++ format
Wrapping Arrays In English, the word "wrap" means "to cover or enclose". Programmers often use the term wrap to mean the act of placing one kind of programming construct inside another. This is often done for convenience, compatibility, or to add features to an existing object. You will sometimes here the term "thin wrapper" used in object-oriented programming to mean that a minimal class has been constructed to "wrap" some object. If the "wrapper" does not add much [or any] functionality on its own, it is "thin The default behaviors of arrays in C++ are driven by the fact that arrays are a reference type the array variable is not a named storage location for the entire contents of the array but rather it contains just enough information to locate the array in memory, and must be combined with an index to provide access to (part of) the logically enclosed data. This means that arrays seem to behave differently than most other data types in C++. Primitive, structured, and object types all have similar behavioral defaults. For example, they default to pass-by-value semantics with respect to function calls, while arrays default to pass-by-reference semantics. If we wrap an array in a struct or class however-even a thin wrapper- we change the default behavior. Just by wrapping the array, we change it from being a reference type to either a structured type or object type. This means we get the following changes in default behavior [among others): * pass-by-value semantics with respect to function calls [VS the raw array pass-by-reference). e ability to perform [bitwise] assignment by default (VS the raw array being non-assignable]. In this assignment, you will experiment with object wrappers for arrays, with varying levels of added functionality A Thin Wrapper Start by creating a new header file ThinArraywrapper.h and a program file lab02hw.cpp that will be used for testing To wrap an array in the "thinnest possible way, you could use a struct, like this: struct SuperThinArrayWrapperf int array [201 In your main program, declare an instance of SuperThinArraywrapper and use a for loop to fill it with sequential values from 10 to 200, counting by 10% Notice that you will have to access the .array attribute of your object, so the notation is a bit more verbose than with raw arrays. After you have initialized the array, add a function matching the following prototype to ThinArraywrapper.h: void print array (SuperThinArraywrapper a)i Implement the function so that it will print each value in the wrapped array contained in parameter a to the screen, separated by a comma-space ("" There is a problem here. The array contained in SuperThinArrayWrapper is fixed at size 20. There is nothing we can do to change that, but it is also not obvious (without reading the structure definition]. This means the loop you write in print_array) must stop after printing 20 items, but you had to know that in advance. Perhaps we could introduce a second parameter to carry the size of the array- but this size will always be 20 (since it is hard-coded into the implementation). A better way to deal with it might be to make it a property of the SuperThinArrayWrapper itself so that we could "ask" what the size was. Create a new class ThinArraywrapper by making a copy of SuperThinArraywrapper and adding a static constant attribute, as shown below: struct ThinArrayWrappert 2 const static int size = 20; int array[size]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started