0% found this document useful (0 votes)
24 views

Friend Function

The document discusses friend functions in C++. A friend function can access private and protected members of a class without creating an object of that class. It is declared using the friend keyword inside the class definition but defined outside the class. A friend function does not have access to the this pointer as it is not a member function of the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Friend Function

The document discusses friend functions in C++. A friend function can access private and protected members of a class without creating an object of that class. It is declared using the friend keyword inside the class definition but defined outside the class. A friend function does not have access to the this pointer as it is not a member function of the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Friend Function

Friend Function
 Friend function is not a member function of the
class to which it is a friend.
 Friend function is declared in the class with the
keyword friend.
 It must be defined outside the class to which it
is friend.
Friend Function
 Friend function can access any member of the
class to which it is a friend.
 Friend function cannot access members of the
class directly.
 It has no caller object.
 It should not be defined with membership
label.
Friend Function(Example)
Friend function
 Friend Function can become friend to
more than one class.
 Friend function can be either private or
public, doesn’t matter.
 Friend function has the ability to access
the members of different classes
simultaneously.
Friend function
Operator overloading as friend function.
class complex
{ private:
int a,b;
public:
void setdata(int x, int y)
{ a=x; b=y; }
void showdata()
{ cout<<"a="<<a<<" b= "<<b; }
complex operator +(complex c) How to make + a friend function ???
{ complex temp;
temp.a=a+c.a;
temp.b=b+c.b;
return (temp);
}
};
Operator overloading as friend function.

When ever we overload a binary


operator as friend function
we have to send two arguments.

Here both c1 & c2 are arguments.


Operator overloading as friend function.
 Now overload unary operator (++, pre increment) using
friend function.
friend complex operator ++(complex); int main()
}; {
complex c1,c2,c3;
complex operator ++(complex X) c1.setdata(3,4);
{ c2.setdata(5,6);
complex temp; c3=++c1;
temp.a=++X.a; c3.showdata();
temp.b=++X.b; getch();
return 0;
return (temp); }
}
Friend Function( Example)
Operator overloading as friend function.
Task 1:
Overload post increment operator using friend
function.
Task 2:
Write a class to represent a vector. Include member
functions( defined outside) to perform the following
operations:
1. create the vector in constructor.
2. Modify the value of given element.
3. Display the vector in formatted form.
Assignment
Task :
Create a class for an electricity board that charges the
following rates to users
 a) For first 100 units : 40p per unit
b) For next 200 units : 50p per unit
c) Beyond 300 units : 60p per unit
 All users are charged a minimum of Rs.500. If the total cost
is more than Rs.250.00 then an additional charges of 15% are
added.
 Write a C++ program using class to read the names of users
& number of units consumed & print out the charges with
names.
DUE On: 19th March,2020 (Before :11:30 AM)
this Keyword.
Object pointer
 A pointer that contains the address of an object is
called object pointer.
this pointer
 class Box
{
private:
int l,h,b;
public:
void setdimension(int x, int y, int z)
{ l=x; h=y; b=z; }
void showdimension()
{ cout<<"length="<<l<<"\nheight="<<h<<"\nBreadth="<<b<<endl;}
};
int main() int main()
{ {
Box smallBox; Box *p,smallBox;
smallBox.setdimension(10,20,30); p=&smallBox;
smallBox.showdimension(); p->setdimension(10,20,30);
getch(); p->showdimension();
return 0; getch();
} return 0;
}
Object pointer
 A pointer that contains the address of an object is
called object pointer.

smallBox
Pointer l b h
this pointer
 this is a keyword.
 this is a local object pointer in every instance member
function containing address of the caller object.
 this pointer can not be modified.
 It is used to refer caller object in member function.
 friend or static functions do not have a this pointer,
because these functions are not members of a class.
Only member functions have a this pointer.
this pointer
 class Box
{
private:
int l,h,b;
public:
void setdimension(int l, int h, int b)
{ l=l; h=h; b=b; }
void showdimension()

{ cout<<"length="<<l<<"\nheight="<<h<<"\nBreadth="<<b<<endl;}
};
this pointer
 class Box
{
private:
int l,h,b;
public:
void setdimension(int l, int h, int b)
{ this->l=l; this->h=h; this->b=b; }
void showdimension()

{ cout<<"length="<<l<<"\nheight="<<h<<"\nBreadth="<<b<<endl;}
};
this pointer
 class Box
{
private:
int l,h,b;
public:
void setdimension(int l, int h, int b)
{ Box::l=l; Box::h=h; Box::b=b; }
void showdimension()

{ cout<<"length="<<l<<"\nheight="<<h<<"\nBreadth="<<b<<endl;}
};
this pointer (Example)

this pointer

Encapsulation
Encapsulation.
 An act of combining properties and methods, related to
the same object, is known as Encapsulation.

Book

InputData();

showData(); Methods

updateprice();

Variables
Why Encapsulation.
 Object becomes equipped with sufficient information
and set of operations.

 Any system can be assumed as a collection of objects.

 These objects are capable to interact with each other


using various methods.
 So encapsulation has two concepts:
 Binding of data and functions.
 Controlled access. (Class)
Why Encapsulation.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy