Popular Posts

Wednesday, December 7, 2011

Simple Template Inheritance (Multiplication of two Matrices)

/* This is a simple matrix multiplication program
 * using Template inheritance.It take rows and columns of
 * two matrixes and dynamically create matrices arr1 and arr2
 * accoriding to the rows and columns user given
 * and finally add matrices arr1 and arr2 and show
 * the results i terminal.  
 */


#include <iostream>

using namespace std;

/* Template class First */
template<class T>
class First{
protected:
  int r1,c1;
  T **arr1;
 
public:
  /* First class constructor */
  First(int x,int y):r1(x),c1(y){
    arr1 = new T*[c1];
    for(int i = 0; i < r1; i++ )
      arr1[i] = new T[r1];
  }
};

/* Template class Second */
template<class T>
class Second{
protected:
  int r2,c2;
  T **arr2;
 
public:
  /* Second class constructor */
  Second(int x,int y):r2(x),c2(y){
    arr2 = new T*[c2];
    for(int i = 0; i < r2; i++ )
      arr2[i] = new T[r2];
   
  }
};

/* This class Result inherit the template classes
 * First and Second.
*/
template<class T>
class Result:public First<T>,public Second<T>{
public:
  Result(int x, int y, int c, int d):First<T>(x,y),Second<T>(c,d){
  }

  /* This getvalue function get values from the user
   * for the two matrices arr1 and arr2.
   */
  void getvalue()
  {
    cout<<"\nEnter Matrix1 elements one by one:\n";
    for(int i = 0; i < First<T>::r1; i++ )
      for(int j = 0; j < First<T>::c1; j++)
    cin >> First<T>::arr1[i][j];
   
    cout<<"\nEnter Matrix2 elements one by one:\n";
    for(int i = 0; i < Second<T>::r2; i++ )
      for(int j = 0; j < Second<T>::c2; j++)
    cin >> Second<T>::arr2[i][j];
   
  }
 
  /* This print function print the matrices*/
  void print(){
    cout<<"\n--------Matrix1 elements ---------\n";
    for(int i = 0; i < First<T>::r1; i++ ){
      for(int j = 0; j < First<T>::c1; j++)
    cout << First<T>::arr1[i][j]<<"\t";
      cout<<"\n";
    }
   
    cout<<"\n--------Matrix2 elements ---------\n";
    for(int i = 0; i < Second<T>::r2; i++ ){
      for(int j = 0; j < Second<T>::c2; j++)
    cout << Second<T>::arr2[i][j]<<"\t";
      cout<<"\n";
    }
   
  }

  /* This function multiply two matrices arr1 and arr2 and display
   * the result.  
   */
  void multiply()
  {
    if(First<T>::c1 == Second<T>::r2){
      cout<<"\nResultant Matrix after multiplication\n";
      T x ;
      for(int i = 0; i < First<T>::r1; i++){
    for(int j = 0; j < Second<T>::c2; j++){
      x = 0;
      for(int k = 0; k < First<T>::c1; k++)
        x += First<T>::arr1[i][k] * Second<T>::arr2[k][j];
      cout<<x<<"\t";
    }
    cout<<"\n";
      }
    }else{
      cout<<"Wrong Matrixes for multiplication \n pls cheke rows and colums of the two matxices";
    }
  }
 
};

int main()
{
  int x,y,c,d;
  cout<<"\nEnter Matrix1 row and col:";
  cin>>x>>y;
  cout<<"\nEnter Matrix2 row and col:";
  cin>>c>>d;
  Result<float> Rs(x,y,c,d);
  Rs.getvalue();
  Rs.print();
  Rs.multiply();
  getch();
}

No comments:

Post a Comment