Question: Here's main.cpp. I need DynamicArray.h: #include #include #include #include DynamicArray.h using namespace std; // Sample class, to show a template can work with anything class

Here's main.cpp. I need DynamicArray.h: #include #include #include #include "DynamicArray.h" using namespacestd; // Sample class, to show a template can work with anythingclass Hero { string name_; int hitpoints_; int maxHP_; public: Hero() {}Here's main.cpp. I need DynamicArray.h:

#include #include #include #include "DynamicArray.h" using namespace std;

// Sample class, to show a template can work with anything class Hero { string name_; int hitpoints_; int maxHP_; public: Hero() {} Hero(const char *name, int hp, int maxHP) { name_ = name; hitpoints_ = hp; maxHP_ = maxHP; } void SetName(const char *name) { name_ = name; } string GetName() { return name_; } friend ostream &operator

void TestOne(); void TestTwo(); void TestThree(); void TestFour();

int main() { cout

void TestOne() { cout data(0); cout

cout data2(10); cout

for (int i = 0; i

for (unsigned int i = 0; i

cout

cout

for (unsigned int i = 0; i

cout data3;

data3.Add(100); data3.Add(999); data3.Add(200); data3.Add(300); data3.Add(400);

for (unsigned int i = 0; i

cout

for (unsigned int i = 0; i

cout

cout

} void TestFour() { DynamicArray heroes; heroes.Add(Hero("Conan", 100, 120)); heroes.Add(Hero("Thor", 150, 150)); heroes.Add(Hero("Merlin", 80, 85)); heroes.Add(Hero("Bob", 25, 26)); cout

for (unsigned int i = 0; i

DynamicArray heroClones; cout

cout Description All programs need storage of some kind as they execute. Arrays are the most basic, and probably the most common form of storage. However, because of their fixed nature they can be problematic to use, particularly if you need to store data of varying lengths as your application's needs change over time. One of the first data structures people start to use in any programming language is some form of an expandable array. Java has the ArrayList class, C# has the List class, and even C++ has the vector class. The DynamicArray class you create will be very similar to the vector class. The concepts behind allocating and freeing memory will be applicable when implementing numerous other data structures and algorithms, so you aren't reinventing the wheel here. You're learning how wheels are made, so you can build your own, custom version of them as needed in later projects. Templates Templates in C++ can be a bit confusing at first, but they allow you to reuse code very easily. By defining a class as a template, you can create multiple instances of that class to use different types. For example: Foo a; // Instance of Foo which uses ints Foo b; // Instance of Foo which uses floats Foo c; // Instance of Foo which uses Bars The DynamicArray class you write is going to be a storage container, and while you may create storage containers in the future that are custom-built to solve a single problem, reusable code can make your life much easier. DynamicArray Storage Internally, a class like this does not store much data. There are only three data members that you are required to store (though you may add any additional variables you see fit to help you write this) Data A pointer to the data you are storing. This will be based on the type-id you define for the template. This pointer is the core of this class. It's where the magic happens. Size How many objects are being stored currently? Capacity- How many objects COULD be stored? This is not the same as the size

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!