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

From Script | Spoken-Tutorial
Revision as of 13:18, 6 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 examples.

Slide 3


To record this tutorial, I am using

Ubuntu OS version 11.10

g++ compiler v. 4.6.1

Slide 4-5 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.

The function can be invoked without using an object.

The argument passed in the friend function is used as its object.

Slide 6 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

In this program we will perform addition operation.

I have already typed the code on the editor.

Point the cursor

frnd.cpp

Note that I have saved the file with the name frnd.cpp

Let me explain the code now.

Highlight

#include<iostream>

This is our header file iostream.
Highlight

using namespace std;

This is the using statement.
Highlight

class frnd

{

private:

int a,b;

public:

void input()

Here we have declared a class frnd.

In this we have declared variables a and b as private.

And input as public member function.

Highlight

cout<<"Enter the value of a and b\n";

cin>>a>>b;

Here we take input from 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.

Then we close the class.

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 the compute method, In this we will perform addition operation.

Here we add the variables.

And 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 main function we create an object of class frnd as f.

We call the function input.

Then 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.
Click on Save Now Click on Save
Let us execute
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

To compile the program type

g++ frnd.cpp -o frnd

To execute type

./frnd

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.
Slide 7

Summary

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