Você está na página 1de 26

 Non-member function cannot have access to the private data of

a class.
 A situation may require two classes to share their data.
 To handle this situation, friend function is used.
class abc  Function declaration should
be preceded by friend
{ keyword.
...  Function is defined outside as
... a normal C++ function.
public:  Function definition will not
require the use of friend
friend void xyz(void); keyword or scope resolution
operator.
};
 A function can be declared as
friend to any number of
classes.
 A friend function , although
not a member function, has
full access to the private
members of the class.
Property:
To access the data members of a class in the
friend function, the object of the class is used
along with the dot operator.

#include<iostream> OUTPUT
using namespace std; Error
class X{ est.cpp: In function 'void disp()':
test.cpp:21:7: error: 'a' was not declared in this scope
int a; cout<<a<<"\n"<<b;
int b; ^
test.cpp:21:16: error: 'b' was not declared in this
scope
public: cout<<a<<"\n"<<b;
X(){ ^
test.cpp: In function 'int main()':
a=10; test.cpp:27:4: error: 'class X' has no member named
b=20; 'disp'
ob.disp();
} ^~~~
friend void disp();
};

void disp(){
cout<<a<<"\n"<<b;
}

int main(){
X ob;
ob.disp();
}
#include<iostream> OUTPUT
10
using namespace std; 20

class X{
int a;
int b;

public:
X(){
a=10;
b=20;
}
friend void disp(X ob2);
};

void disp(X ob1){


cout<<ob1.a<<"\n"<<ob1.b;
}

int main(){
X ob;
disp(ob);
}
Error

Here in Class X, class Y is unknown.


Declare class Y before class X
Referred to as forward declaration

#include<iostream> void disp(X ob1, Y ob2)


{
using namespace std; cout<<(ob1.a+ob2.b)/2.0;
}
class X{
int a; int main()
public: {
X(){ X ob5;
a=10; Y ob6;
} disp(ob5,ob6);
friend void disp(X ob1,Y ob2); }
};

class Y{
int b;
public:
Y(){
b=20;
}
friend void disp(X ob1,Y ob2);
};
Output

15

#include<iostream> void disp(X ob1, Y ob2)


{
using namespace std; cout<<(ob1.a+ob2.b)/2.0;
}
class Y;
int main()
class X{ {
int a; X ob5;
public: Y ob6;
X(){ disp(ob5,ob6);
a=10; }
}
friend void disp(X ob1,Y ob2);
};

class Y{
int b;
public:
Y(){
b=20;
}
friend void disp(X ob1,Y ob2);
};
 It is not in the scope of the class to which it has been declared as friend.

 Since it is not in scope of the class, it cannot be called using the object of that
class.

 It can be invoked like a normal function without the help of any object.

 Unlike member functions, it cannot access the member names directly and has
to use an object name and dot membership operator with each member name.

 It can be declared either in the public or the private part of a class without
affecting its meaning.

 Usually it has the objects as arguments.


Output

20 10
10 20

#include<iostream> void swap(X ob1, Y ob2)


{
using namespace std; int z;
z=ob1.a;
class Y; ob1.a=ob2.b;
class X{ ob2.b=z;
int a; cout<<ob1.a<<" "<<ob2.b;
public: }
X(){
a=10; int main()
} {
friend void swap(X ob1,Y ob2); X ob5;
void disp(){cout<<a<<" ";} Y ob6;
}; swap(ob5,ob6);
ob5.disp();
ob6.disp();
class Y{ }
int b;
public:
Y(){
b=20;
}
friend void swap(X ob1,Y ob2);
void disp(){cout<<b;}
};
Output

20 10
20 10

#include<iostream> void swap(X & ob1, Y & ob2)


{
using namespace std; int z;
z=ob1.a;
class Y; ob1.a=ob2.b;
class X{ ob2.b=z;
int a; cout<<ob1.a<<" "<<ob2.b;
public: }
X(){
a=10; int main()
} {
friend void swap(X & ob1,Y & ob2); X ob5;
void disp(){cout<<a<<" ";} Y ob6;
}; swap(ob5,ob6);
ob5.disp();
ob6.disp();
class Y{ }
int b;
public:
Y(){
b=20;
}
friend void swap(X & ob1,Y & ob2);
void disp(){cout<<b;}
};
 Mechanism of giving special meaning to an operator.
 All the C++ operators can be overloaded except
 Class member access operators (., *)
 Scope resolution operator (::)
 Size operator
 Conditional operator

 The semantics of a operator can be changed but we cannot


change its syntax, grammatical rules (number of operands,
precedence, associativity).
 When an operator is overloaded its original meaning is not
lost.
return_type class_name :: operator op(arg_list)
{
function body
}

 Operator function must be either member function or friend


function.
 A friend function will have 1 argument for unary operator, 2 for
binary operator
 A member function will have no argument for unary operator, 1
argument for binary operator as the object used to invoke the
method is passed implicitly and is available to the member
function.
 Create a class that defines the data type that is to be used in the
