OOPc using C++

With this blog I want to share my knowledge of OOPs and C++. I Hope this can quench your thirst for learning.Any suggestions and comments are welcome.
Showing posts with label Function overloading. Show all posts
Showing posts with label Function overloading. Show all posts

Wednesday, August 4, 2010

Program 6 -Operator Overloading illustrated by Matrix Addition,Subtraction and Multiplication Program

The following program implements Function Overloading by doing addition,subtraction and multiplication of Matrices.

#include<iostream.h>
#include<iomanio.h>


const int MAXROW=3,MAXCOL=3;
struct matrix
{
         int arr[MAXROW][MAXCOL];
};


matrix operator+(matrix a,matrix b);
matrix operator-(matrix a,matrix b);
matrix operator*(matrix a,matrix b);
void mat_print(matrix p);


void main( )
{
           matrix a = {
                                    1,2,3,
                                    4,5,6,
                                    7,8,9,
                              };
           matrix b = {
                                     1,2,3,
                                     4,5,6,
                                     7,8,9,
                               };


           matrix c,d,e,f;


          c=a+b;
          d=a*b;
          e=a+b*c;
          f=a-b*c+d;


          mat_print(c);
          mat_print(d);
          mat_print(e);
          mat_print(f);


}


matrix operator+(matrix a,matrix b)
{
          matrix c;
          int i,j;


          for(i=0;i<MAXROW;i++)
          {
                        for(j=0;j<MAXROW;j++)
                                c.arr[i][j] = a.arr[i][j] + b.arr[i][j];
            }
         return c;
}



matrix operator-(matrix a,matrix b)



{
          matrix c;
          int i,j;

          for(i=0;i<MAXROW;i++)
          {
                        for(j=0;j<MAXROW;j++)
                                c.arr[i][j] = a.arr[i][j] - b.arr[i][j];
            }
         return c;
}



matrix operator*(matrix a,matrix b)



{
          matrix c;
          int i,j,k;

          for(i=0;i<MAXROW;i++)
          {
                        for(j=0;j<MAXROW;j++)
                          {
                                     c.arr[i][j]=0;
                                     for(k=0;k<MAXCOL;k++)      
                                                    c.arr[i][j] + = a.arr[i][k] * b.arr[k][j];
                            }
            }
          return c;
}

void mat_print(matrix p)
{
            int i,j;

            cout<<endl<<endl;
            for(i=0;i<MAXROW;i++)
             {
                          cout<<endl;
                          for(j=0;j<MAXCOL;j++)
                                    cout<<setw(5)<<p.arr[i][j];
               }
}

Program 5- Program to demonstrate Function Overloading.

The program returns the absolute value of an argument.

#include<iostream.h>
void main( )
{
        int i=-25,j;
        long l=-100000L,m;
        double d=-12.34,e;
        int abs(int);
        long abs(long);
        double abs(double);


        j=abs(i);
        m=abs(l);
        e=abs(d);


        cout<<endl<<j<<endl<<m<<e;
}


int abs(int ii)
{
         return(ii>0?ii:ii*-1);
}
long abs(long ll)
{
         return(ll>0?ll:ll*-1);
}
double abs(double dd)
{
         return(dd>0?dd:dd*-1);
}


Click here to read about Function Overloading