Submitted Questions

  • Arithmetic Operations using Operator Overloading

    Write a program for all the four arithmetic operations in float data type using operator overloading concept.

  • Matrix Operation using Operator Overloading

    Write a program for four atrithmetic operations in matrix using operator overloading.

    JoolsL

    • Nov 19th, 2012

    In both the above answers the op overloads should be using a const ref as input. You should have a (private) ctor that makes the matrix from the data, and should use RVO in the return.

    binoy

    • Aug 23rd, 2012

    Code
    1. #include<iostream.h>
    2. #include<conio.h>
    3. class matrix
    4. {
    5.   private:long m[5][5];
    6.   int row;int col;
    7.   public:void getdata();
    8.   int operator ==(matrix);
    9.   matrix operator+(matrix);
    10.   matrix operator-(matrix);
    11.   friend ostream & operator << (ostream &,matrix &);
    12. };
    13. /* function to check whether the order of matrix are same or not */
    14. int matrix::operator==(matrix cm)
    15. {
    16.   if(row==cm.row && col==cm.col)
    17.   {
    18.     return 1;
    19.   }
    20.   return 0;
    21. }
    22. /* function to read data for matrix*/
    23. void matrix::getdata()
    24. {
    25.   cout<<"enter the number of rows
    26. ";
    27.   cin>>row;
    28.   cout<<"enter the number of columns
    29. ";
    30.   cin>>col;
    31.   cout<<"enter the elements of the matrix
    32. ";
    33.   for(int i=0;i<row;i++)
    34.   {
    35.     for(int j=0;j<col;j++)
    36.     {
    37.        cin>>m[i][j];
    38.     }
    39.   }
    40. }
    41. /* function to add two matrix */
    42. matrix matrix::operator+(matrix am)
    43. {
    44.   matrix temp;
    45.   for(int i=0;i<row;i++)
    46.   {
    47.     for(int j=0;j<col;j++)
    48.     {
    49.       temp.m[i][j]=m[i][j]+am.m[i][j];
    50.     }
    51.     temp.row=row;
    52.     temp.col=col;
    53.   }
    54.   return temp;
    55. }
    56. /* function to subtract two matrix */
    57. matrix matrix::operator-(matrix sm)
    58. {
    59.   matrix temp;
    60.   for(int i=0;i<row;i++)
    61.   {
    62.     for(int j=0;j<col;j++)
    63.     {
    64.       temp.m[i][j]=m[i][j]-sm.m[i][j];
    65.     }
    66.     temp.row=row;
    67.     temp.col=col;
    68.   }
    69.   return temp;
    70. }
    71. /* function to display the contents of the matrix */
    72. ostream & operator <<(ostream &fout,matrix &d)
    73. {
    74.   for(int i=0;i<d.col;i++)
    75.   {
    76.     for(int j=0;j<d.col;j++)
    77.     {
    78.       fout<<d.m[i][j];
    79.       cout<<" ";
    80.     }
    81.     cout<<endl;
    82.   }
    83. return fout;
    84. }
    85. /* main function */
    86. void main()
    87. {
    88.   matrix m1,m2,m3,m4;
    89.   clrscr();
    90.   m1.getdata();
    91.   m2.getdata();
    92.   if(m1==m2)
    93.   {
    94.     m3=m1+m2;
    95.     m4=m1-m2;
    96.     cout<<"Addition of matrices
    97. ";
    98.     cout<<"the result is
    99. ";
    100.     cout<<m3;
    101.     cout<<"subtraction of matrices
    102. ";
    103.     cout<<"The result is
    104. ";
    105.     cout<<m4;
    106.   }
    107.   else
    108.   {
    109.     cout<<"order of the input matrices is not identical
    110. ";
    111.   }
    112.   getch();
    113. }