Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Friend Functions: Accessing Private Members with Default Arguments, Study notes of Computer Science

The concept of friend functions in c++, which have access to private and protected members of a class despite being defined outside its scope. It also covers default arguments in c++ functions and demonstrates their usage through examples. Call by reference and call by value in c++ function arguments and their implications.

Typology: Study notes

2017/2018

Uploaded on 11/04/2018

vignesh333
vignesh333 🇮🇳

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
A friend function of a class is dened outside that class' scope but it has the
right to access all private and protected members of the class. Even though
the prototypes for friend functions appear in the class denition, friends are
not member functions.
A friend can be a function, function template, or member function, or a class
or class template, in which case the entire class and all of its members are
friends.
To declare a function as a friend of a class, precede the function prototype in
the class denition with keyword friend as follows
Default Arguments in C++
A default argument is a value provided in function declaration that is automatically assigned by the
compiler if caller of the function doesn’t provide a value for the argument with default value.
Following is a simple C++ example to demonstrate use of default arguments. We don’t have to write 3
sum functions, only one function works by using default values for 3rd and 4th arguments.
C++ function call by reference
The call by reference method of passing arguments to a function copies
the reference of an argument into the formal parameter. Inside the function,
the reference is used to access the actual argument used in the call. This
means that changes made to the parameter aect the passed argument.
To pass the value by reference, argument reference is passed to the
functions just like any other value. So accordingly you need to declare the
function parameters as reference types as in the following function swap(),
which exchanges the values of the two integer variables pointed to by its
arguments
C++ function call by value
The call by value method of passing arguments to a function copies the
actual value of an argument into the formal parameter of the function. In
pf2

Partial preview of the text

Download C++ Friend Functions: Accessing Private Members with Default Arguments and more Study notes Computer Science in PDF only on Docsity!

A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends. To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows −

Default Arguments in C++

A default argument is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn’t provide a value for the argument with default value. Following is a simple C++ example to demonstrate use of default arguments. We don’t have to write 3 sum functions, only one function works by using default values for 3rd and 4th arguments.

C++ function call by reference

The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in the following function swap() , which exchanges the values of the two integer variables pointed to by its arguments

C++ function call by value

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In

this case, changes made to the parameter inside the function have no effect on the argument.

By default, C++ uses call by value to pass arguments. In general, this means that code within a function cannot alter the arguments used to call the function. Consider the function swap() definition as follows.