The exercise consisted of implementing the operations for a non-generic matrix TAD , once everything has been implemented correctly, with 'templates' you have to make the matrix TAD generic and accept any valid dimension of rows and columns, as well as any valid data type .
Here is the summary code:
In the header:
#ifndef MATRIZ_HPP
#define MATRIZ_HPP
#include <stdexcept>
#include <iostream>
#include <iomanip>
#define F 3
#define C 3
class Matriz
{
public:
// Definición de las clases de excepciones
class ECoordenadasIncorrectas: public std::runtime_error
{
public:
ECoordenadasIncorrectas(const std::string& w = "ECoordenadasIncorrectas"): std::runtime_error(w) {}
};
// Declaración de la interfaz del TAD Matriz<E,F,C>
Matriz();
~Matriz();
void identidad();
void ceros();
void unos();
void multiplicar(Matriz m, Matriz& salida);
void sumar(Matriz m, Matriz& salida);
void restar(Matriz m, Matriz& salida);
void trasponer(Matriz& salida);
int filas();
int columnas();
float valor(int i, int j);
void asignar(int i, int j, float v);
template <typename ModificaElemento>
void modificar(ModificaElemento modifica_elemento);
Matriz& operator=(Matriz& m);
/** Constructor de copia */
Matriz(Matriz& m);
bool operator==(Matriz& m);
bool operator!=(Matriz& m);
private:
float elementos_[F][C];
};
#include "matriz.cpp"
#include "matriz_io.cpp"
#endif // MATRIZ_HPP
And in the .cpp with the code of the operations (I don't put all of them so as not to make it long):
#include <limits>
#include <cmath>
// COMPLEJIDAD: O(F*C)
Matriz::Matriz()
{
for(int i=0; i<F; i++)
{
for(int j=0; j<C; j++)
{
elementos_[i][j] = 0; // Valor int, C++ lo convierte automáticamente a E.
}
}
}
template <typename ModificaElemento>//esta viene ya con template, pero no la del TAD
void Matriz::modificar(ModificaElemento modifica_elemento)
{
for(int i=0; i<F; i++)
{
for(int j=0; j<C; j++)
{
elementos_[i][j] = modifica_elemento(elementos_[i][j]);
}
}
}
Matriz& Matriz::operator=(Matriz& m)
{
for(int i=0; i<F; i++)
{
for(int j=0; j<C; j++)
{
elementos_[i][j] = m.elementos_[i][j];
}
}
return (*this);
}
How do I create the template and make the TAD generic?
I know that for example in the class you have to add:
template<typename E, int F, int C>//añadido para el tipo generico y el numero de filas y columnas class Matriz { public: //codigo private: //codigo };
But I don't know how to modify the .cpp operations to make them generic.
PS: I don't think I need to attach the test code for the operations, but if someone who answers thinks it is necessary, I'll edit the question to add it.
An initial note, in C++ it is customary to use
const [tipo]
instead of#define
.#define
It has no type and that can lead to errors that are difficult to locate.Ok, now speaking of your template. The templates have several peculiarities (I'll list a couple of them):
That said, the first thing you need to do is move the entire implementation to the header. You can do this by cutting and pasting or adding an include to the cpp:
In either case the result will be the same and allows the compiler to find the source code of the template.
Now how to deal with generic types? It is done exactly the same as with concrete types. I give you some examples:
Declaration of the class and the internal array:
Builder:
Methods that indicate the rows and columns:
Assignment operator
Note that, in this case, you will only be able to make assignments on templates of exactly the same type: