Advanced-C++/C2/Friend-Function/English-timed
From Script | Spoken-Tutorial
| Time | Narration |
| 00:01 | Welcome to the spoken tutorial on Friend Function in C++. |
| 00:06 | In this tutorial, we will learn |
| 00:08 | Friend function. |
| 00:10 | We will do this with the help of an example. |
| 00:13 | To record this tutorial, I am using: |
| 00:15 | Ubuntu OS version 11.10 |
| 00:19 | g++ compiler version 4.6.1 |
| 00:24 | Let us start with an introduction to friend function. |
| 00:27 | We know, the private data is not accessible outside the class. |
| 00:33 | To access the private data, we use friend function. |
| 00:37 | A friend function is not a member function of the class. |
| 00:42 | Friend function can be invoked without using an object. |
| 00:46 | The argument passed in the friend function is used as its object. |
| 00:51 | Let us see the declaration of a friend function. |
| 00:55 | friend keyword is used to declare a friend function. |
| 00:59 | Then we give the return_type. |
| 01:02 | function_name is the name of the function. |
| 01:05 | Then we pass arguments as class_name and object of the class. |
| 01:11 | Let us look at an example. |
| 01:13 | I have already typed the code on the editor. |
| 01:16 | I will open it. |
| 01:18 | In this program, we will perform addition operation. |
| 01:22 | Note that our file-name is frnd.cpp. |
| 01:27 | Let me explain the code now. |
| 01:30 | This is our header file as iostream. |
| 01:34 | Here we are using std namespace. |
| 01:37 | Then we have declared a class frnd. |
| 01:41 | In this, we have declared variables 'a' and 'b' as private. |
| 01:46 | Here we have declared a function input() as public. |
| 01:52 | In this, we take input from the user. |
| 01:55 | This is our friend function as compute(). |
| 01:58 | Here, we have passed arguments as class_name frnd and object of the class 'f1' . |
| 02:06 | Then we close the class. |
| 02:08 | Now, we can access the private members of class frnd using the friend function. |
| 02:16 | Here, we have used the compute() function. |
| 02:19 | In this, we will perform addition operation. |
| 02:23 | We add the variables 'a' and 'b'. |
| 02:26 | And then return the value. |
| 02:28 | Here, we access the private variables in non-member function using the object f1. |
| 02:35 | This is our main() function. |
| 02:38 | In this, we create an object of class frnd as f. |
| 02:44 | Then we call the function input() using the object f. |
| 02:48 | And here, we call function compute() and pass f as argument. |
| 02:54 | You can see that we have passed the argument as f in function compute(). |
| 02:58 | This is done by using the method pass by value. |
| 03:03 | f is passed to the value of f1. |
| 03:06 | And this is our return statement. |
| 03:09 | Now, let us execute the program. |
| 03:11 | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously on your keyboard. |
| 03:20 | Now type: |
| 03:21 | g++ space frnd dot cpp space hyphen o space frnd. Press Enter. |
| 03:32 | Type: dot slash frnd |
| 03:36 | press Enter. |
| 03:38 | Here, it is displayed as: Enter the value of a and b. |
| 03:41 | I will enter as: 8 and 4. |
| 03:46 | The output is displayed as: |
| 03:48 | The result is: 12. |
| 03:51 | This brings us to the end of this tutorial. |
| 03:54 | Let us move back to our slides. |
| 03:56 | Let us summarize.In this tutorial, we learned Friend function. e.g. friend int compute class-name frnd and object 'f1' . |
| 04:08 | As an assignment, write a program to find out the square and cube of a number. |
| 04:14 | Watch the video available at the link shown below. |
| 04:17 | It summarizes the Spoken-Tutorial project. |
| 04:20 | If you do not have good bandwidth, you can download and watch it. |
| 04:24 | The Spoken-Tutorial Project team: conducts workshops using spoken tutorials. |
| 04:30 | Gives certificates to those who pass an online test. |
| 04:33 | For more details, please write to: contact@spoken-tutorial.org |
| 04:40 | Spoken Tutorial Project is a part of the "Talk to a Teacher" project. |
| 04:43 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
| 04:51 | More information on this mission is available at the link shown below: http://spoken-tutorial.org/NMEICT-Intro |
| 04:56 | This is Ashwini Patil from IIT Bombay, signing off.Thank You for joining. |