You must Sign In to post a response.
  • Category: Programming

    C++ program to perform addition of two matrices using operator overloading


    Preparing for BCA exams? Are you having doubts and facing difficulty in writing C++ programs for the subject of Object Oriented Programming? Check out this page and get solution to performing addition of two matrices using operator overloading from ISC Experts.

    Following is a question that was asked in the question paper of BCA(May 2017) for subject Object Oriented Programming using C++(US-649).

    This question is asked for 7 marks and hence requires some explanation along with the program code. Please let me know the solution -
    Q) Write a C++ program to overload '+' operator to perform addition of two matrices ?[ 7 Marks]
  • Answers

    2 Answers found.
  • This is a C++ program suitable for you. The '+' operator is overloaded here.
    The program asks the user to enter the matrices to be added. Then it calls the member functions of the class Matrix to get the matrices and add them by the overloaded '+' operator.

    Problem Solution: You can use the following algorithm
    1. Define functions for get_matrix(), display_matrix(), and overload the '+' operator.
    2. Take user input for matrices.
    3. Decide on two variables of the Matrix type.
    4. Use the get_matrix() function to receive the matrix
    5. Use the display_matrix() function to display the matrices.
    6. Add them using the overloaded '+' operator.
    7. Print the result.

    drrao
    always confident

  • Operator overloading is a useful feature supported by Object Oriented Programming. Though class is a user defined data type user can easily perform those operations that can be performed on primary data type. So to support this concept Object Oriented Programming use the concept of Operator Overloading. Actually it is function generally called as operator op function. It's syntax is as follows

    return data type operator op(argument list);
    Here op is replaced by sign of operator which is to be overloaded. Operator function may be simple member function or friend function. While overloading binary operator using simple member function operator function requires only one argument and while overloading binary operator using friend function it requires two arguments.
    Following program is solved using simple member function.

    Please include iostream.h and conio.h files before declaration of class.
    //ADDITION OF TWO MATRIX USING OPERATOR OVERLOADING
    #include
    #include
    class matrix
    {
    private:
    int mat[2][2],add[2][2];
    public:
    void get(void)
    {
    for(int i=0;i<2;i++)
    {
    for(int j=0;j<2;j++)
    {
    cin>>mat[i][j];
    }
    }
    }
    void operator +(matrix a)
    {
    for(int i=0;i<2;i++)
    {
    for(int j=0;j<2;j++)
    {
    add[i][j]=mat[i][j]+a.mat[i][j];
    cout< }
    cout<<"\n";
    }
    }
    };
    void main(void)
    {
    class matrix p,q;
    clrscr();
    cout<<"\nEnter four elements of firt matrix\n";
    p.get();
    cout<<"\nEnter four elements of second matrix\n";
    q.get();
    cout<<"\nAddition of two matrices is as follows\n";
    p+q;
    getch();
    }

    In above program when the statement p+q appears in main function, compiler treat it as p.operator+(q).


  • Sign In to post your comments