Friend Function
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.
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.