using OOP to implement matrix class that provide the basic matrix operations

i want to know how i can solve this question? do i need to create a class or write the program codes, i dont understand it
What is the complete question you have been assigned?
the question is using oop implement a matrix class that provides the basic matrix operations (addition, subtraction, multiplication, Inverse, Transportation).
do i need to create a class or write the program codes

To create a class you write the program's code.

Anyway, using oop means that you have to create a class. That class should contain a matrix (meaning an array of arrays) of elements on which you will perform the calculations needed by matrix operations.
ok, thank you maeriden. I will try and it to you and see if there is any mistake, tanks
this is the solution, pls someone should help me with the correction, the question is using oop implement a matrix class that provides the basic matrix operations (addition, subtraction, multiplication, Inverse, Transportation).

#include <iostream>
using namespace std;
int main;

class matrix
{
int **p, m, n;
public:
matrix(int row = 2, int col = 2)
{
m = row;
n = col;
p = new(int *); m;
for (int i = 0; i < m; i++)
p[i] = new int[n];
}
~matrix()
{
for (int i = 0; i < m; i++)
delete p[i];
delete p;
}
void accept()
{
cout<<"Enter matrix elements:";
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cin >> p[i][j];
}
}
}
void display()
{
cout <<"The matrix is:";
for(int i = 0; i < m; i++)
{
cout <<endl;
for(int j = 0; j < n; j++)
{
cout << p[i][j] <<" ";
}
}
}
matrix operator +(matrix m2)
{
matrix T(m, n);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
T.p[i][j] = p[i][j] + m2.p[i][j];
}
}
return T;
}
friend matrix operator * (matrix, matrix);
};
matrix operator * (matrix a , matrix b)
{
if(a.n == b.m)
{
matrix T(a.m, b.n);
for(int i = 0; i < a.m; i++)
{
for(int k = 0; k < b.n; k++)
{
T.p[i][k] = 0;
for(int j = 0; j < a.n; j++)
{
T.p[i][k]+= a.p[i][j] * b.p[j][k];
}
}
}
return T;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
using namespace std;

class matrix
{ 
   int **p, m, n; 
public: 
   matrix(int row, int col) 
   { 
      m = row; 
      n = col; 
      p = new int*[m]; 
      for (int i = 0; i < m; i++) 
	 p[i] = new int[n]; 
   }
   ~matrix() 
   { 
      for (int i = 0; i < m; i++) 
	 delete p[i]; 
      delete p; 
   } 
   void accept() 
   { 
      cout<<"Enter matrix elements:"; 
      for(int i = 0; i < m; i++) 
      { 
	 for(int j = 0; j < n; j++) 
	 { 
	    cin >> p[i][j]; 
	 } 
      } 
   } 
   void display() 
   { 
      cout <<"The matrix is:";
      for(int i = 0; i < m; i++) 
      { 
	 cout <<endl; 
	 for(int j = 0; j < n; j++) 
	 { 
	    cout << p[i][j] <<" "; 
	 } 
      } 
   }
   matrix operator +(matrix m2)
   {
      matrix T(m, n);
      for(int i = 0; i < m; i++)
      {
	 for(int j = 0; j < n; j++)
	 {
	    T.p[i][j] = p[i][j] + m2.p[i][j]; 
	 } 
      }
      return T;
   }
   
   matrix operator= (matrix eq)
   {
      m = eq.m;
      n = eq.n;
      p = eq.p;
      
      return *this;
   }
   
   friend matrix operator * (matrix, matrix);
};

matrix operator* (matrix a , matrix b)
{
   matrix B(1,1);
   if(a.n == b.m)
   {
      matrix T(a.m, b.n);
      for(int i = 0; i < a.m; i++)
      {
	 for(int k = 0; k < b.n; k++)
	 {
	    T.p[i][k] = 0;
	    for(int j = 0; j < a.n; j++)
	    {
	       T.p[i][k]+= a.p[i][j] * b.p[j][k];
	    }
	 }
      }
      B = T;
   }
   return B;
}

int main()
{
   matrix rf(3,3);
}
Thank you smac89, it compile successfully but when i run the program it prints process exited with return value 0, pls what can i do?
You have to add more things for it to do:

Here is a revision of the previous, still has some bugs. I have to use friend operators instead of matrix operators to avoid overwritting valueable data. This can be easily. I will check back here tomorrrow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
using namespace std;

class matrix
{ 
   int **p, m, n; 
public: 
   matrix(int row, int col) 
   { 
      m = row; 
      n = col; 
      p = new int*[m]; 
      for (int i = 0; i < m; ++i)
      {
	 p[i] = new int[n];
	 for (int j = 0; j < n; ++j)
	    p[i][j] = 0;
      }
   }
   
//    ~matrix() 
//    { 
//       delete [] p;
//    }
   
   void accept() 
   { 
      cout<<"Enter matrix elements: "; 
      for(int i = 0; i < m; i++) 
      { 
	 for(int j = 0; j < n; j++) 
	 { 
	    cin >> p[i][j]; 
	 } 
      } 
   }
   
   void display() 
   { 
      cout <<"The matrix is:\n";
      for(int i = 0; i < m; i++) 
      { 
	 for(int j = 0; j < n; j++) 
	 { 
	    cout << p[i][j] <<" "; 
	 }
	 cout <<endl;
      } 
   }
   
   matrix& operator+ (matrix& lhs, const matrix& m1)
   {
      return (*this += m1);
   }
   
   matrix& operator* (const matrix& m1)
   {
      return (*this *= m1);
   }
   
   matrix& operator+= (const matrix& rhs)
   {
      for(int i = 0; i < m; i++)
      {
	 for(int j = 0; j < n; j++)
	 {
	    p[i][j] += rhs.p[i][j]; 
	 } 
      }      
      return *this;
   }
   
   matrix& operator*= (const matrix& T)
   {
      if(n == T.m)
      {
	 for(int i = 0; i < T.m; ++i)
	 {
	    for(int k = 0; k < n; ++k)
	    {
		 p[i][k] *= T.p[k][i];
	    }
	 }
      }
      
      return *this;
   }
   
   matrix& operator= (const matrix& T)
   {
      p = T.p;
      n = T.n;
      m = T.m;
      
      return *this;
   }
   
};


int main()
{
   matrix rf(3,3), ff(3,3);
   rf.accept();
   cout << "\nrf\n";
   rf.display();
   
   ff.accept();
   cout << "\nff\n";
   ff.display();
   
   cout << "\nff = ff + rf\n";
   ff = ff + rf;
   ff.display();
   
   cout << "\nff = (rf * ff)\n";
   ff = (rf * ff);
   ff.display();
   
   cout << "\nrf += ff\n";
   rf += ff;
   rf.display();
}
Last edited on
So you sent me a private msg last Friday asking me to do this assignment for you. I foolishly wrote the code on Saturday, but as you haven't replied to any of my messages, I haven't handed the code over. Good luck...
Last edited on
You could use a pseudomatrix
http://www.cplusplus.com/doc/tutorial/arrays/
See the "pseudo-multidimensional array" part.

I have made my own matrix class using a pseudo-multidimensional array
Last edited on
You could use a pseudomatrix
http://www.cplusplus.com/doc/tutorial/arrays/
See the "pseudo-multidimensional array" part.

I have made my own matrix class using a pseudo-multidimensional array


Yes, that's what I did with my solution, except I used a vector.
please im so sorry ajh32, i didnt receive any of your message, thank you @ eklavya sharma 2, scam89, and ajh32, i really appreciate your contributions, i will do it know, thanks once again
Topic archived. No new replies allowed.