matrix class

hello i really need help, my code is a class matrix that should a matrix multiplication .i am not familiar with classes .When ever i run the code an error with come up at the private function.

This is my code please help.


#include <iostream>
#include <iostream>
#include <fstream>

using namespace std;



class cat { friend istream& operator>>(istream &a,cat &b);
friend ostream&operator <<(ostream&a, cat &b);

private:
int row,col,*data;


public: cat(){ row = col = 0; data = 0; }

cat(int r, int c) {row = r; col = c; data = new int [row*col];}
cat(cat &a ) { row=a.row; col = a.col; data = new int [row*col];
cat operator*(const cat& b);
for (int i=0; i< row*col; i++) data[i]=a.data[i];

}
~cat()
{
if (data !=0) delete [] data;
}


void showMatrix()
{
int w=0, j;

for (int i = 0; i<row; i++)
{
for (j=0; j<col; j++)

{
cout<<data[w] << ' '; w++;
}

cout<<endl;
}

cout <<endl;
}
};



cat product(cat &a, cat &b)
{
cat tmp( a.row, b.col);
int s, i, j;

for (i=0; i<a.row; i++)
for (j=0; j<b.col; j++)
for (tmp.data[i*b.col+j] = 0, s=0; s<a.col; s++)

tmp.data[i*b.col+j] +=a.data[i*a.col+s]*b.data[s*b.col+j];
return tmp;

}

cat cat::operator*(cat& a){
cat product(row, col);
int n=0,k;
for (int i=0; i<row; i++)
for(int j=0; j<col; j++)
for(k = 0; k < col; k++)
{n=i*col+k;
product(i,j) += *(data+n) * a(k,j);

}
return product;
}


int main() { int m, n ,k;

cout<< "enter row no. of the matrix A: "; cin >>m;
cout<<"enter column no. of the matrix A: "; cin >>n;
cout<<"enter column no. of the matrix B: "; cin >>k; cat a (m, n); cat b(n, k);

cat a,b;
cin>>a>>b;
cout<<a<<b;
cat c;
c=a*b;
cout<<c;

system("pause");
return 0;

}


Code tags.
why do you have two #include <iostream> you only need one
and you need to have main at the top to declare void showmartix()
the top of your program should be
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <fstream>

using namespace std;

void showmatrix();
int main()
{
.....
Last edited on
Topic archived. No new replies allowed.