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

Difference between method overloading and method overriding, Overloading a ...

Overloading a method (or function) in C++ is the ability for functions of the similar name to be defined as long as these methods have dissimilar signatures (different set of param

Function that has two int parameters number , Write a function that has two...

Write a function that has two int parameters num and n, and returns TRUE when the nth bit in num is 1, otherwise FALSE. This function should use the function in the above problem.

I want to change c++ code to python extension, I want to change C++ code to...

I want to change C++ code to Python extension Project Description: I have the C++ source code for an executable that takes a path to an image file as the input and prints tex

What does extern "c" int func(int *, A: This will turn o_ "name mangling" f...

A: This will turn o_ "name mangling" for func so that one can connect to code compiled by a C compiler.

Linking source code in vc++ 6.0, i have a project in BDD(Binary Decision Di...

i have a project in BDD(Binary Decision Diagram).. where i have to use BDD library...i m using cudd package which uses BDD technique...i download cudd package..try to run in vc++ 6

Define scope rules of c program - computer programming, Define Scope Rules ...

Define Scope Rules of C program - Computer Programming? The fundamental rule of scope is that identifiers are accessible only within the block in which they are declared and th

C program to convert time in 24 hour format to 12., Aim: To implement a pr...

Aim: To implement a program to convert time in 24 hour format to 12 hour format. Code:                       #include #include #include class time24 {

Wap to calculate total marks and percentage of 3 subjects, WAP TO ACCEPT MA...

WAP TO ACCEPT MARKS OF THREE SUBJECT FOR STUDENT & CALCULATE TOTAL MARKS AND PERCENTAGE #include stdio.h> #include conio.h>   void main() {            int M1,M

Coding, dispaly the last ant on rod

dispaly the last ant on rod

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