Advanced-C++/C2/Friend-Function/English

From Script | Spoken-Tutorial
Revision as of 11:46, 8 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Friend function in C++

Author: Ashwini Patil

Keywords: friend function, C++, Video tutorial.


Visual Cue
Narration
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


Acknowledgement

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.

Contributors and Content Editors

Ashwini