COMP 322 Assignment 3: How Smart is your Pointer? solved

$35.00

Category: You will receive a download link of the .ZIP file upon Payment

Description

5/5 - (1 vote)

Question 1 (15 pts)
Research smart pointers provided by the standard library (since C++11). List them and
explain the difference between them.
Question 2 (15 pts)
Design and implement your own smart pointer class called SmartPointer that will
automatically delete the memory for built-in integer type that is allocated through it.
SmartPointer class should be used this way in your main() function:
SmartPointer sPointer(11);
2
Here we are passing 11 as an argument to the constructor of SmartPointer class. The
constructor will allocate an integer variable and will initialize it to 11.
To get the value of your variable, you should provide and implement a method called
getValue(). You use it this way:
cout << sPointer.getValue(); // prints 11
You should also be able to differ the initialization of your allocated int variable to a later
moment. For example the following code should be supported as well:
SmartPointer sPointer;
sPointer.setValue(133);
cout << sPointer.getValue(); // prints 133
Please note that for this question, SmartPointer class will only handle allocation of integer
type variables. Allocating arrays is not supported to keep the class simple. No reference
counting is required.
The default constructor should allocate and initialize the allocated integer to zero. The
destructor should take care of deleting the allocated memory.
Question 3 (15 pts)
Exceptions are problems encountered during the execution of the program. For example, if
the system runs out of memory during a dynamic allocation, it will raise an exception.
These exceptions should be treated whenever they are raised in order to avoid the
program from yielding undefined behavior or even worse, crashing.
Add appropriate exception handling for the SmartPointer class to treat the following 2
cases:
1. When allocating a new variable, the code should throw an exception if the system
runs out of memory. This exception should be treated and a message should be
3
displayed to the screen warning the user that the variable was not being allocated. I
am aware that this cannot really be tested out of the box, so I’m not expecting your
main to provide a test case for this situation. However, I want to see that your code
“catch” the appropriate exception that the operating system throws when it runs out
of memory.
2. Let’s assume that we are only interested in positive numbers. The code should
throw an exception whenever a user tries to assign a negative number to any
variable that was being allocated through SmartPointer class. This exception should
be treated and a message should be displayed to the screen warning the user that
the class does not handle negative numbers.
Question 4 (15 pts)
Make the class SmartPointer generic using templates to be able to allocate any built-in type
through it (int, float, double). Let’s ignore char and string types for simplicity.
Use case:
SmartPointer sPointer;
sPointer.setValue(13.31);
cout << sPointer.getValue();
Question 5 (20 pts)
Overload the operators +, – and * to be able to perform arithmetic operations on variables
allocated through SmartPointer class. Don’t use member methods for the overloaded
functions, use friend functions instead. These overloaded functions should be generic as
well (using templates).
Use case:
SmartPointer sPointer1;
4
sPointer1.setValue(1.5);
SmartPointer sPointer2;
sPointer2.setValue(2.5);
SmartPointer sPointer3 = sPointer1 + sPointer2;
cout << sPointer3.getValue() << endl; // prints 4
Question 6 (20 pts)
How can you adapt the class SmartPointer to be able to handle allocation of arrays? Please
provide a full implementation. Feel free to choose any suitable signature for your class,
including the way it should be used.
Note that the same class should now handle both cases, the array case and the single
variable case. You should not provide two separate implementations to handle these 2
cases.
You should also add the use case in the main function.
5