Sale!

 CSED 232 Programming Assignment # 4 Template & STL  Solved

Original price was: $35.00.Current price is: $30.00.

Download Details:

  • Name: assn4-sx7mce.zip
  • Type: zip
  • Size: 357.59 KB

Category:

Description

5/5 - (1 vote)

2. Program Design and Implementation ● Are the variables and algorithms designed well to satisfy the requirements? ● Are all the detailed conditions presented in the problem satisfied? ● Has the designed content been properly implemented using the required language? 3. Program Readability ● Is the program written in a way that is easy to read and understand? ● Is it easy to understand what the variable names mean? ● Is the program source code well commented to make it easy to understand? 4. Report Structure, Content, and Format ● Is the report well written, easy to understand, and easy to read with appropriate content? ● Did it follow the report format properly? Copying or simply modifying someone else’s code or a program from the Internet will result in an unconditional ‘F’ grade. If such misconduct is discovered, there may be additional disadvantages according to the standards set by the department. Page 3 / 11 Shared Pointer & Image Library Objective In this assignment, you will directly implement shared_ptr, one of the functions provided by the Standard Template Library, and implement an image processing class using it to increase your understanding of templates and STL. Problem 1 – SharedPtr 1.1. Overview Problem 1 of this assignment implements the SharedPtr template class, a simplified version of shared_ptr. Smart pointers are a feature of C++’s Standard Template Library (STL) that protects programs from memory leaks. A smart pointer is a class template that behaves like a pointer and automatically dynamically frees memory after use. When using a regular pointer, you must declare it to point to actual memory using the new operator, and manually free the memory using the delete operator after use. This approach makes memory management difficult and frequently leads to memory leaks or runtime errors caused by memory issues. Smart pointers solve this problem by automatically freeing memory after use, eliminating the need for developers to manually delete the pointer. C++’s STL provides several types of smart pointers, one of which is shared_ptr . Shared_ptr can be used by including the memory header file. Shared_ptr uses a reference counting mechanism to determine whether a dynamically allocated memory area is no longer in use. For information on how smart pointers work using reference counting, please refer to the lecture material “16. The string Class and the Standard Template Library (1)” and the appendix of this document. 1.2. Assignment Requirements The functions of SharedPtr that must be implemented in this assignment are as follows. ⚫ SharedPtr Creation ◼ SharedPtr is a template class that receives at least one template parameter. If you create a pointer that points to an object of a class called MyClass, you should be able to create a SharedPtr object using the following code. ◆ SharedPtr ptr; ◼ You can also initialize a SharedPtr object using a dynamically allocated memory area when creating a SharedPtr object, as in the example code below. Page 4 / 11 ◆ SharedPtr ptr(new MyClass()); ◼ You can initialize a SharedPtr object using another SharedPtr object that points to an object of the same type. ◆ SharedPtr ptr(new MyClass()); SharedPtr ptr2(ptr); ⚫ SharedPtr Assignment Operation ◼ A SharedPtr object can be assigned to another SharedPtr object. In this case, the two SharedPtr objects will point to common dynamically allocated memory.◆ SharedPtr ptr(new MyClass()); SharedPtr ptr2; ptr2 = ptr; ◼ You cannot directly assign a new dynamically allocated memory address to a SharedPtr object. If you want to assign a new dynamically allocated memory address to an existing SharedPtr object, you must create a new object using the SharedPtr constructor and assign it. ◆ SharedPtr ptr; ptr = new MyClass(); // must raise a compile error!! ptr = SharedPtr(ptr); // You must do this. ⚫ Using the SharedPtr object ◼ A SharedPtr object must support the following two operators (*, ->) to enable use of the object it points to. Both operators must support const and non-const versions. ◆ SharedPtr ptr(new MyClass); ptr->some_method(); (*ptr).some_method(); const SharedPtr const_ptr(new MyClass); const_ptr->some_const_method(); (*const_ptr).some_const_method(); ◼ SharedPtr objects must be able to be converted to regular pointers when necessary. ◆ SharedPtr ptr(new MyClass); MyClass* ptr2 = (MyClass*)ptr; const MyClass* ptr3 = (const MyClass*)ptr; Page 5 / 11 ⚫ Automatic dynamic freeing ◼ Memory areas that are no longer referenced by SharedPtr objects are automatically dynamically freed. Below is an example of this behavior. ◆ SharedPtr ptr(new MyClass(1)); // Dynamically allocate the first object SharedPtr ptr2(ptr); ptr = SharedPtr(new MyClass(2)); // Dynamically allocate the second object. The first object is maintained because it is pointed to by ptr2 ptr2 = SharedPtr(new MyClass(3)); // Dynamically allocate the third object. The first object is automatically deallocated dynamically because no SharedPtr objects point to it anymore. ⚫ Support for dynamically allocated arrays ◼ The SharedPtr object created in this assignment also supports dynamic allocation of arrays. In the case of arrays, you must use the new[] and delete[] operators instead of new and delete for dynamic allocation. To this end, the SharedPtr template supports handling memory deallocation differently for general objects and arrays by inputting a function for deallocation as the second template parameter. Please refer to the skeleton code provided with this assignment for the implementation. ◆ template void ArrayDeallocator(T* ptr) { delete[] ptr; } SharedArray<int,ArrayDeallocator> ptr(new int[N]); // You can specify a function to deallocate an array as the second template parameter. If the second template parameter is not specified, a function to deallocate a general object is specified as the default parameter. ◼ The skeleton code provided together provides a type called SharedArray defined as follows for arrays. ◆ Template using SharedArray = SharedPtr<T,ArrayDeallocator >; ◼ The part that needs to be written in this assignment is the array element access operator to support dynamically allocated arrays. ([ ]) ◆ SharedArray ptr(new MyClass[10]); ptr[0].some_method(); const SharedArray ptr(new MyClass[10]); ptr[2].some_const_method(); Page 6 / 11 ⚫ Other requirements ◼ If the SharedPtr template class can be improved from an object-oriented programming or generic programming perspective or from any other aspect, describe in the report how it can be improved. ◼ Among the attached codes, SharedPtr_test.cpp contains simple codes that students can use to check for themselves whether the SharedPtr class template has been implemented properly. However, this code is provided for reference only. It is recommended that you personally verify whether your class has been properly implemented considering various situations. The teaching assistant will test more complex situations than the sharedPtr_test.cpp example. For reference, if the SharedPtr class template has been properly implemented, the compiled SharedPtr_test.cpp should produce the following output: test_SharedPtr() MyClass object(100) created: 1 MyClass object(200) created: 2 ============= ptr1: 200 ptr2: 100 ptr3: 100 ============= Dealloc Object MyClass object(100) destroyed: 1 ============= ptr1: 200 ptr2: 200 ptr3: 200 ============= MyClass object(300) created: 2 ============= const_ptr: 300 const_ptr: 300 ============ pp: 200 Dealloc Object MyClass object(300) destroyed: 1 Dealloc Object MyClass object(200) destroyed: 0 test_SharedArray() ============= arr1[0]: 1 arr2[0]: 1 arr3[0]: 1 ============= ============= arr1[0]: 2 arr2[0]: 3 arr3[0]:3 ============== Dealloc Array ============ arr1[0]: 2 arr2[0]: 2 arr3[0]: 2 ============= Dealloc Array Page 7 / 11 Problem 2 – Image 2.1. Overview Problem 2 of this assignment is to write a basic template class for image processing. An image is generally represented on a computer as a two-dimensional array of pixels, and each pixel stores information about brightness or color. In the case of a color image, information about the three colors Red, Green, and Blue is stored for each pixel, and in the case of a grayscale image, brightness information is stored for each pixel. Generally, color information or brightness information is stored using an 8-bit unsigned integer type with a value between 0 and 255. Therefore, a grayscale image uses 8 bits for each pixel, and a color image uses 8 bits for each color of the pixel, for a total of 24 bits. When applying numerical operations to each pixel of an image, floating-point real-number types such as float or double are sometimes used because 8-bit integer types are not suitable for various numerical operations. In this assignment, we will create a template class to handle such images. Through this, we will write a program that can read an image from a BMP file, one of the image file formats, perform simple image processing, and save the result as a BMP file. We will also write code that converts the image read as a BMP file into characters and outputs it. 2.2. Assignment Requirements The functions of the Image class to be implemented in this assignment are as follows. ⚫ Image Creation ◼ The Image class template supports various pixel types. To do this, it receives the pixel type as a template parameter. Example code is as follows. ◆ Image img; // Create a grayscale image object that uses an 8-bit unsigned integer type as its pixel value. uint8_t is an 8-bit unsigned integer type declared in cstdint. Image imgf; // Create a grayscale image object that uses float type as pixel value Image<RGB> rgbimg; // Create an RGB image that uses 8-bit integer type as pixel value. Refer to the provided skeleton code for the RGB<> class template ⚫ Public interface provided by the Image class template ◼ Constructors ◆ Image() // Default constructor Page 8 / 11 ◆ Image(size_t _width, size_t _height); // When creating an image object, allocate memory as large as the image (_width x _height) ◆ Image(size_t _width, size_t _height, const PixelType& val); // When creating an image object, allocate memory as large as the image and initialize all pixel values ​​using the val value ◆ Image(const Image& img); // copy constructor ◼ Destructor ◆ ~Image(); // Does nothing special. ◼ Operators ◆ Assignment operator (operator=) – An operator that receives assignment from an image object that uses the same pixel type. You should be able to perform the following actions: ⚫ const Image a; Image b; b = a; ◆ Array access operator ⚫ You do not need to implement this separately. Please refer to the skeleton code, which is already implemented. ◼ Other member functions ◆ size_t width() const; // Return the width of the image ◆ size_t height() const; // Return the height of the image ⚫ Other requirements ◼ The space for storing the pixel values ​​of the Image class must be implemented using dynamic allocation. In this case, implement it using the SharedArray implemented in Problem 1. ◼ If the Image template class can be improved from an object-oriented programming or generic programming perspective or from other aspects, describe in the report how it can be improved. ◼ The image_test.cpp provided with this assignment is a simple image processing example code using the Image class template. If the Image class template is implemented well, it will output the following content. In addition, if there are any other image processing examples or additional functions that you would like to implement, implement them and describe them in the report. The Image class will also be tested by the teaching assistant in more complex situations beyond the code presented in image_test.cpp. Page 9 / 11 Dealloc Array Dealloc Array Dealloc Array Dealloc Array Dealloc Array 2. ASCII conversion 2.1. Grayscale conversion Dealloc Array 2.2. Downsampling Dealloc Array Dealloc Array Dealloc Array 2.3.ASCII art drawing OOOOOOOOOOOOOOOOOOOOopEo3Cii}t5SVOOOOOOOOOOOOOOOOOOOOOOOOOOOOOode())JJJJJJJ7(uSpOOOOOOOOOOOOOOOOOOOOOOOOOx7))))JJJJJJ777iZhOOOOOOOOOOOOOOOOOOOoptv)))))JJJJJJJ77777Ij6pOd]p OOOOOOOOOOOOOOOOOOp3vvv))))) OOOOOOOOOOOOOOOOOKvvvvvv))))))))JJJJJJJJ77777|9O OOOOOOOOOOOOOOOOfTvvvvvv)v)))))JJJJJJJJ7777yOO OwOOOOOOOOOOOOOOOO6TTTvvvvvvv)))))JJJJJJJJ77YOOO OFfwOOOOOOOOOOOp5TTTvvvvvvv))v)))))JJJJJJIqVdOO OZ(7[4OOOOOOOOOOOVLTTTTTvvvvvvT+v))))))Jfnj6dyidOO OVI77F2OOOOOOOOOOOT=zTTTTvvT!^Lv)))))))}Z[3}FJJfOOO Op3JJJ)19OOOOOOOOOSk1vTTTTzCjpy)vv)))))7))JJJJJEOOO Olv7JJ))J5pOOOOOOSz^5TTTT3E/vV[)v))))))vvTFSOOOOOu“[)JTTo/`xjvvvvv)))))))|yOOOOOO Oy()))vvTTTL19OOd97vtfff7}I`=htvvvvv))))Jt2pOOOOOO OOpP))vvTLLLsv5pqFo3fffffTuShovvvvvvv))eSaZ3dOOOOO OOOOe(vTTLLsLLL|ZLvifFF7FJTTTTTTvvvvvv}|)))ZOOOOOO OOOOpSSFLLssLLLLsLLLL|+`.FTTTTTTTvvvvvvv))fpOOOOOO OOOOOOOpYJLLLLLLLLsLTFJi7LTTTTTTTvvvvvvvvf4OOOOOOO OOOOO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OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOVh2(e22)]2ESx|oOOOOOOOOOOOOOOOOOOOOOOOOOE5ZlI[55555555CF5OOOOOOOOOOOOOOOOOOOOOOPHqE)) counting) 동적 using STL’s shared_ptr Manages allocated objects. The principle of operation is as follows. Let’s say we have code like below. 1: { 2: string* tmp = new string(“Some String”); 3: shared_ptr ptr1(tmp); 4: { 5: shared_ptr ptr2; 6: ptr2 = ptr1; 7: } 8: } Suppose the above code is executed up to line 2. In this case, the string object stored by tmp is dynamically allocated to the heap area. In this case, the heap area can be represented as follows. As line 3 is executed, ptr1, an object of shared_ptr, is created and ptr1 stores the string object stored by tmp. Similarly, it creates a variable (counter) that checks how many shared_ptr objects are storing this object in the heap area. And since ptr1 is currently hiding this object, initialize the counting variable to 1. 도식화 if this situation is as follows. 5 times As this line executes, a shared_ptr object, ptr2, is created. And the 6th line is executed and the value of ptr1 is inserted into ptr2. In 이 대정연산산 ptr2 는 ptr1 이 가리고 다 다 이 가리고고 이 가리고 가리 도 counter at the same time.Also, at this time, the value of counter is increased by 1. When line 7 on page 11/11 is executed, the inner block is exited and the ptr2 object, which is a local variable within the block, is destroyed. As the ptr2 object is destroyed, the counter is decreased by 1. Finally, when line 8 is executed, the outer block is exited and the ptr1 object, which is a local variable within the block, is destroyed. As the ptr1 object is destroyed, the counter is decreased by 1 again. And if the decremented counter becomes 0, the destructor of the ptr1 object dynamically frees both the counter and the object.