Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 using namespace std;

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 #include #include "matrix.h"

using namespace std;

template void demoTemplate(Matrix& M, T1 array1[][MAXCOLS], T1 array2[][MAXCOLS]);

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 stringMatrix; Matrix intMatrix;

cout

cout

cout

template void demoTemplate(Matrix& M, T1 array1[][MAXCOLS], T1 array2[][MAXCOLS]) { M.setMatrix(array1); cout

M.addMatrix(array2); cout

Here is the sample output.

image text in transcribed

Demonstrating with string matrix: Matrix set to first array Congra y ar alm don La Matrix incremented by second array Congratulations you are almost done the Lab! Demonstrating with int matrix: Matrix set to first array Matrix incremented by second array Demonstrating with float matrix: Matrix set to first array 1.6 2.5 3.4 4.3 5.2 6.1 Matrix incremented by second array 7.7 7.7 7.7 7.77.7 7.7 Demonstrating with string matrix: Matrix set to first array Congra y ar alm don La Matrix incremented by second array Congratulations you are almost done the Lab! Demonstrating with int matrix: Matrix set to first array Matrix incremented by second array Demonstrating with float matrix: Matrix set to first array 1.6 2.5 3.4 4.3 5.2 6.1 Matrix incremented by second array 7.7 7.7 7.7 7.77.7 7.7

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago