0% found this document useful (0 votes)
153 views14 pages

Understanding Friend Functions in C++

This document discusses friend functions in C++. It provides examples of how to declare friend functions to access private members of classes and how to use friend functions for operator overloading. Specifically, it shows how to declare a friend function to add two Cents objects by overloading the + operator. The function is declared as a friend of the Cents class but is not a member function, allowing it to directly access the private m_cents member variable of the Cents objects.

Uploaded by

khawar abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views14 pages

Understanding Friend Functions in C++

This document discusses friend functions in C++. It provides examples of how to declare friend functions to access private members of classes and how to use friend functions for operator overloading. Specifically, it shows how to declare a friend function to add two Cents objects by overloading the + operator. The function is declared as a friend of the Cents class but is not a member function, allowing it to directly access the private m_cents member variable of the Cents objects.

Uploaded by

khawar abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Object oriented Programming

Lecture 14: Friend functions

Object oriented programming in C++ by Robert Lafore 1


Recap..
• Conversion between basic and user defined types
• Overloading the overloaded operator functions
• Copy elision
• Friend functions

Object oriented programming in C++ by Robert Lafore 2


Friend function belonging to two classes: beta(first class) alpha(second class)
class beta
{ int print(alpha a, beta b)
private: {
int data;
return (a.data + b.data); //ERROR
public:
beta(int num)
}
{ int main()
data = num; {
} alpha a(1);
friend int print(alpha, beta); beta b(2);
}; cout << print(a,b);
class alpha
{
return 0;
private:
int data;
}
public:
alpha(int num)
Forward declaration of a class alpha should be made.
{ This is because class alpha is referenced within the class beta
data = num;
}
friend int print(alpha,beta);
};
Object oriented programming in C++ by Robert Lafore 3
Solution: forward declaration
class alpha;//fwd declare
class beta
{ int print(alpha a, beta b)
private: {
int data;
return (a.data + b.data);
public:
beta(int num)
}
{ int main()
data = num; {
} alpha a(1);
friend int print(alpha, beta); beta b(2);
}; cout << print(a,b);
class alpha
{
return 0;
private:
int data;
}
public:
alpha(int num)
{
data = num; Forward declaration of a class alpha should be made.
} This is because class alpha is referenced within the class beta
friend int print(alpha,beta);
};
Object oriented programming in C++ by Robert Lafore 4
Friend function belonging to two classes: alpha(first class) beta(second class)
class alpha
{ int print(alpha a, beta b)
private:
{
int data;
public:
return (a.data + b.data); //ERROR
alpha(int num) }
{ int main()
data = num; {
} alpha a(1);
friend int print(alpha,beta); beta b(2);
}; cout << print(a,b);
class beta
{
private: return 0;
int data; }
public:
beta(int num)
{ Forward declaration of a class beta should be made.
data = num; This is because class beta is referenced within the class alpha
}
friend int print(alpha, beta);
};
Object oriented programming in C++ by Robert Lafore 5
Solution: forward declaration
class beta;//fwd declare
class alpha
{
private: int print(alpha a, beta b)
int data; {
public: return (a.data + b.data);
alpha(int num) }
{ int main()
data = num; {
} alpha a(1);
friend int print(alpha,beta);
beta b(2);
};
class beta cout << print(a,b);
{
private: return 0;
int data; }
public:
beta(int num)
{
data = num; Forward declaration of a class beta should be made.
} This is because class beta is referenced within the class alpha
friend int print(alpha, beta);
};
Object oriented programming in C++ by Robert Lafore 6
Case: Friend function is a member of some class
We have to use scope resolution operator ::
class alpha void beta::frndA()
class beta
{
{ {
alpha a(2);
private: private: cout<<"Alpha.data = "<<a.data<<endl;
int data; int data; }
public: public:
beta(int num)
alpha(int num) int main()
{
{ {
data = num;
data = num; alpha a(1);
}
} beta b(2);
void frndA();
b.frndA();
}; friend void beta::frndA();
return 0;
}; }

Object oriented programming in C++ by Robert Lafore 7


Case: Friend function is a member of some class
We have to use scope resolution operator ::
class alpha;//need declaration
class alpha void beta::frndA(alpha a)
class beta
{
{ {
cout<<"Alpha.data = "<<a.data<<endl;
private: private: }
int data; int data;
public: public: int main()
beta(int num)
alpha(int num) {
{
{ alpha a(1);
data = num;
data = num; beta b(2);
}
} b.frndA(a);
void frndA(alpha);
return 0;
}; friend void beta::frndA(alpha);
}
};

Object oriented programming in C++ by Robert Lafore 8


Operator overloading using friend functions
• Friend function using operator overloading offers better flexibility to the class.
• These functions are not a members of the class and they do not have 'this'
pointer.
• When you overload a unary operator you have to pass one argument.
• When you overload a binary operator you have to pass two arguments.
• Friend function can access private members of a class directly.

Object oriented programming in C++ by Robert Lafore 9


Operator overloading using friend functions
• Friend function using operator overloading offers better flexibility to the class.
• These functions are not a members of the class and they do not have 'this'
pointer.
• When you overload a unary operator you have to pass one argument.
• When you overload a binary operator you have to pass two arguments.
• Friend function can access private members of a class directly.

Syntax:
friend return-type operator operator-symbol
(Variable 1, Varibale2)
{
//Statements;
}

Object oriented programming in C++ by Robert Lafore 10


class Cents
{
private:
int m_cents;
int main()
public: {
Cents(int cents) { m_cents = cents; } Cents cents1(6);
Cents cents2(8);
// add Cents + Cents using a friend function Cents centsSum = cents1 + cents2;
friend Cents operator+(Cents c1, Cents c2); std::cout << "I have " << centsSum.getCents() << " cen
ts." << std::endl;
int getCents() const { return m_cents; }
}; return 0;
Cents operator+(Cents c1, Cents c2) }
{
return Cents(c1.m_cents + c2.m_cents);
}

Object oriented programming in C++ by Robert Lafore 11


Overload << and >>
• We must know following things before we start overloading these operators.
• cout is an object of ostream class and cin is an object istream class
• These operators must be overloaded as a global function. And if we want to
allow them to access private data members of class, we must make them
friend.
• Why these operators must be overloaded as global?
• In operator overloading, if an operator is overloaded as member, then it must
be a member of the object on left side of the operator. For example, consider
the statement “ob1 + ob2” (let ob1 and ob2 be objects of two different
classes). To make this statement compile, we must overload ‘+’ in class of
‘ob1’ or make ‘+’ a global function.

Object oriented programming in C++ by Robert Lafore 12


class Complex
{ istream & operator >> (istream &in, Complex &c)
private: {
int real, imag; cout << "Enter Real Part ";
public: in >> c.real;
Complex(int r = 0, int i =0) cout << "Enter Imagenory Part ";
{ real = r; imag = i; } in >> c.imag;
friend ostream & operator << (ostream &out, const Complex return in;
&c); }
friend istream & operator >> (istream &in, Complex &c);
}; int main()
{
ostream & operator << (ostream &out, const Complex &c) Complex c1;
{ cin >> c1;
out << c.real; cout << "The complex object is ";
out << "+i" << c.imag << endl; cout << c1;
return out; return 0;
} }

Object oriented programming in C++ by Robert Lafore 13


That’s it

Object oriented programming in C++ by Robert Lafore 14

You might also like