0% found this document useful (0 votes)
3 views

C++(friend function)

The document explains three methods of passing arguments in C++: Call by Value, Call by Pointer, and Call by Reference, highlighting their differences in how they affect actual values. It also covers function overloading, allowing multiple functions with the same name but different parameters, and introduces friend functions and classes that enable access to private members of other classes. Examples illustrate each concept, demonstrating their practical applications in C++ programming.

Uploaded by

megha210103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C++(friend function)

The document explains three methods of passing arguments in C++: Call by Value, Call by Pointer, and Call by Reference, highlighting their differences in how they affect actual values. It also covers function overloading, allowing multiple functions with the same name but different parameters, and introduces friend functions and classes that enable access to private members of other classes. Examples illustrate each concept, demonstrating their practical applications in C++ programming.

Uploaded by

megha210103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Call by Value & Call by Reference in C++

Call by Value in C++


Call by value is a method in C++ to pass the values to the function arguments. In case of call
by value the copies of actual parameters are sent to the formal parameter, which means that
if we change the values inside the function that will not affect the actual values.
void swap(int a, int b)
{ //temp a b
int temp = a; //4 4 5
a = b; //4 5 5
b = temp; //4 5 4
}
int main(){
int x =4, y=5;
cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
swap(x, y);
cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
return 0;
}

Figure 1: Call by Value Swap Function Output


As shown in figure 3, the values of “a” and “b” are the same for both times they are printed.
So, the main point here is that when the call by value method is used it doesn’t change the
actual values because copies of actual values are sent to the function.

Call by Pointer in C++


A call by the pointer is a method in C++ to pass the values to the function arguments. In the
case of call by pointer, the address of actual parameters is sent to the formal parameter,
which means that if we change the values inside the function that will affect the actual
values.
// Call by reference using pointers
void swapPointer(int* a, int* b)
{ //temp a b
int temp = *a; //4 4 5
*a = *b; //4 5 5
*b = temp; //4 5 4
}
int main(){
int x =4, y=5;
cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
swapPointer(&x, &y); //This will swap a and b using pointer reference
cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
return 0;
}

Figure 2: Call by Pointer Swap Function Output


As shown in figure 2, the values of “a” and “b” are swapped when the swap function is
called. So the main point here is that when the call by pointer method is used it changes
the actual values because addresses of actual values are sent to the function.

Call by Reference in C++


Call by reference is a method in C++ to pass the values to the function arguments. In the
case of call by reference, the reference of actual parameters is sent to the formal parameter,
which means that if we change the values inside the function that will affect the actual
values.
void swapReferenceVar(int &a, int &b)
{ //temp a b
int temp = a; //4 4 5
a = b; //4 5 5
b = temp; //4 5 4
}
int main(){
int x =4, y=5;
cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
swapReferenceVar(x, y); //This will swap a and b using reference variables
cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
return 0;
}

Figure 3: Call by Reference Swap Function Output


As shown in figure 3, the values of “a” and “b” are swapped when the swap function is
called. So the main point here is that when the call by reference method is used it changes
the actual values because references of actual values are sent to the function.

C++ Function Overloading


In C++, two functions can have the same name if the number and/or type of arguments
passed is different.
These functions having the same name but different arguments are known as overloaded
functions. For example:
// same name different arguments
int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }
Here, all 4 functions are overloaded functions.
Notice that the return types of all these 4 functions are not the same. Overloaded functions
may or may not have different return types but they must have different arguments. For
example,
// Error code
int test(int a) { }
double test(int b){ }
Here, both functions have the same name, the same type, and the same number of
arguments. Hence, the compiler will throw an error.

Example 1: Overloading Using Different Types of Parameter

// Program to compute absolute value

// Works for both int and float

#include <iostream>

using namespace std;

// function with float type parameter

