by Dinesh Thakur Category: Classes in C++

 

#include<iostream.h>

#include<conio.h>
class mat
{
      private:
                    int s[10][10];
                    int u,v;
      public:
                    void show();
                    mat operator +(mat);
                    mat operator *(mat);
                    void read();
};
           mat mat::operator+(mat uu2)
      {
               mat t;
               t.u=u;
               t.v=v;
               cout<<t.u;
               cout<<t.v;
               for(int i=0;i<t.u;i++)
                    for(int j=0;j<t.v;j++)
                         t.s[i][i]=s[i][i]+uu2.s[i][i];
                         return t;
      }
          mat mat::operator*(mat uu2)
     {
               mat t;
               t.u=u;
               t.v=uu2.v;
               for(int i=0;i<t.u;i++)
                    for(int j=0;j<t.v;j++)
                        {
                           t.s[i][i]=0;
                           for(int k=0;k<v;k++)
                                  t.s[i][j]+=s[i][k]*uu2.s[k][j];
                        }
                                  return t;
       }
           void mat::read()
       {
              cout<<"Enter Size of Matrix like 3 x 3:\n";
              cin>>u>>v;
              cout<<"Enter the Elements of Matrix :\n";
              for(int i=0;i<u;i++)
                   for(int j=0;j<v;j++)
                       cin>>s[i][j];
     }
             void mat::show()
     {
             for(int i=0;i<u;i++)
                  {
                  for(int j=0;j<v;j++)
                       {
                             cout<<s[i][j]<<"\t";
                            
                        }
                             cout<<"\n";
                  }
     }
             void main()
     {
                mat obj1 ,obj2,obj3;
                clrscr();
                cout<<"Enter First Matrix\n";
                obj1.read();
                cout<<"Enter Second Matrix\n";
                obj2.read();
                obj3=obj1 +obj2;
                cout<<"Result After Addition of two Matrix\n";
                obj3.show();
                obj3=obj1 *obj2;
                cout<<"Result After Multiplication of two Matrix\n";
                obj3.show();
                getch();
     }

Addition & Multiplication Of Two Matrices By Overloading + And * Operators



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles