C++ Friend function
If a function is defined as a friend function in C++, then the protected and private data of a class can be accessed using the function.
By using the keyword friend compiler knows the given function is a friend function.
For accessing the data, the declaration of a friend function should be done inside the body of a class starting with the keyword friend.
Declaration of friend function in C++
In the above declaration, the friend function is preceded by the keyword friend. The function can be defined anywhere in the program like a normal C++ function. The function definition does not use either the keyword friend or scope resolution operator.
Characteristics of a Friend function:
- The function is not in the scope of the class to which it has been declared as a friend.
- It cannot be called using the object as it is not in the scope of that class.
- It can be invoked like a normal function without using the object.
- It cannot access the member names directly and has to use an object name and dot membership operator with the member name.
- It can be declared either in the private or the public part.
C++ friend function Example
Let's see the simple example of C++ friend function used to print the length of a box.
Output:
Length of box: 10
Let's see a simple example when the function is friendly to two classes.
Output:
10
In the above example, min() function is friendly to two classes, i.e., the min() function can access the private members of both the classes A and B.
C++ Friend class
A friend class can access both private and protected members of the class in which it has been declared as friend.
Let's see a simple example of a friend class.
Output:
value of x is : 5
In the above example, class B is declared as a friend inside the class A. Therefore, B is a friend of class A. Class B can access the private members of class A.
C++ Math Functions
C++ offers some basic math functions and the required header file to use these functions is <math.h>
Trignometric functions
| Method | Description |
|---|---|
| cos(x) | It computes the cosine of x. |
| sin(x) | It computes the sine of x. |
| tan(x) | It computes the tangent of x. |
| acos(x) | It finds the inverse cosine of x. |
| asin(x) | It finds the inverse sine of x. |
| atan(x) | It finds the inverse tangent of x. |
| atan2(x,y) | It finds the inverse tangent of a coordinate x and y. |
Hyperbolic functions
| Method | Description |
|---|---|
| cosh(x) | It computes the hyperbolic cosine of x. |
| sinh(x) | It computes the hyperbolic sine of x. |
| tanh(x) | It computes the hyperbolic tangent of x. |
| acosh(x) | It finds the arc hyperbolic cosine of x. |
| asinh(x) | It finds the arc hyperbolic sine of x. |
| atanh(x) | It finds the arc hyperbolic tangent of x. |
Exponential functions
| Method | Description |
|---|---|
| exp(x) | It computes the exponential e raised to the power x. |
| frexp(value_type x,int* exp) | It breaks a number into significand and 2 raised to the power exponent. |
| Idexp(float x, int e) | It computes the product of x and 2 raised to the power e. |
| log(x) | It computes the natural logarithm of x. |
| log10(x) | It computes the common logarithm of x. |
| modf() | It breaks a number into an integer and fractional part. |
| exp2(x) | It computes the base 2 exponential of x. |
| expm1(x) | It computes the exponential raised to the power x minus one. |
| log1p(x) | It computes the natural logarithm of x plus one. |
| log2(x) | It computes the base 2 logarithm of x. |
| logb(x) | It computes the logarithm of x. |
| scalbn( x, n) | It computes the product of x and FLT_RADX raised to the power n. |
| scalbln( x, n) | It computes the product of x and FLT_RADX raised to the power n. |
| ilogb(x) | It returns the exponent part of x. |
Floating point manipulation functions
| Method | Description |
|---|---|
| copysign(x,y) | It returns the magnitude of x with the sign of y. |
| nextafter(x,y) | It represents the next representable value of x in the direction of y. |
| nexttoward(x,y) | It represents the next representable value of x in the direction of y. |
Maximum,Minimum and Difference functions
| Method | Description |
|---|---|
| fdim(x,y) | It calculates the positive difference between x and y. |
| fmax(x,y) | It returns the larger number among two numbers x and y. |
| fmin() | It returns the smaller number among two numbers x and y . |
Power functions
| Method | Description |
|---|---|
| pow(x,y) | It computes x raised to the power y. |
| sqrt(x) | It computes the square root of x. |
| cbrt(x) | It computes the cube root of x. |
| hypot(x,y) | It finds the hypotenuse of a right angled triangle. |
Nearest integer operations
| Method | Description |
|---|---|
| ceil(x) | It rounds up the value of x. |
| floor(x) | It rounds down the value of x. |
| round(x) | It rounds off the value of x. |
| lround(x) | It rounds off the value of x and cast to long integer. |
| llround(x) | It rounds off the value of x and cast to long long integer. |
| fmod(n,d) | It computes the remainder of division n/d. |
| trunc(x) | It rounds off the value x towards zero. |
| rint(x) | It rounds off the value of x using rounding mode. |
| lrint(x) | It rounds off the value of x using rounding mode and cast to long integer. |
| llrint(x) | It rounds off the value x and cast to long long integer. |
| nearbyint(x) | It rounds off the value x to a nearby integral value. |
| remainder(n,d) | It computes the remainder of n/d. |
| remquo() | It computes remainder and quotient both. |
Other functions
| Method | Description |
|---|---|
| fabs(x) | It computes the absolute value of x. |
| abs(x) | It computes the absolute value of x. |
| fma(x,y,z) | It computes the expression x*y+z. |
Macro functions
| Method | Description |
|---|---|
| fpclassify(x) | It returns the value of type that matches one of the macro constants. |
| isfinite(x) | It checks whether x is finite or not. |
| isinf() | It checks whether x is infinite or not. |
| isnan() | It checks whether x is nan or not. |
| isnormal(x) | It checks whether x is normal or not. |
| signbit(x) | It checks whether the sign of x is negative or not. |
Comparison macro functions
| Method | Description |
|---|---|
| isgreater(x,y) | It determines whether x is greater than y or not. |
| isgreaterequal(x,y) | It determines whether x is greater than or equal to y or not. |
| less(x,y) | It determines whether x is less than y or not. |
| islessequal(x,y) | It determines whether x is less than or equal to y. |
| islessgreater(x,y) | It determines whether x is less or greater than y or not. |
| isunordered(x,y) | It checks whether x can be meaningfully compared or not. |
Error and gamma functions
| Method | Description |
|---|---|
| erf(x) | It computes the error function value of x. |
| erfc(x) | It computes the complementary error function value of x. |
| tgamma(x) | It computes the gamma function value of x. |
| lgamma(x) | It computes the logarithm of a gamma function of x. |
Comments
Post a Comment