---
title: "How do I find square of a number using friend function in c++?"  
description: "How do I find square of a number using friend function in c++?"  
author: "samay srivastava"  
published: 2018-03-08  
canonical: https://answers.mindstick.com/qa/37399/how-do-i-find-square-of-a-number-using-friend-function-in-c-plus-plus  
category: "sports"  
reading_time: 2 minutes  

---

# How do I find square of a number using friend function in c++?

How do I find square of a number using [friend function](https://answers.mindstick.com/qa/92532/what-is-a-friend-function) in [c++](https://www.mindstick.com/blog/745/array-in-c-plus-plus)?

## Answers

### Answer by Arti Mishra

**Friend [Function](https://www.mindstick.com/articles/43970/purchase-suitable-wedding-dresses-for-your-wedding-function) in C++:** Friend functions are those function that is not a member of class. It also define the outside of class. Using friend function you can access the private and protected member of a class. Friend function declare within the class and define outside of the class. It is special type of function.\
**How to declare Friend Function:** For declaring friend function you can use friend keyword. **Syntax :**

```
//within class declaration
class className {
    friend functionName( parameters );
}
```

\
**Advantage of Friend Function:**

- Friend functions can access private and protected members of a class.
- The most powerful advantage of encapsulation and data hiding is that a non-member function of the class it cannot access a member data of that particular class.
- It is very helpful for operator overloading.

**\****Find the square of a number using friend function:****\**

```
#include<iostream>
using namespace std;
Class SquareExample
{
int number;
public:
void enterNumber()
{
cout<<”Enter Any Number… “;
cin>>number;
}
friend int findSquare(SquareExample obj1);
};
int findSquare(SquareExample obj1)
{
return (obj1.number*obj1.number);
}
int main()
{
SquareExample obj;
obj.enterNumber();
cout<< “Square of a number is : ”<< findSquare(obj);
return 0;
}
```

\


---

Original Source: https://answers.mindstick.com/qa/37399/how-do-i-find-square-of-a-number-using-friend-function-in-c-plus-plus

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
