Question
This is in c++ program. Please follow instructions and take a picture of your code for proof that it works. Thanks matrix.h, matrix.cpp, and tempMain.cpp
This is in c++ program. Please follow instructions and take a picture of your code for proof that it works. Thanks matrix.h, matrix.cpp, and tempMain.cpp.
-Add the preprocessor directives to matrix.h and matrix.cpp
-Template the definition of the Matrix class in matrix.h.
-Template the member functions in matrix.cpp.
-Add two more 2D arrays to tempMain.cpp that hold a new data type.
-Create an instances of Matrix that can hold that data type and use the templated demoTemplate function to show that your templated class works.
-Test your changes.
Here are the codes:
*** Preprocessor ***
If you try to define templates in multiple files, it is likely that you will come across a linker error in Visual C++. The error might be something like the following:
error LNK2019: unresolved external symbol "public: __thiscall my_class ::my_class(int,int)" (??0?$my_class@H@@QAE@HH@Z)
To get rid of this error, you need to add the following at the end of .cpp file, so at the end of the swapper.cpp, add the following:
template class my_class; template class my_class ; template class my_class ;
*** matrix.h ***
const int MAXROWS=2; const int MAXCOLS=3;
class Matrix { private: int doubleArray[MAXROWS][MAXCOLS]; int rows; int cols;
public: //Constructor Matrix(); //Set rows to MAXROWS and cols to MAXCOLS //Initialize all the values of doubleArray to zero //Getter Functions void printMatrix();
//Setter Functions void setMatrix(int [][MAXCOLS]); //set the doubleArray to what is sent void addMatrix(int [][MAXCOLS]); //add an array to doubleArray void addMatrix(Matrix otherMatrix);
//No destructor needed };
*** matrix.cpp ***
#include "matrix.h" #include
Matrix::Matrix() { rows = MAXROWS; cols = MAXCOLS; for (int i=0; i
void Matrix::printMatrix() { for (int i=0; i
void Matrix::setMatrix(int otherArray[][MAXCOLS]) { for (int i=0; i
void Matrix::addMatrix(int otherArray[][MAXCOLS]) { for (int i=0; i
void Matrix::addMatrix(Matrix otherMatrix) { addMatrix(otherMatrix.doubleArray); }
*** tempMain.cpp ***
#include
using namespace std;
template
int main() { string Str1[MAXROWS][MAXCOLS] = { {"Congra", "y", "ar"}, {"alm", "don", "La"} }; string Str2[MAXROWS][MAXCOLS] = { {"tulations", "ou", "e"}, {"ost", "e the", "b!"} }; int Num1[MAXROWS][MAXCOLS] = { {1,2,3}, {4,5,6} }; int Num2[MAXROWS][MAXCOLS] = { {6,5,4}, {3,2,1} };
Matrix
cout
cout
cout
template M.addMatrix(array2); cout Here is the sample output.
Step 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