Answered step by step
Verified Expert Solution
Question
1 Approved Answer
c++ problems need .cpp and .h file RAll and Destructors Object-Oriented C++ offers a tool to help protect against memory leaks. If we create objects
c++ problems
need .cpp and .h file
RAll and Destructors Object-Oriented C++ offers a tool to help protect against memory leaks. If we create objects to represent dynamic data structures (like a dynamic array), the object itself can be responsible for its own initialization, memory allocation, and eventually memory deallocation. The lifetime of the dynamic resources can then be bound to the lifetime of the object itself. The term used for this idea of managing dynamic resources within a class in such a way that external users need not be aware of it is a part of the C++ design pattern known as RAII& (Resource Allocation Is Initialization) Add a new header file DynamicArray.h and a corresponding DynamicArray.cpp implementation file. We will start with the following simple class definition in the header file class DynamicArrayt public: DynamicArray (int size) int size) const return size; h int& at(int i) DynamicArray) private: int* _array nullptr int size0; In your implementation file, start by implementing the constructor as follows The parameter size contains the number of elements the user wants to store. If the value of size is greater than zero, allocate the appropriately sized integer array dynamically using the new operator and store the result in the attribute _array. (lf the value of size is not positive, do nothing and allow the default "empty" array state to remain.) Create a temporary cout statement in the constructor to indicate that the array has been created and what size it is. Check that your code will compile (no need to run it yet though). Fix any errors you receive At this point your object would allocate memory when it is instantiated, and the memory is owned by the attributearray. Thus, it is the responsibility of the object to deallocate the array memory when we are finished using it. When would this be? Well, an obvious choice is when the object itself is being destroyed (by going out of scope or being deleted C++ allows us to define a special kind of method called a destructor that will execute automatically whenever the object's lifetime is over. The prototype for the destructor is the same as a default constructor, except that it begins with the "tilde" symbol ( In our case, the prototype is: DynamicArray) Now, implement the destructor for DynamicArray in the implementation file. The destructor's responsibility is to perform any necessary shutdown actions on behalf of the object. In our case, since the object owns some dynamic memory, it is the destructor's responsibility to make sure that memory is released Release the dynamic memory owned by the _array attribute by using the delete [ operator. Create a temporary cout statement in the destructor to indicate that the array's memory has been freed. Now, check that your code will compile. When it does, add code to your main program to attempt to create an array (you choose the size anywhere between 10 and 10000). Compile the program and run it. You should observe the statements generated by both the constructor and the destructor. If you do not, fix the issue before moving on Finally, implement the at) method so that it performs these actions: The parameter i represents an index into the array that the user would like to access. Begin by checking to see if i is invalid, and if it is, throw the exception std: :out of range with the following statement: throw std: :out_of range"Array index out of rangeStep 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