#include <iostream>
usingnamespace std;
class vector
{
private:
int size;
int *p;
friendclass multiply;
public:
vector(int a)
{
size = a;
p = newint[a];
}
int getsize()
{
return size;
}
void input()
{
for (int i = 0; i < size; i++)
{
int x;
cout << "Enter values" << endl;
cin >> x;
p[i] = x;
}
}
void output()
{
for (int i = 0; i < size; i++)
{
cout << p[i] << " ";
}
cout << endl;
}
int getelement(int i)
{
return p[i];
}
};
class matrix
{
private:
int row;
int col;
int **r;
friendclass multiply;
public:
matrix(int s, int a)
{
row = s;
col = a;
r = newint*[s];
for (int i = 0; i < s; i++)
{
r[i] = newint[a];
}
}
int getrow()
{
return row;
}
int getcol()
{
return col;
}
void input()
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
int x;
cout << "Enter values for matrix" << endl;
cin >> x;
r[i][j] = x;
}
}
}
void output()
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
cout << r[i][j] << " ";
}
cout << endl;
}
}
int getelement(int i, int j)
{
return r[i][j];
}
};
class multiply
{
private:
int r;
int c;
int **a;
public:
multiply(int s, int a)
{
r = s;
c = a;
a = newint*[s];
for (int i = 0; i < s; i++)
{
a[i] = newint[a];
}
}
void input()
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
a[i][j] = 0;
}
}
}
for (int i = 0; i < matrix.r; i++)
{
if (matrix.row != vector.p)
{
cout << "Can't be multiplied" << endl;
}
else
{
for (int j = 0; j < matrix.col; j++)
{
for (int k = 0; k < matrix.row; k++)
{
a[i][k] += matrix.r[i][j] * vector.p[k];
}
}
}
}
};
void main()
{
int s, r;
cout << "Enter size" << endl;
cin >> s;
vector c(s);
c.input();
c.output();
cout << "Your element is:" << c.getelement(5) << endl;
cout << "Enter number of rows" << endl << ;
cin >> r;
matrix b(r, 1);
b.input();
b.output();
cout << "Your element is:" << b.getelement(1, 0) << endl;
}
This code is supposed to multiply the vector by the matrix, I have to use friend class and I'm not exactly sure I used it correctly. Can someone help me? And how do I call it in the main function?? Please, I need help with it
In lines 125 to 137, you're using matrix like it's the name of an object, but I don't see any object with that name defined anywhere.
Did you intend for there to be an instance of the class matrix in there somewhere?
In fact... lines 125 - 141 look as though they're not inside any method or function. The input() function ends at 124, and then there's a "for" statement immediately afterwards. You can't do that.
I didn't want to use it as an object, I meant that the code should take row from class matrix and use it in class multiply (since there is a friend class)
But again, I'm not sure about how friend class works and how do I use it.
Do you know how can I rewrite my code using friend class?
Making multiply a friend class of matrix, just means that methods of multiply can access the private members of matrix objects. A method of multiply will still need to have a matrix object to operate on, just as it would if it wasn't a friend class and was having to use the public interface of matrix instead.