Difference between revisions of "Advanced-C++/C2/Friend-Function/English"
(Created page with ''''Title of script''': Friend function in C++ '''Author: '''Ashwini Patil '''Keywords: friend function, C++, Video tutorial.''' {| style="border-spacing:0;" | style="backgro…') |
|||
Line 24: | Line 24: | ||
'''Friend function.''' | '''Friend function.''' | ||
− | We will do this with the help of | + | We will do this with the help of an example. |
|- | |- | ||
Line 33: | Line 33: | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To record this tutorial, I am using | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To record this tutorial, I am using | ||
− | '''Ubuntu OS ''' | + | '''Ubuntu OS '''v. 11.10 |
'''g++ compiler '''v. 4.6.1 | '''g++ compiler '''v. 4.6.1 | ||
Line 39: | Line 39: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 4-5 | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 4-5 | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| We know, the private data is not accessible outside the class. | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us start with the introduction to friend function. |
+ | |||
+ | We know, the private data is not accessible outside the class. | ||
To access the private data we use '''friend function'''. | To access the private data we use '''friend function'''. | ||
Line 45: | Line 47: | ||
A friend function is not a member of the class. | A friend function is not a member of the class. | ||
− | + | Friend function can be invoked without using an object. | |
The argument passed in the friend function is used as its object. | The argument passed in the friend function is used as its object. | ||
Line 51: | Line 53: | ||
|- | |- | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 6 | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 6 | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us see the declaration of a friend function. |
'''friend return_type function_name( class_name object)''' | '''friend return_type function_name( class_name object)''' | ||
Line 69: | Line 71: | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us look at an example | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us look at an example | ||
− | |||
− | |||
I have already typed the code on the editor. | I have already typed the code on the editor. | ||
+ | |||
+ | I will open it. | ||
+ | |||
+ | In this program we will perform addition operation. | ||
|- | |- | ||
Line 78: | Line 82: | ||
'''frnd.cpp''' | '''frnd.cpp''' | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Note that | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Note that our filename is '''frnd.cpp''' |
Let me explain the code now. | Let me explain the code now. | ||
Line 86: | Line 90: | ||
'''<nowiki>#include<iostream></nowiki>''' | '''<nowiki>#include<iostream></nowiki>''' | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''header file iostream.''' | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''header file as iostream.''' |
|- | |- | ||
Line 92: | Line 96: | ||
'''using namespace std;''' | '''using namespace std;''' | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we are using std namespace. |
|- | |- | ||
Line 108: | Line 112: | ||
'''void input()''' | '''void input()''' | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Then we have declared a '''class frnd.''' |
In this we have declared variables '''a''' and '''b '''as private. | In this we have declared variables '''a''' and '''b '''as private. | ||
− | + | '''Here we have declared a function input''' as '''public''' | |
|- | |- | ||
Line 120: | Line 124: | ||
'''cin>>a>>b;''' | '''cin>>a>>b;''' | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this we take input from the user. |
|- | |- | ||
Line 130: | Line 134: | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''friend function '''as''' compute'''. | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''friend function '''as''' compute'''. | ||
− | Here, we have passed arguments as '''class_name''' '''frnd '''and''' object '''of the''' class'''. | + | Here, we have passed arguments as '''class_name''' '''frnd '''and''' object '''of the''' class f1'''. |
Then we close the class. | Then we close the class. | ||
− | + | Now we can access the private members of class '''frnd''' using the friend function. | |
|- | |- | ||
Line 142: | Line 146: | ||
'''return int(f1.a+f1.b);''' | '''return int(f1.a+f1.b);''' | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have the compute method | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have used the compute method. |
− | + | In this we will perform addition operation. | |
− | And return the value. | + | We add the variables. |
+ | |||
+ | And then return the value. | ||
Here we access the private variables in non-member function using the '''object f1.''' | Here we access the private variables in non-member function using the '''object f1.''' | ||
Line 157: | Line 163: | ||
'''{''' | '''{''' | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''main''' function. | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''main''' function. | ||
+ | |||
+ | |||
+ | |||
|- | |- | ||
Line 166: | Line 175: | ||
'''<nowiki>cout << "The result is: " << compute(f)<<"\n"; </nowiki>''' | '''<nowiki>cout << "The result is: " << compute(f)<<"\n"; </nowiki>''' | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this we create an '''object''' of class '''frnd''' as '''f.''' |
− | + | Then we call the '''function input '''using the''' object f.''' | |
− | + | '''And here we call function compute '''and pass''' f '''as argument. | |
− | You can see that we have passed the argument as f in function compute. | + | You can see that we have passed the argument as '''f''' in function compute. |
− | This is done by using the method pass by value. | + | This is done by using the method '''pass by value.''' |
− | f is passed to the value of f1. | + | '''f''' is passed to the value of '''f1.''' |
|- | |- | ||
Line 183: | Line 192: | ||
'''return 0;''' | '''return 0;''' | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is the return statement. | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is the return statement. | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now let us execute the program. |
|- | |- | ||
Line 208: | Line 213: | ||
'''./frnd''' | '''./frnd''' | ||
− | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| | + | | style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now type: |
'''g++ frnd.cpp -o frnd''' | '''g++ frnd.cpp -o frnd''' | ||
Line 215: | Line 220: | ||
'''./frnd''' | '''./frnd''' | ||
+ | |||
+ | '''Press Enter''' | ||
|- | |- | ||
Line 237: | Line 244: | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Please add a little more on the flow of the program. | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Please add a little more on the flow of the program. | ||
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This brings us to the end of this tutorial. | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This brings us to the end of this tutorial. | ||
+ | |||
+ | Let us move back to our slides. | ||
|- | |- | ||
Line 242: | Line 251: | ||
Summary | Summary | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial we have seen | + | | style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us summarize: |
+ | |||
+ | In this tutorial we have seen | ||
'''Friend function.''' | '''Friend function.''' |
Latest revision as of 11:46, 8 May 2013
Title of script: Friend function in C++
Author: Ashwini Patil
Keywords: friend function, C++, Video tutorial.
|
|
Slide 1 | Welcome to the spoken tutorial on friend function in C++. |
Slide 2
|
In this tutorial we will learn,
Friend function. We will do this with the help of an example. |
Slide 3
|
To record this tutorial, I am using
Ubuntu OS v. 11.10 g++ compiler v. 4.6.1 |
Slide 4-5 | Let us start with the introduction to friend function.
We know, the private data is not accessible outside the class. To access the private data we use friend function. A friend function is not a member of the class. Friend function can be invoked without using an object. The argument passed in the friend function is used as its object. |
Slide 6 | Let us see the declaration of a friend function.
friend return_type function_name( class_name object) friend keyword is used to declare a friend function. Then we give the return_type. function_name is the name of the function. Then we pass arguments as class_name and object of the class. |
Open frnd.cpp
|
Let us look at an example
I have already typed the code on the editor. I will open it. In this program we will perform addition operation. |
Point the cursor
frnd.cpp |
Note that our filename is frnd.cpp
Let me explain the code now. |
Highlight
#include<iostream> |
This is our header file as iostream. |
Highlight
using namespace std; |
Here we are using std namespace. |
Highlight
class frnd { private: int a,b; public: void input() |
Then we have declared a class frnd.
In this we have declared variables a and b as private. Here we have declared a function input as public |
Highlight
cout<<"Enter the value of a and b\n"; cin>>a>>b; |
In this we take input from the user. |
Highlight
friend int compute(frnd f1); }; |
This is our friend function as compute.
Here, we have passed arguments as class_name frnd and object of the class f1. Then we close the class. Now we can access the private members of class frnd using the friend function. |
Highlight
int compPlease make a mention of the compute method.ute(frnd f1) return int(f1.a+f1.b); |
Here we have used the compute method.
In this we will perform addition operation. We add the variables. And then return the value. Here we access the private variables in non-member function using the object f1. |
Highlight
int main() { |
This is our main function.
|
Highlight
frnd f; f.input(); cout << "The result is: " << compute(f)<<"\n"; |
In this we create an object of class frnd as f.
Then we call the function input using the object f. And here we call function compute and pass f as argument. You can see that we have passed the argument as f in function compute. This is done by using the method pass by value. f is passed to the value of f1. |
Highlight
return 0; |
This is the return statement. |
Now let us execute the program. | |
Open the terminal
Ctrl, Alt and T keys simultaneously |
Open the terminal by pressing Ctrl, Alt and T keys simultaneously |
Type
g++ frnd.cpp -o frnd To execute Type ./frnd |
Now type:
g++ frnd.cpp -o frnd To execute type ./frnd Press Enter |
Highlight
Output |
Here it is displayed as:
Enter the value of a and b I will enter as: 8 4 The output is displayed as: The result is: 12 |
Please add a little more on the flow of the program. | This brings us to the end of this tutorial.
Let us move back to our slides. |
Slide 7
Summary |
Let us summarize:
In this tutorial we have seen Friend function. eg. friend int compute(frnd f1); |
Slide 8
Assignment |
As an assignment,
Write a program to find out the square and cube of a number. |
Slide 9
About the Spoken Tutorial Project |
Watch the video available at the link shown
It summarizes the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it |
Slide 10
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to, contact@spoken-tutorial.org |
Slide Number 11
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
This is Ashwini Patil from IIT Bombay signing off
Thank You for joining. |