Explain friend for overloading operators, C/C++ Programming

Assignment Help:

Friend for Overloading Operators

Sometimes friend functions cannot be avoided. For example with the operator overloading. Consider the following class that have data members to simulate a matrix. Several operations can be performed on the matrices. One of them is to multiply the given matrix by a number (constant literal). There are two ways in which we can do this. The two ways are:

                Matrix * num;

 

   [or]

num * Matrix;

In the first case we can overload * to perform the operation and an object invokes this as the statement gets changed to :

Mobj.operator*(num);

Where Mobj is an object of Matrix and num is a normal integer variable. What happens to the second one ? It  gets changed by the compiler as :

num.operator*(Mobj);

Let us see this program in detail.

class Matrix

{

  public:

:

                                :

                                Matrix &operator*(int num);

                                friend Matrix &operator*(int n, Matrix &m);

 private:

                                int mat[20][20];

                                int rows, cols;

}

 

Matrix Matrix::operator*(int num)

 {

  Matrix temp;

  temp.rows=rows;

  temp.cols=cols;

 

   for(int i=1; i<=rows; i++)

                for(int j=1; j<=cols; j++)

                                temp.mat[i][j]=mat[i][j]*num;

    return (temp);

 

 }

 

Matrix operator*(int n, Matrix &m)

{

   Matrix temp;

 

                temp= m*n;

                return temp;

}

 

void main()

 {

Matrix M1, M2, M3;

int num;

  :

  :                             // accept matrix one and num

 

M2=M1*num;  // calls member operator function.

M3=num*M1;  // calls friend function.

 }

 


Related Discussions:- Explain friend for overloading operators

Explain the symbolic constants in c language, Explain the Symbolic Constant...

Explain the Symbolic Constants in c language? Symbolic constants are the constants of any type that declared by using the #define compiler directive and it is a preprocessor di

C++ programming, Write a program to convert English units to metric (e.g., ...

Write a program to convert English units to metric (e.g., miles to kilometers, gallons to liters, etc.). Include a specification and a code design.

C program to handle stack using exception handling, Aim: To implement a pr...

Aim: To implement a program to handle stack overflow, underflow and odd number exception using Exception Handling. Code: #include #include #include #define MAX

Prime no., program to find if a no . is prime or not

program to find if a no . is prime or not

Boardcoloring, in this problem u given a board in which some of the element...

in this problem u given a board in which some of the elements are placed as shown in diagram below .each element represent a color .fill the other elements in the board such that n

Board coloring, color representation 0,1,2,3,4,5,6,7...

color representation 0,1,2,3,4,5,6,7...

Padovan string, #questio#A Padovan string P(n) for a natural number n is de...

#questio#A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes string concatenation. For a string

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd