CS 202 – Assignment #6 Purpose: Learn operator overloading both as member and non-member functions, friend functions, this pointer, copy constructor. Points: 100 Why we need Operator Overloading in...

I will attach the skeleton key and other necessary parts in the additional notes section.



CS 202 – Assignment #6 Purpose: Learn operator overloading both as member and non-member functions, friend functions, this pointer, copy constructor. Points: 100 Why we need Operator Overloading in C++? With the use of Operator Overloading we can make operators that can work for our user defined classes. You can redefine, majority of the C++ operators. We can give a special meaning for the existing operator and we can redefine it to work with our user-defined class. However, the operator overloading works only for user defined types but not pre-defined types(int, double, char, etc). An example of operator overloading is that, we can use ‘+’ to add two instance variables/objects of the same class, where it is not possible by default. Assignment: In this assignment you will learn to use operator overloading both as member and non-member functions, friend functions, this pointer, copy constructor. This assignment is based on matrix operations like addition, subtraction, multiplication, increment (pre-, post-), decrement (pre-, post-), assignment operation, copy constructor, transpose matrix. The matrix(s) should be loaded from a file. The program should prompt a message to select the size of matrix. In this assignment, the matrix is always a square matrix. That means rows and columns are same. After selecting the size, the program should prompt to select the data for each row and column. After input selection, the program should prompt a menu with the above operations. The program uses pointers and dynamic allocation for the above operations. As a designer, it is your job to free the memory so that your program is free from memory leaks. You can use Valgrind to check the memory leaks. Base form of the class, not including operators, has the following structure: Matrix #size: int #**matrix: int -MATRIX_SIZE_MIN=1: static constexpr int -MATRIX_SIZE_MAX=10: static constexpr int +Matrix(int) +~Matrix() +createMatrix(int A[MATRIX_SIZE][MATRIX_SIZE]): void +display(): void +DeallocateMatrix(): void +Matrix(Matrix &) +Matrix &operator=(Matrix &x); Member Variables Descriptions: # int size: determines the size for a row and column # int **matrix: To create a square matrix(two dimensional array) dynamically. - static constexpr int MATRIX_SIZE_MIN = 1: determines the MIN_SIZE of matrix which is 1 in this assignment - static constexpr int MATRIX_SIZE_MAX = 10: determines the MAX_SIZE of matrix which is 10 in this assignment. Member Functions Descriptions: Matrix(int); The constructor Matrix(int) should verify the passed size (between SIZE_MIN and SIZE_MAX, inclusive). If the size is valid, an integer two-dimensional array, size x size, should be dynamically created and all values set to 0. If the size is invalid, a message should be displayed (e.g., "Error, invalid size." and "Please provide valid size”) and the matrix class variable set to NULL and the size set to 0. ~Matrix(); The destructor ~Matrix() should delete the dynamically allocated memory (if any). The destructor is called automatically when the object goes out of scope. However, in this assignment, instead of writing the logic/code for destroying the dynamic allocation in the destructor, we call the function DeallocateMatrix() void createMatrix(int A[MATRIX_SIZE][MATRIX_SIZE]); This function is used to fill/create the matrix based on the size. The parameters A[MATRIX_SIZE] [MATRIX_SIZE] can be deemed as A[rows][columns]. From the file it reads the data and saves in matrix. However, you no need to use file operation in this function. Also, you no need to ask the user to enter the size of the matrix. File read and size are taken care in main.cpp. What you should do for createMatrix(int A[MATRIX_SIZE][MATRIX_SIZE])? This function is used to fill the class matrix (already created in the constructor). The function should prompt with a message to “Enter matrix index for” row column. For example, if you have selected 2x2 matrix (from the main), then the function should prompt 4 times; Enter matrix index for 00, Enter matrix index for 01, Enter matrix index for 10, and Enter matrix index for 11. However, the function will read an index and obtain the value from the passed array. The passed array is a 10x10 two- dimensional array. Specifically, the function will read a one-dimensional index, verify the index is valid (0 – 99), convert that index into a two-dimensional index ([index/10][index%10]), and read the value from the passed array at that two-dimensional index and place it into the class array. If the entered index is not in the above range, it should re-prompt to enter matrix index by displaying “Please provide valid index”. When all input is obtained (based on class matrix size), the function should display a new line and exit. void display(); This function should print the grid in a formatted manner (with each value in a small text box). Refer to the example executions for output formatting. Matrix(Matrix &x); This is for copy constructor operator overloading. It allows the deep copy when you use pointers as member variables in any class. Matrix &operator=(Matrix &x); This is for assignment operator overloading. It allows the deep copy when you use pointers as member variables in any class. void DeallocateMatrix(); This function is to delete the dynamically allocated memory (if any). Global functions listed in matrix.h – You have to write these definitions in your matrixImp.cpp file int fileOpen(ifstream &file, string fileName); This function should open the file and if the file is not found, return -1 else return 0; int fileRead(ifstream &file, int array2D[][cols]); This function is to read the values from the file and save it in to the order of matrix selected by the user. void print_matrix(const int M[rows][cols], int n, int m); This function is to print the Matrix of matrix_1.txt and matrix_2.txt. (check your main.cpp to know, how this function is called. Check your assignment 4 as a reference for this function) Note: Check sample output to understand the operations that have to be performed in this assignment 6. Refer to the example executions for output formatting. The following operators need to be overloaded for Matrix class in order to implement operations on matrices. friend functions for insertion and extraction operator: • To read data use the friend function: friend istream &operator>>(istream&, Matrix &); • To display/print data use the friend function: friend ostream &operator<(ostream&, const matrix &); overload the following arithmetic operators. note: matrix mat1(size), matrix mat2(size) and matrix mat3(size) are declared in main. after selecting size, the user will be prompted to select the data using creatematrix(int [matrix_size] [matrix_size]). here size is same for rows and columns. also the size of matrix 1 and matrix 2 is same. however, the user will be asked to enter index for each matrix individually. the result of arithmetic operations like addition, subtraction, multiplication, assignment operator (from mat1), and copy constructor (from mat1) are stored in the object mat3. however, the transpose, increment, decrement operators will update the matrix values of mat1 and mat2 individually. • + (addition) - adds two matrices (mat3=mat1+mat2) • - (subtraction) - subtracts two matrices (mat3=mat1-mat2) • *(multiplication) - multiplies two matrices (mat3=mat1*mat2) • !(transpose) – transpose mat1 and mat2 individually. (mat1=!mat1) overload increment and decrement operators – your selection from menu will reflect bot mat1 and mat2 • mat1++/mat2++ (post increment/ postfix operator) – it increments all the elements in mat1 and mat2 by 1 individually. this is post-increment operation. • ++mat1/++mat2 (pre increment/ prefix operator) – it increments all the elements in mat1 and mat2 by 1 individually. this is pre-increment operation. • mat1--/mat2-- (post decrement/ postfix operator) – it decrements all the elements in mat1 and mat2 by 1 individually. this is post-decrement operation. • --mat1/--mat2 (pre decrement/ prefix operator) – it decrements all the elements in mat1 and mat2 by 1 individually. this is pre-decrement operation. provided files: matrix.h main.cpp matriximp.cpp cs202_assignment6.pdf sample_output_ast6.pdf you have to complete the implementation file. write all the definitions of the functions in matriximp.cpp. the function prototypes are in header file. do not make any changes to the header file as well as main.cpp file. guidelines for submission: file 1: matriximp.cpp to compile: option 1: make option 2: g++ -g -std=c++11 -wall -wpedantic main.cpp matrix.cpp -o main to check memory leaks: valgrind --tool=memcheck --leak-check=yes --show-reachable=yes –show—leak-kinds=all --track-origins=yes --track-fds=yes -s ./main matrix_1.txt matrix_2.txt if you want save the output/report of a valgrind to a text file, then use the following: valgrind --tool=memcheck --leak-check=yes --show-reachable=yes –show—leak-kinds=all --track-origins=yes --track-fds=yes –log-file=ast6memcheck.txt -s ./main matrix_1.txt matrix_2.txt sample_output: please check the sample output, which is provided in a separate file. your program should match the sample output. the program has big menu and the menu should be displayed after each and every operation. however, you no need to worry about that, as the main.cpp includes that logic. in sample output, i will exclude the menu display after each operation to cut down the number of pages. but for checking the functionality of program, menu display can be handy. const="" matrix="" &);="" overload="" the="" following="" arithmetic="" operators.="" note:="" matrix="" mat1(size),="" matrix="" mat2(size)="" and="" matrix="" mat3(size)="" are="" declared="" in="" main.="" after="" selecting="" size,="" the="" user="" will="" be="" prompted="" to="" select="" the="" data="" using="" creatematrix(int="" [matrix_size]="" [matrix_size]).="" here="" size="" is="" same="" for="" rows="" and="" columns.="" also="" the="" size="" of="" matrix="" 1="" and="" matrix="" 2="" is="" same.="" however,="" the="" user="" will="" be="" asked="" to="" enter="" index="" for="" each="" matrix="" individually.="" the="" result="" of="" arithmetic="" operations="" like="" addition,="" subtraction,="" multiplication,="" assignment="" operator="" (from="" mat1),="" and="" copy="" constructor="" (from="" mat1)="" are="" stored="" in="" the="" object="" mat3.="" however,="" the="" transpose,="" increment,="" decrement="" operators="" will="" update="" the="" matrix="" values="" of="" mat1="" and="" mat2="" individually.="" •="" +="" (addition)="" -="" adds="" two="" matrices="" (mat3="mat1+mat2)" •="" -="" (subtraction)="" -="" subtracts="" two="" matrices="" (mat3="mat1-mat2)" •="" *(multiplication)="" -="" multiplies="" two="" matrices="" (mat3="mat1*mat2)" •="" !(transpose)="" –="" transpose="" mat1="" and="" mat2="" individually.="" (mat1="!Mat1)" overload="" increment="" and="" decrement="" operators="" –="" your="" selection="" from="" menu="" will="" reflect="" bot="" mat1="" and="" mat2="" •="" mat1++/mat2++="" (post="" increment/="" postfix="" operator)="" –="" it="" increments="" all="" the="" elements="" in="" mat1="" and="" mat2="" by="" 1="" individually.="" this="" is="" post-increment="" operation.="" •="" ++mat1/++mat2="" (pre="" increment/="" prefix="" operator)="" –="" it="" increments="" all="" the="" elements="" in="" mat1="" and="" mat2="" by="" 1="" individually.="" this="" is="" pre-increment="" operation.="" •="" mat1--/mat2--="" (post="" decrement/="" postfix="" operator)="" –="" it="" decrements="" all="" the="" elements="" in="" mat1="" and="" mat2="" by="" 1="" individually.="" this="" is="" post-decrement="" operation.="" •="" --mat1/--mat2="" (pre="" decrement/="" prefix="" operator)="" –="" it="" decrements="" all="" the="" elements="" in="" mat1="" and="" mat2="" by="" 1="" individually.="" this="" is="" pre-decrement="" operation.="" provided="" files:="" matrix.h="" main.cpp="" matriximp.cpp="" cs202_assignment6.pdf="" sample_output_ast6.pdf="" you="" have="" to="" complete="" the="" implementation="" file.="" write="" all="" the="" definitions="" of="" the="" functions="" in="" matriximp.cpp.="" the="" function="" prototypes="" are="" in="" header="" file.="" do="" not="" make="" any="" changes="" to="" the="" header="" file="" as="" well="" as="" main.cpp="" file.="" guidelines="" for="" submission:="" file="" 1:="" matriximp.cpp="" to="" compile:="" option="" 1:="" make="" option="" 2:="" g++="" -g="" -std="c++11" -wall="" -wpedantic="" main.cpp="" matrix.cpp="" -o="" main="" to="" check="" memory="" leaks:="" valgrind="" --tool="memcheck" --leak-check="yes" --show-reachable="yes" –show—leak-kinds="all" --track-origins="yes" --track-fds="yes" -s="" ./main="" matrix_1.txt="" matrix_2.txt="" if="" you="" want="" save="" the="" output/report="" of="" a="" valgrind="" to="" a="" text="" file,="" then="" use="" the="" following:="" valgrind="" --tool="memcheck" --leak-check="yes" --show-reachable="yes" –show—leak-kinds="all" --track-origins="yes" --track-fds="yes" –log-file="ast6memcheck.txt" -s="" ./main="" matrix_1.txt="" matrix_2.txt="" sample_output:="" please="" check="" the="" sample="" output,="" which="" is="" provided="" in="" a="" separate="" file.="" your="" program="" should="" match="" the="" sample="" output.="" the="" program="" has="" big="" menu="" and="" the="" menu="" should="" be="" displayed="" after="" each="" and="" every="" operation.="" however,="" you="" no="" need="" to="" worry="" about="" that,="" as="" the="" main.cpp="" includes="" that="" logic.="" in="" sample="" output,="" i="" will="" exclude="" the="" menu="" display="" after="" each="" operation="" to="" cut="" down="" the="" number="" of="" pages.="" but="" for="" checking="" the="" functionality="" of="" program,="" menu="" display="" can="" be="">
Oct 21, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here