Did I use friend class correctly?

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
using namespace std;
class vector
{
private:
	int size;
	int *p;
	friend class multiply;
public:
	vector(int a)
	{
		size = a;
		p = new int[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;
	friend class multiply;
public:
	matrix(int s, int a)
	{
		row = s;
		col = a;
		r = new int*[s];
		for (int i = 0; i < s; i++)
		{
			r[i] = new int[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 = new int*[s];
		for (int i = 0; i < s; i++)
		{
			a[i] = new int[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.
Last edited on
Topic archived. No new replies allowed.