Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 1
Department of Information Technology
Friend Function
CE 142: Object Oriented
Programming with C++
Kamlesh Makvana
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 2
Department of Information Technology
Example: Rectangle
• calcArea(): Public
Member function of
class Rectangle
• this: to access data
member
• What if calcArea() is not
a member function of
class Rectangle?
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 3
Department of Information Technology
What if calcArea() is not a member
function of class Rectangle?
 Two Possibilities
 calcArea(): Member function of some other
class
 calcArea(): A Normal User Defined function
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 4
Department of Information Technology
calcArea(): A Normal User Defined
function
• No. of Errors:4
• In calcArea():3
• area, width and height
was not declared in the
scope
• In main():1
• class Rectangle has no
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 5
Department of Information Technology
Solution?
• Call calcArea() as
normal function
• Access data member of
r1 in calcArea()
• No. of Errors: 1
• In calcArea()
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 6
Department of Information Technology
Solution?
• Solution
• Pass Object as
Function parameters
• Error: 6
• area, width, height is
private!!!!!
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 7
Department of Information Technology
Solution
 Make calcArea() as friend of class
Rectangle
 What it means?
 If any function is friend of class then that
function can access Private/Protected
members of that class.
 How to make any function as friend of
class?
 Syntax:
 Write prototype of function in class
with prefixed friend!!!!
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 8
Department of Information Technology
Solution?
• Solution
• Make calcArea() as
friend of class
Rectangle
• Output?
• Garbage value!!!!!
• r1 passed by value
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 9
Department of Information Technology
Solution?
• Solution
• Passs r1 as reference.
• Output?
• 7 Error!!!!
• Prototype Does not
matched in friend
function declarations
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 10
Department of Information Technology
Solution?
• Solution
• Function prototype
and Function
definition must
matched
• Output?
• 200
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 11
Department of Information Technology
What if calcArea() is not a member
function of class Rectangle?
 Two Possibilities
 calcArea(): Member function of some
other class
 calcArea(): A Normal User Defined
function
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 12
Department of Information Technology
calcArea(): Member function
of some other class
• Error: Rectangle has not
declared in the scope.
• Solution?
• Forward Declaration of
Rectangle class.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 13
Department of Information Technology
Forward Declaration of class.
 What is forward declaration?
 To instruct the compiler that declared
identifier is class that has been defined
within a program.
 How to forward declare a class
 Syntax:
 class class_name;
 Use this forward declaration before the use
of that class in program.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 14
Department of Information Technology
calcArea(): Member function
of some other class
• Error: Incomplete Data
Type (class)
• Solution?
• Make it Complete!!!!
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 15
Department of Information Technology
Incomplete Data Type
 When incomplete data type error in case
of class occurs in program?
 Class is user-defined data type
 No memory gets allocated to class but it’s
instance (Object)
 Required memory blocks to assign to
objects depends on data member of the
class.
 So when you create object before defining
the class leads to incomplete data type
error.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 16
Department of Information Technology
Incomplete Data Type
 How to resolve error of incomplete data
type? (How to make it complete?!!!!)
 Define class before it’s instance created.
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 17
Department of Information Technology
calcArea(): Member function
of some other class
• Error: Incomplete Data
Type (class)
• Solution?
• Make it Complete!!!!
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 18
Department of Information Technology
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 19
Department of Information Technology
Friend class
Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 20
Department of Information Technology
Important Point to Note:
 Friends should be used only for limited purpose.
too many functions or external classes are
declared as friends of a class with protected or
private data, it lessens the value of encapsulation
of separate classes in object-oriented
programming.
 Declaration can be in either part of function either
public or private.
 Friendship is not mutual. If a class A is friend of B,
then B doesn’t become friend of A automatically.
 Concept of friend is not in java
 Friendship is not inherited

Friend function

  • 1.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 1 Department of Information Technology Friend Function CE 142: Object Oriented Programming with C++ Kamlesh Makvana
  • 2.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 2 Department of Information Technology Example: Rectangle • calcArea(): Public Member function of class Rectangle • this: to access data member • What if calcArea() is not a member function of class Rectangle?
  • 3.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 3 Department of Information Technology What if calcArea() is not a member function of class Rectangle?  Two Possibilities  calcArea(): Member function of some other class  calcArea(): A Normal User Defined function
  • 4.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 4 Department of Information Technology calcArea(): A Normal User Defined function • No. of Errors:4 • In calcArea():3 • area, width and height was not declared in the scope • In main():1 • class Rectangle has no
  • 5.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 5 Department of Information Technology Solution? • Call calcArea() as normal function • Access data member of r1 in calcArea() • No. of Errors: 1 • In calcArea()
  • 6.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 6 Department of Information Technology Solution? • Solution • Pass Object as Function parameters • Error: 6 • area, width, height is private!!!!!
  • 7.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 7 Department of Information Technology Solution  Make calcArea() as friend of class Rectangle  What it means?  If any function is friend of class then that function can access Private/Protected members of that class.  How to make any function as friend of class?  Syntax:  Write prototype of function in class with prefixed friend!!!!
  • 8.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 8 Department of Information Technology Solution? • Solution • Make calcArea() as friend of class Rectangle • Output? • Garbage value!!!!! • r1 passed by value
  • 9.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 9 Department of Information Technology Solution? • Solution • Passs r1 as reference. • Output? • 7 Error!!!! • Prototype Does not matched in friend function declarations
  • 10.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 10 Department of Information Technology Solution? • Solution • Function prototype and Function definition must matched • Output? • 200
  • 11.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 11 Department of Information Technology What if calcArea() is not a member function of class Rectangle?  Two Possibilities  calcArea(): Member function of some other class  calcArea(): A Normal User Defined function
  • 12.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 12 Department of Information Technology calcArea(): Member function of some other class • Error: Rectangle has not declared in the scope. • Solution? • Forward Declaration of Rectangle class.
  • 13.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 13 Department of Information Technology Forward Declaration of class.  What is forward declaration?  To instruct the compiler that declared identifier is class that has been defined within a program.  How to forward declare a class  Syntax:  class class_name;  Use this forward declaration before the use of that class in program.
  • 14.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 14 Department of Information Technology calcArea(): Member function of some other class • Error: Incomplete Data Type (class) • Solution? • Make it Complete!!!!
  • 15.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 15 Department of Information Technology Incomplete Data Type  When incomplete data type error in case of class occurs in program?  Class is user-defined data type  No memory gets allocated to class but it’s instance (Object)  Required memory blocks to assign to objects depends on data member of the class.  So when you create object before defining the class leads to incomplete data type error.
  • 16.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 16 Department of Information Technology Incomplete Data Type  How to resolve error of incomplete data type? (How to make it complete?!!!!)  Define class before it’s instance created.
  • 17.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 17 Department of Information Technology calcArea(): Member function of some other class • Error: Incomplete Data Type (class) • Solution? • Make it Complete!!!!
  • 18.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 18 Department of Information Technology
  • 19.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 19 Department of Information Technology Friend class
  • 20.
    Classified e-Material ©CopyrightsCharotar Institute of Technology, Changa 20 Department of Information Technology Important Point to Note:  Friends should be used only for limited purpose. too many functions or external classes are declared as friends of a class with protected or private data, it lessens the value of encapsulation of separate classes in object-oriented programming.  Declaration can be in either part of function either public or private.  Friendship is not mutual. If a class A is friend of B, then B doesn’t become friend of A automatically.  Concept of friend is not in java  Friendship is not inherited