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

How can one encourage his (older) compiler , Q: How can one encourage his (...

Q: How can one encourage his (older) compiler to check new to see automatically if it returns NULL?       A: His compiler eventually will. If he contain an old compiler wh

ASCII, A string S is said to be "Super ASCII", if it contains the character...

A string S is said to be "Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''''a''''-''''z'''') and

Stack, c++ program to to implement multiple stacks using single array

c++ program to to implement multiple stacks using single array

Compiler Design - Limit the methods, L is a text and can be composed of any...

L is a text and can be composed of any of the characters {, }, (, ) , and P, where P will represent the instruction. L will contain single spaced characters where each character

Decode the code, Smugglers are becoming very smart day by day. Now they hav...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Program to calculate the n factorial, Debug the following program to calcul...

Debug the following program to calculate N! #include using namespace std; main() {             int N, factorial=1;             cout             cin >> N;

Program is to define a class as employee, Program is to define a class as e...

Program is to define a class as employee: Write a program to define a class as employee and collect information about them by using classes and object class employee   {

Minimum total number of shelves, At a shop of marbles, packs of marbles are...

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

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