float absolute(float var){

if (var < 0.0)

var = -var;

return var;

// function with int type parameter

int absolute(int var) {

if (var < 0)

var = -var;

return var;

int main() {

// call function with int type parameter

cout << "Absolute value of -5 = " << absolute(-5) << endl;


// call function with float type parameter

cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl;

return 0;

Output

Absolute value of -5 = 5

Absolute value of 5.5 = 5.5

Working of overloading
for the absolute() function

In this program, we overload the absolute() function. Based on the type of parameter passed during
the function call, the corresponding function is called.

Example 2: Overloading Using Different Number of Parameters


#include <iostream>
using namespace std;

// function with 2 parameters


void display(int var1, double var2) {
cout << "Integer number: " << var1;
cout << " and double number: " << var2 << endl;
}

// function with double type single parameter


void display(double var) {
cout << "Double number: " << var << endl;
}

// function with int type single parameter


void display(int var) {
cout << "Integer number: " << var << endl;
}

int main() {

int a = 5;
double b = 5.5;

// call function with int type parameter


display(a);

// call function with double type parameter


display(b);

// call function with 2 parameters


display(a, b);

return 0;
}
Output
Integer number: 5
Float number: 5.5
Integer number: 5 and double number: 5.5
Here, the display() function is called three times with different arguments. Depending on the
number and type of arguments passed, the corresponding display() function is called.

Working of overloading for the display() function


The return type of all these functions is the same but that need not be the case for function
overloading.

Note: In C++, many standard library functions are overloaded. For example,
the sqrt() function can take double, float, int, etc. as parameters. This is possible because
the sqrt() function is overloaded in C++.
C++ friend Function and friend Classes
Data hiding is a fundamental concept of object-oriented programming. It restricts the access
of private members from outside of the class.
Similarly, protected members can only be accessed by derived classes and are inaccessible
from outside. For example,
class MyClass {
private:
int member1;
}

int main() {
MyClass obj;

// Error! Cannot access private members from here.


obj.member1 = 5;
}
However, there is a feature in C++ called friend functions that break this rule and allow us to
access member functions from outside the class.
Similarly, there is a friend class as well, which we will learn later in this tutorial.

friend Function in C++


A friend function can access the private and protected data of a class. We declare a friend
function using the friend keyword inside the body of the class.
class className {
... .. ...
friend returnType functionName(arguments);
... .. ...
}
Example 1: Working of friend Function
// C++ program to demonstrate the working of friend function

#include <iostream>
using namespace std;

class Distance {
private:
int meter;

// friend function
friend int addFive(Distance);

public:
Distance() : meter(0) {}

};

// friend function definition


int addFive(Distance d) {

//accessing private members from the friend function


d.meter += 5;
return d.meter;
}

int main() {
Distance D;
cout << "Distance: " << addFive(D);
return 0;
}
Output
Distance: 5
Here, addFive() is a friend function that can access both private and public data members.
Though this example gives us an idea about the concept of a friend function, it doesn't show
any meaningful use.
A more meaningful use would be operating on objects of two different classes. That's when
the friend function can be very helpful.

Example 2: Add Members of Two Different Classes


// Add members of two different classes using friend functions

#include <iostream>
using namespace std;

// forward declaration
class ClassB;

class ClassA {

public:
// constructor to initialize numA to 12
ClassA() : numA(12) {}

private:
int numA;
// friend function declaration
friend int add(ClassA, ClassB);
};
class ClassB {

public:
// constructor to initialize numB to 1
ClassB() : numB(1) {}

private:
int numB;

// friend function declaration


friend int add(ClassA, ClassB);
};

// access members of both classes


int add(ClassA objectA, ClassB objectB) {
return (objectA.numA + objectB.numB);
}

int main() {
ClassA objectA;
ClassB objectB;
cout << "Sum: " << add(objectA, objectB);
return 0;
}
Output
Sum: 13
In this program, ClassA and ClassB have declared add() as a friend function. Thus, this
function can access private data of both classes.
One thing to notice here is the friend function inside ClassA is using the ClassB. However, we
haven't defined ClassB at this point.
// inside classA
friend int add(ClassA, ClassB);
For this to work, we need a forward declaration of ClassB in our program.
// forward declaration
class ClassB;

friend Class in C++


We can also use a friend Class in C++ using the friend keyword. For example,
class ClassB;

class ClassA {
// ClassB is a friend class of ClassA
friend class ClassB;
... .. ...
}

class ClassB {
... .. ...
}
When a class is declared a friend class, all the member functions of the friend class become
friend functions.
Since ClassB is a friend class, we can access all members of ClassA from inside ClassB.
However, we cannot access members of ClassB from inside ClassA. It is because friend
relation in C++ is only granted, not taken.
Example 3: C++ friend Class
// C++ program to demonstrate the working of friend class

#include <iostream>
using namespace std;

// forward declaration
class ClassB;

class ClassA {
private:
int numA;

// friend class declaration


friend class ClassB;

public:
// constructor to initialize numA to 12
ClassA() : numA(12) {}
};

class ClassB {
private:
int numB;

public:
// constructor to initialize numB to 1
ClassB() : numB(1) {}
// member function to add numA
// from ClassA and numB from ClassB
int add() {
ClassA objectA;
return objectA.numA + numB;
}
};

int main() {
ClassB objectB;
cout << "Sum: " << objectB.add();
return 0;
}
Output
Sum: 13
Here, ClassB is a friend class of ClassA. So, ClassB has access to the members of classA.
In ClassB, we have created a function add() that returns the sum of numA and numB.
Since ClassB is a friend class, we can create objects of ClassA inside of ClassB.

You might also like