Hi. There's something about classes that i want to ask.
How to access the private member in classes.
As i know, the only way to access private members is to use the classes name as data type.
For example, i want to create a matrix calculator program:
class Matrix
{
public:
//constructor, destructor, and other functions
private:
int row; //row of the matrix
int col; //column of the matrix
void **m; //2 dimensional array for matrix
}
when i want to access the private members from the function below, it won't compile.
and the error message is 'row' was not declared in this scope, 'col' was not declared in this scope.
Matrix::Matrix multiplyMatrix (const Matrix& mtx)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
m[i][j] *= m[i][i];
]
}
}
in addition, i have made another function before, to copy the constructor, using the same data type, which is the class name and it didn't show any error message at all when i compiled it.
here is the function:
how can i solve this problem?
and what make the different between those 2 function?
with the same data type, but one can access the private member while the other one cannot.
my knowledge about classes isn't much.
the only way to access to the private member is only the one that i stated above.
or there's another way to access the private member? please tell me.
Private members can be accessed only from member functions.
I think your problem is with defining member function.
Write Matrix Matrix::multiplyMatrix (const Matrix& mtx).
Here Matrix it the return type and Matrix::multiplyMatrix is function name.
class Matrix
{
public:
//constructor, destructor, and other functions
private:
int row; //row of the matrix
int col; //column of the matrix
void **m; //2 dimensional array for matrix
}
To Disch:
Hmm. I typed the wrong one for that. LOL.
This is a part of my assignment, the first one is using int **m, and the 2nd one required me to use the void***m, i think this one will be confusing me more than the int **m.
Sorry for the typing error.
Thanks. :)
To DrChill:
Thanks.
Sorry if the code looked bad without tabs and colors.
Yesterday i didn't know how to use the [code.][/code.].
(i put a dot after 'code' to make sure it don't run)
Thanks. :)
To Hamsterman:
Geee.. Got it!! Thanks!!
Thanks, hamsterman. :)