Question

In: Computer Science

What is a friend function? Do we have to use friend functions? Explain why or why...

What is a friend function? Do we have to use friend functions? Explain why or why not.

c++

Solutions

Expert Solution

Friend function:

A friend function is not the member function of the class, so there is no 'this' pointer. When we are overloading a unary operator then we have to pass one parameter. This function can access the private member of a class.

The complete program source code using friend function and without friend function is given below:

#include <iostream>

using namespace std;

class Number
{
//private data member
int a;
  
public:
  
//default constructor
Number()
{
a = 0;
}
  
//parameterized consturctor
Number(int num)
{
a = num;
}
  
//Operator ++ overloading function
Number & operator--(int)
{
a--;
return *this;
}
  
//display the object values
void display()
{
cout<<endl<<"a = "<<a;
}
  
//method declaration to overload operator - using frined function
friend Number operator-(const Number & obj);
};

//method to overload operator - using frined function
Number operator-(const Number & obj)
{
   return Number(-(obj.a));
}

int main()
{
//create objects
Number num1(100);
Number num2(200);
  
num1--;
num2 = -num2;
  
//display the object on the computer screen
num1.display();
num2.display();

return 0;
}


OUTPUT:

We can use the friend function if it is required to share the data between the two classes. A friend function can be the friend of any number of classes. This function works as a normal function and there is no need for the class object for calling the friend function.

This function is not good from the security point of view because this function can access the private data of more than one class but it is not the member function of any class. Forward declaration of a class is needed if the function is a friend function of two or more classes.


Related Solutions

What functions do central banks perform in a market-oriented economy? Explain why each function you have...
What functions do central banks perform in a market-oriented economy? Explain why each function you have listed is important in the functioning of a market-oriented economic system.
What are the ledgers, why do we use them? And then HOW do we use them,...
What are the ledgers, why do we use them? And then HOW do we use them, how does information get into them how do balances get extracted. And then what should the balances for various accounts be, i.e. assets, liabilities, expenses, revenues, equity, dividends. Why SHOULD they have a particular balance as either debit or credit.
Why do we have introns and what is the purpose of them? Why do we waste...
Why do we have introns and what is the purpose of them? Why do we waste energy making introns? The question may be broad but it's something our professor asked us. Thank you.
Trigonometric Functions. Why do they have asymptotes and why do they have intervals?
Trigonometric Functions. Why do they have asymptotes and why do they have intervals?
What is a beam expander? Elaborate on the different types. Why do we use it? Explain...
What is a beam expander? Elaborate on the different types. Why do we use it? Explain with the help of an example
What is the importantance of the DuPont, why do we use it?
What is the importantance of the DuPont, why do we use it?
What are the different measures of unemployment? Why do we use the category that we do...
What are the different measures of unemployment? Why do we use the category that we do for the official rate?
What are chi distributions, how do we use them, when do we use them, and why...
What are chi distributions, how do we use them, when do we use them, and why are they important?
1. What do we mean by a "production function"? Interpret the following production functions. (a) Q...
1. What do we mean by a "production function"? Interpret the following production functions. (a) Q = 50L^3/4 K^1/4 (b) Q = 640K L^2-K^2L^3
why do we use complete induction and why do we use structual induction? When should we...
why do we use complete induction and why do we use structual induction? When should we use complete or structual?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT