class MatrixNxN { public: int N; float *A; ... } |
Public member variables and member functions can be accessed by any function where the class variables is visible ("in scope").
class MatrixNxN { private: int N; float *A; ... } |
class MatrixNxN { private: float *A; int N; .... } |
int main(int argc, char *argv[]) { int N = 4; MatrixNxN M(N); int i, j, k; k = 1; for (i = 0; i < N; i = i + 1) for (j = 0; j < N; j = j + 1) { M.A[i*N+j] = k; // Error: inaccessible !!! k = k + 1; } } |
 
  You have absolute control on WHAT operations will be allowed on member variables that are private   |
class MatrixNxN { private: float *A; int N; .... } int main(int argc, char *argv[]) { int N = 4; MatrixNxN M(N); int i, j, k; k = 1; for (i = 0; i < N; i = i + 1) for (j = 0; j < N; j = j + 1) { M.A[i*N+j] = k; k = k + 1; } } |
class MatrixNxN { private: float *A; int N; public: void InitMatrix(float B[]) { int i; for (i = 0; i < N*N; i = i + 1) A[i] = B[i]; } .... } int main(int argc, char *argv[]) { int N = 4; MatrixNxN M(N); float B[16]; int i, j, k; k = 1; for (i = 0; i < N; i = i + 1) for (j = 0; j < N; j = j + 1) { B[i*N+j] = k; k = k + 1; } M.InitMatrix(B); } |
class MatrixNxN { private: float *A; int N; public: void InitMatrix(float B[]) { int i; for (i = 0; i < N*N; i = i + 1) A[i] = B[i]; } .... } int main(int argc, char *argv[]) { int N = 4; MatrixNxN M(N); float B[16]; int i, j, k; k = 1; for (i = 0; i < N; i = i + 1) for (j = 0; j < N; j = j + 1) { B[i*N+j] = k; k = k + 1; } M.InitMatrix(B); } |
class MatrixNxN { private: float *A; int N; public: /* ----------------- Constructor(int) ----------------- */ MatrixNxN(int n) { A = new float[n*n]; N = n; } /* ------------------------- Constructor(int, float[]) ------------------------- */ MatrixNxN(int n, float B[]) { int i; A = new float[n*n]; N = n; for (i = 0; i < N*N; i = i + 1) A[i] = B[i]; } .... } int main(int argc, char *argv[]) { int N = 4; MatrixNxN M(N); // Invoke Matrix(int) float B[16]; int i, j, k; k = 1; for (i = 0; i < N; i = i + 1) for (j = 0; j < N; j = j + 1) { B[i*N+j] = k; k = k + 1; } MatrixNxN Q(N, B); // Invoke Matrix(int, float[]) } |
class MatrixNxN { private: float *A; int N; friend myFunction(...); // Non-member friend function friend myClass::myFunction(...); // Member friend function } |
Usually, the same author would be writing the friend function !
So it is not considered a "privacy violation"...
cout << "Hello World !"; |
C++ looks for:
operator<<(ostream, char *); or ostream::operator<<(char *); |
(cout is of the type ostream and "Hello World !" is of the type (char *))
C++ has generously provide a class ostream that included the member function ostream& ostream::operator<< (char *)
MatrixNxN M; cout << M; |
C++ looks for:
operator<<(ostream, MatrixNxN); or ostream::operator<<(MatrixNxN); |
Declare a friend operator function in MatrixNxN:
class MatrixNxN { private: float *A; int N; friend operator<<(ostream, MatrixNxN); } |
Possibly inside another file - implement the friend function:
ostream & operator<<(ostream & cout, MatrixNxN M) { int i, j; for (i = 0; i < M.N; i = i + 1) { for (j = 0; j < M.N; j = j + 1) cout << M.A[i*M.N+j] << "\t"; cout << "\n"; } cout << "\n"; }; |
Now you can output a MatrixNxN variable with <<:
cout << M; |
Because operator<<() returns the ostream object, you can even cascade
the output operator:
cout << "Matrix M is:" << endl << M << endl; |
MatrixNxN M; VectorN v1, v2; v2 = M * v1; |
C++ will look for the following member function:
MatrixNxN::operator*(VectorN);Because matrix and vector frequently inter-operate, it would be beneficial to allow every function in MatrixNxN to access private variables in VectorN |
The assignment operator inside VectorN can handle the assignment !
The Vector3 class:
class Vector3 { private: float x[3]; public: /* -------------------- Default constructor -------------------- */ Vector3() { int i; for (i = 0; i < 3; i=i+1) x[i] = i + 10; } friend ostream & operator<<(ostream &, Vector3 &); friend class Matrix3; // Whole class as friend ! }; |
You need to separate each class by writing a class definition file and a class implementation file:
Anyone with an I.Q. higher than a chimp will be able to access the private variables in a class
class MatrixNxN { private: int N; float *A; ... } class myMatrixNxN { public: int N; float *A; ... } |
int main(int argc, char *argv[]) { int N = 4; MatrixNxN M(N); myMatrixNxN *X; X = (myMatrixNxN *) &M; X->A[i] = .... you can access private variables !!!... } |