overloading operation.

 Declare the operator function operator op() in the public part


of the class.

 Define the operator function to implement the required


operations.
Output

10 10 10
-10 -10 -10

#include<iostream> void operator -(){


x=-x;
using namespace std; y=-y;
z=-z;
class _3D }
{ };
int x, y, z;
public: int main(){
_3D(){ _3D ob;
x=10,y=10,z=10; ob.disp();
} -ob;
ob.disp();
disp(){ }
cout<<x<<" "<<y<<" "<<z;
}
Output

10 10 10
-10 -10 -10

#include<iostream> void _3D::operator -()


{
using namespace std; x=-x;
y=-y;
class _3D z=-z;
{ }
int x, y, z;
public: int main()
_3D(){ {
x=10,y=10,z=10; _3D ob;
} ob.disp();
-ob;
void disp(); ob.disp();
void operator - (); }

};
void _3D::disp(){
cout<<x<<" "<<y<<" "<<z;
}
Output

10 10 10
-10 -10 -10

#include<iostream> void operator -(_3D & obb)


{
using namespace std; obb.x=-obb.x;
obb.y=-obb.y;
class _3D obb.z=-obb.z;
{ }
int x, y, z;
public: int main()
_3D(){ {
x=10,y=10,z=10; _3D ob1,ob2;
} ob1.disp();
-ob1;
void disp(); ob1.disp();
friend void operator - (_3D & obb); }
};
void _3D::disp(){
cout<<x<<" "<<y<<" "<<z;
}
Output
10 20 30 40 50 60

50 70 90
ob1.operator +(ob2);

#include<iostream> void operator +(IIID &ob2)


#include<math.h> {
x=x+ob2.x;
using namespace std; y=y+ob2.y;
z=z+ob2.z;
class IIID cout<<x<<" "<<y<<" "<<z;
{ }
int x,y,z; };
public:
get() int main()
{ {
cin>>x; IIID ob1;ob1.get();
cin>>y; IIID ob2;ob2.get();
cin>>z; ob1+ob2;
} }
Output

10 20 30
40 50 60

50 70 90

#include<iostream> void operator +(IIID &ob2)


#include<math.h> {
x=x+ob2.x;
using namespace std; y=y+ob2.y;
z=z+ob2.z;
class IIID cout<<x<<" "<<y<<" "<<z;
{ }
int x,y,z; };
public:
IIID() int main()
{ {
cin>>x; IIID ob1;
cin>>y; IIID ob2;
cin>>z; ob1+ob2;
} }
Output
10 20 30 40 50 60

50 70 90
Operator +(&ob1, &ob2);

#include<iostream> void operator +(IIID &ob1, IIID &ob2)


#include<math.h> {
ob1.x=ob1.x+ob2.x;
using namespace std; ob1.y=ob1.y+ob2.y;
ob1.z=ob1.z+ob2.z;
class IIID }
{
int x,y,z; int main()
public: {
IIID() IIID ob1;
{ IIID ob2;
cin>>x; ob1.disp();
cin>>y; ob2.disp();
cin>>z; ob1+ob2;
} ob1.disp();
}
friend void operator +(IIID & ob1,IIID
&ob2);
void disp()
{
cout<<x<<" "<<y<<" "<<z;
}
};
Output

10
30

#include<iostream> void disp()


using namespace std; {
cout<<l<<"\n";
class test }
{ };
int l;
public: int main()
test(){l=0;} {
test(int a) test ob(10);
{ test ob1;
l=a; ob1=ob(10,20);
} ob.disp();
ob1.disp();
test operator ()(int a, int b) }
{
test obb;
obb.l=a+b;
return obb;
}
Output

11

#include<iostream> void disp()


using namespace std; {
class test cout<<l;
{ }
int l; };
public:
test(int a) int main()
{ {
l=a; test ob(10);
} ++ob;
ob.disp();
operator ++() }
{
l=l+1;
}
Output

11

#include<iostream> test operator ++(int)


using namespace std; {
test obb(l);
class test l=l+1;
{ return obb;
int l; }
public:
//test(){l=0;} void disp()
test(int a) {
{ cout<<l;
l=a; }
} };

int main()
{
test ob(10);
ob++;
ob.disp();
}
Output

10 10 10
-10 -10 -10

#include<iostream> void _3D:: operator =(_3D obb)


{
using namespace std; x=obb.x;
y=obb.y;
class _3D{ x=obb.z;
int x, y, z; }
public:
_3D(){ int main()
x=10,y=10,z=10; {
} _3D ob1,ob2;
ob2 = -ob1;
void disp(); ob1.disp();
_3D operator - (); ob2,.disp();
void operator = (_3D obb); }
};
void _3D::disp(){
cout<<x<<" "<<y<<" "<<z;
}
_3D _3D::operator -(){
_3D ob4;
ob4.x=-x;
ob4.y=-y;
ob4.z=-z;
return ob4;
}

Você também pode gostar