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 access privileges, Access privileges 1. If the designer of the ...

Access privileges 1. If the designer of the base class needs no one, not even a derived class to access a member, then that member should be made private. 2. If the designer

C++, Program to print the total marks and percentage of the 3 students usin...

Program to print the total marks and percentage of the 3 students using array

Quicksorting in linked lists, How do i write a code in C++ to bubblesort in...

How do i write a code in C++ to bubblesort in linked list

Program to create a class and store student information, Develop a Student ...

Develop a Student class that has the following header file: #ifndef STUDENT_H #define STUDENT_H #include #include #include using namespace std; class Stu

#Padovan string, A Padovan string P(n) for a natural number n is defined as...

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 of the c

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

Write a structured and annotated c program, Intelligent homes are becoming ...

Intelligent homes are becoming increasingly popular as the cost/performance ratio of microcontrollers is continuously dropping.  These systems incorporate various transducers to de

Programming and solving problems with a computer, One person who is special...

One person who is specialist at programming and solving problems with a computer Project Description: Potential computer, hardware, programming and software genius, I look

Student, Ask4. Write a query to display the columns listed below. The query...

Ask4. Write a query to display the columns listed below. The query should list each customer in which the video rental is overdue. The Days_Overdue column should calculate the numb

Program, c | c-c-c-c-c | c-c-C-c-c | ...

c | c-c-c-c-c | c-c-C-c-c | c find distance between difftent carbon atom by programing

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