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

Algorithm, Write an algorithm to print all even numbers in descending order...

Write an algorithm to print all even numbers in descending order and draw the flowchart.

Explain pointers to objects, Pointers to Objects Passing and returning ...

Pointers to Objects Passing and returning of objects is, though, not very efficient since it includes passing and returning a copy of the data members. This problem can be elim

Calculate the area and circumference of a circle , Write a program which in...

Write a program which incorporates a function named compute and which is used to calculate the area and circumference of a circle. Use the main function for inputs and outputs.

Write a program to create a binary file, Write a program to create a binary...

Write a program to create a binary file and store the following data "hello", 0x0030,'1', 1.234  Using visual studio examine the binary file and note how the day is stored   Answe

Define procedure to input integers and returns the average, The procedure +...

The procedure +, * and list take arbitrary numbers of arguments. One way to define such a procedure is to use define with dotted-tail notation. In a procedure definition, a paramet

C program to store all ascii char into a file, Program is to store all ASCI...

Program is to store all ASCII char into a file: void main()     {   ofstream  fout("ascii.txt");   int i,n=256;   for(i=1;i     {     fout     }

Explain virtual base classes, Virtual Base Classes This ambiguity can b...

Virtual Base Classes This ambiguity can be resolved if the class derived have only one copy of the class base. This can be done by making the base class a virtual class. This k

What is the use of default constructor, What is the use of default construc...

What is the use of default constructor? - It's a constructor that doesn't accept any parameters. - If there is no user-defined constructor for a class, compiler declares a d

Luminous Jewels - The Polishing Game, First line starts with T, number of t...

First line starts with T, number of test cases. Each test case T contains a necklace (N).

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

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