#include #include // // The objective of this sample is to help understand the use of classes // How they can be used to simplify the code // // a class matrix that stores and initializes with rand(), an nxn matrix // the keyword class means we are declaring a class i.e. a user defined type. // the class name is Matrix class Matrix { // we keep a pointer to a pointer. // value is the address of an array of double pointers // *value (i.e. value[0]) is the address of the first pointer to double - i.e. pointer to array of doubles. // **value (i.e. value[0][0] is the double at location 0,0 // *(*(value + i) + j) (i.e. value[i][j]) is double at location i,j where i and j are integers. double ** value; int n; // a class typically not only stores data (see value above), but also supports operations through // methods and operators. These are made accessible to the users of this class by the keyword public // All declarations below the public are accessible, while the member variable value is private - it // cannot be accessed outside the class. public: // The class should have a constructor which takes care of initialization // Here we want to allocate memory to hold our matrix and initialize the // elements of the matrix randomly . Note that a constructor has no return // value Matrix(int _n) { n = _n; // note that new operator returns a pointer to double * i.e. an array of doubles. // so we have an array of array of double. value = new double * [n]; // only the pointers to array of doubles are allocated, they are not initialized yet. They // should be initialized with n pointers to array of n doubles each for( int i = 0; i < n; i++ ) { value[i] = new double [n] ; } // we want to initialize the array with a random number using a standard library random number // generator - see inclusion of stdlib at top - that allows us to use this function here srand48(time(0)); for( int i = 0; i < n; i++ ) for( int j = 0; j < n; j++ ) value[i][j] = drand48(); } // end of constructor. we have an nxn matrix initialized with random double values // the class has a destructor. This would be called automatically, and should free the // resources allocated by the constructor ~Matrix() { for( int i = 0; i < n; i++ ) delete [] value[i]; // free the value[i] = new double [n]; allocation delete [] value; // free the value = new double * [n]; allocation } // the matrix class can have functions so that users can use it. We define functions // here, one for printing the matrix, and other for calculating its transpose. // note the function has no return value - we must indicate that by the return type void void Print() { for( int i = 0; i < n; i++ ) { for( int j = 0; j < n; j++ ) std::cout << value[i][j] << " , " ; // print out the value and a , std::cout << std::endl ; // print out a new line at end of each row } } void Transpose() { double ** a = value; // note that another pointer to the same location can modify the values for( int i = 0; i < n; i++ ) for( int j = 0; j < i; j++ ) { double temp = a[i][j]; a[i][j] = a[j][i]; a[j][i] = temp; } } }; // usage of the class int main() { int n = 6; Matrix m(n); m.Print() ; m.Transpose(); std::cout << std::endl; m.Print(); return 0; }