#pragma once
#include<stdio.h>
usingnamespace std;
template <class T>
class Matrica
{
int vrste, kolone;
T **p;
public:
Matrica();
Matrica(int a, int b);
~Matrica();
Matrica<T>& operator=(const Matrica& m);
Matrica<T> GetElement(int x, int y){ returnthis->p[x][y]; }
void SetElement(int x, int y, T newValue){ this->p[x][y] = newValue; }
int GetX() { return vrste; }
int GetY() { return kolone; }
friend Matrica operator*(const Matrica&a, const Matrica &b);
};
template<class T>
Matrica<T>::Matrica()
{
vrste = kolone = 12;
p = new T*[12];
for (int i = 0; i < 12; i++)
{
p[i] = new T[12];
for (int j = 0; j < 12; j++)
p[i][j] = 0;
}
}
template<class T>
Matrica<T>::Matrica(int a, int b)
{
vrste = a;
kolone = b;
p = new T*[a];
for (int i = 0; i < a; i++)
{
p[i] = new T[b];
for (int j = 0; j< b;j ++)
p[i][j] = 0;
}
}
template<class T>
Matrica<T>::~Matrica() {
for (int i = 0; i < vrste; i++)
delete[] p[i];
delete[] p;
}
template<class T>
Matrica<T>& Matrica<T>::operator=(const Matrica<T> & m)
{
for (int i = 0; i < vrste; i++)
delete[] p[i];
delete[] p;
kolone = m.kolone;
vrste = m.vrste;
p = new T*[vrste];
for (int i = 0; i < vrste; i++)
p[i] = new T[kolone];
for (int i = 0; i < vrste; i++)
for (int j = 0; j< kolone; j++)
p[i][j] = m.p[i][j];
return *this;
}
template<class T>
Matrica<T> operator*(const Matrica<T> &a, const Matrica<T>&b)
{
int i, j,k;
if (a.vrste != b.kolone)
cout << "Matrice se ne mogu pomnoziti" << endl;
else {
Matrica<T> c;
c.p = new T*[a.vrste];
for (i = 0; i < a.vrste; i++)
{
c.p[i] = new T[b.kolone];
for ( j = 0; j <b.kolone; j++)
c.p[i][j] = 0;
}
for (i = 0; i < a.vrste; ++i)
for (j = 0; j < b.kolone; ++j)
for (k = 0; k < a.kolone; ++k)
{
c.p[i][j] += a.p[i][k] * b.p[k][j];
}
return c;
}
}
Source.cpp
1 2 3 4 5 6 7 8 9
#include"Complex.h"
#include"Matrica.h"
void main()
{
Matrica<int> matrica1;
Matrica<Complex> matrica2(4, 7);
int x = matrica1.GetElement<int>(2, 5);//Mistake ? how to do it
system("pause");
}