SlideShare a Scribd company logo
1 of 20
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

More Related Content

What's hot

Machine learning on streams of data
Machine learning on streams of dataMachine learning on streams of data
Machine learning on streams of dataTomasz Sosiński
 
Ni labview y multisim
Ni labview y multisimNi labview y multisim
Ni labview y multisimTame Claudio
 
Lambdas And Streams Hands On Lab
Lambdas And Streams Hands On LabLambdas And Streams Hands On Lab
Lambdas And Streams Hands On LabSimon Ritter
 
Software Technologies for the Interoperability, Reusability and Adaptability...
Software Technologies for the Interoperability,  Reusability and Adaptability...Software Technologies for the Interoperability,  Reusability and Adaptability...
Software Technologies for the Interoperability, Reusability and Adaptability...Daniele Gianni
 
EG-ICE 2015 - Coping with IFC lists in the ifcOWL ontology
EG-ICE 2015 - Coping with IFC lists in the ifcOWL ontologyEG-ICE 2015 - Coping with IFC lists in the ifcOWL ontology
EG-ICE 2015 - Coping with IFC lists in the ifcOWL ontologyPieter Pauwels
 
National instruments for Academics: labview multisim & elsvi
National instruments for Academics: labview multisim & elsviNational instruments for Academics: labview multisim & elsvi
National instruments for Academics: labview multisim & elsviSHAIK AMANULLA
 

What's hot (7)

Machine learning on streams of data
Machine learning on streams of dataMachine learning on streams of data
Machine learning on streams of data
 
Handout#10
Handout#10Handout#10
Handout#10
 
Ni labview y multisim
Ni labview y multisimNi labview y multisim
Ni labview y multisim
 
Lambdas And Streams Hands On Lab
Lambdas And Streams Hands On LabLambdas And Streams Hands On Lab
Lambdas And Streams Hands On Lab
 
Software Technologies for the Interoperability, Reusability and Adaptability...
Software Technologies for the Interoperability,  Reusability and Adaptability...Software Technologies for the Interoperability,  Reusability and Adaptability...
Software Technologies for the Interoperability, Reusability and Adaptability...
 
EG-ICE 2015 - Coping with IFC lists in the ifcOWL ontology
EG-ICE 2015 - Coping with IFC lists in the ifcOWL ontologyEG-ICE 2015 - Coping with IFC lists in the ifcOWL ontology
EG-ICE 2015 - Coping with IFC lists in the ifcOWL ontology
 
National instruments for Academics: labview multisim & elsvi
National instruments for Academics: labview multisim & elsviNational instruments for Academics: labview multisim & elsvi
National instruments for Academics: labview multisim & elsvi
 

Similar to Friend function

Learning Embedded Software Development from Hello World
Learning Embedded Software Development from Hello WorldLearning Embedded Software Development from Hello World
Learning Embedded Software Development from Hello WorldJian-Hong Liu
 
Data Science for Dummies - Data Engineering with Titanic dataset + Databricks...
Data Science for Dummies - Data Engineering with Titanic dataset + Databricks...Data Science for Dummies - Data Engineering with Titanic dataset + Databricks...
Data Science for Dummies - Data Engineering with Titanic dataset + Databricks...Rodney Joyce
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET FrameworkKamlesh Makvana
 
APEX 18 Interactive Grids: And Them Some, Part 2
APEX 18 Interactive Grids: And Them Some, Part 2APEX 18 Interactive Grids: And Them Some, Part 2
APEX 18 Interactive Grids: And Them Some, Part 2Karen Cannell
 
Intro to JavaScript for APEX Developers
Intro to JavaScript for APEX DevelopersIntro to JavaScript for APEX Developers
Intro to JavaScript for APEX DevelopersDaniel McGhan
 
Machine Learning with Azure
Machine Learning with AzureMachine Learning with Azure
Machine Learning with AzureBarbara Fusinska
 
Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)Asfand Hassan
 
Data Lineage, Property Based Testing & Neo4j
Data Lineage, Property Based Testing & Neo4j Data Lineage, Property Based Testing & Neo4j
Data Lineage, Property Based Testing & Neo4j Neo4j
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing SoftwareSteven Smith
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++Michael Redlich
 
2020 09-16-ai-engineering challanges
2020 09-16-ai-engineering challanges2020 09-16-ai-engineering challanges
2020 09-16-ai-engineering challangesIvica Crnkovic
 
Software Quality in Practice
Software Quality in PracticeSoftware Quality in Practice
Software Quality in PracticeGanesh Samarthyam
 
ADBA (Asynchronous Database Access)
ADBA (Asynchronous Database Access)ADBA (Asynchronous Database Access)
ADBA (Asynchronous Database Access)Logico
 
Prospectus editing at the University of Bristol- an overview:TERMINALFOUR t44...
Prospectus editing at the University of Bristol- an overview:TERMINALFOUR t44...Prospectus editing at the University of Bristol- an overview:TERMINALFOUR t44...
Prospectus editing at the University of Bristol- an overview:TERMINALFOUR t44...Terminalfour
 
From science to engineering, the process to build a machine learning product
From science to engineering, the process to build a machine learning productFrom science to engineering, the process to build a machine learning product
From science to engineering, the process to build a machine learning productBruce Kuo
 
Overview of Modern Graph Analysis Tools
Overview of Modern Graph Analysis ToolsOverview of Modern Graph Analysis Tools
Overview of Modern Graph Analysis ToolsKeiichiro Ono
 

Similar to Friend function (20)

The math api
The math apiThe math api
The math api
 
Learning Embedded Software Development from Hello World
Learning Embedded Software Development from Hello WorldLearning Embedded Software Development from Hello World
Learning Embedded Software Development from Hello World
 
Data Science for Dummies - Data Engineering with Titanic dataset + Databricks...
Data Science for Dummies - Data Engineering with Titanic dataset + Databricks...Data Science for Dummies - Data Engineering with Titanic dataset + Databricks...
Data Science for Dummies - Data Engineering with Titanic dataset + Databricks...
 
Introduction to .NET Framework
Introduction to .NET FrameworkIntroduction to .NET Framework
Introduction to .NET Framework
 
Unit 1
Unit 1Unit 1
Unit 1
 
APEX 18 Interactive Grids: And Them Some, Part 2
APEX 18 Interactive Grids: And Them Some, Part 2APEX 18 Interactive Grids: And Them Some, Part 2
APEX 18 Interactive Grids: And Them Some, Part 2
 
Intro to JavaScript for APEX Developers
Intro to JavaScript for APEX DevelopersIntro to JavaScript for APEX Developers
Intro to JavaScript for APEX Developers
 
Machine Learning with Azure
Machine Learning with AzureMachine Learning with Azure
Machine Learning with Azure
 
Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)
 
Data Lineage, Property Based Testing & Neo4j
Data Lineage, Property Based Testing & Neo4j Data Lineage, Property Based Testing & Neo4j
Data Lineage, Property Based Testing & Neo4j
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 
SQL
SQL SQL
SQL
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
 
2020 09-16-ai-engineering challanges
2020 09-16-ai-engineering challanges2020 09-16-ai-engineering challanges
2020 09-16-ai-engineering challanges
 
Software Quality in Practice
Software Quality in PracticeSoftware Quality in Practice
Software Quality in Practice
 
ADBA (Asynchronous Database Access)
ADBA (Asynchronous Database Access)ADBA (Asynchronous Database Access)
ADBA (Asynchronous Database Access)
 
Prospectus editing at the University of Bristol- an overview:TERMINALFOUR t44...
Prospectus editing at the University of Bristol- an overview:TERMINALFOUR t44...Prospectus editing at the University of Bristol- an overview:TERMINALFOUR t44...
Prospectus editing at the University of Bristol- an overview:TERMINALFOUR t44...
 
From science to engineering, the process to build a machine learning product
From science to engineering, the process to build a machine learning productFrom science to engineering, the process to build a machine learning product
From science to engineering, the process to build a machine learning product
 
Relational database design
Relational database designRelational database design
Relational database design
 
Overview of Modern Graph Analysis Tools
Overview of Modern Graph Analysis ToolsOverview of Modern Graph Analysis Tools
Overview of Modern Graph Analysis Tools
 

Recently uploaded

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGSIVASHANKAR N
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 

Recently uploaded (20)

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 

Friend function

  • 1. 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
  • 2. 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?
  • 3. 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
  • 4. 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
  • 5. 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()
  • 6. 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!!!!!
  • 7. 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!!!!
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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.
  • 13. 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.
  • 14. 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!!!!
  • 15. 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.
  • 16. 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.
  • 17. 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!!!!
  • 18. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 18 Department of Information Technology
  • 19. Classified e-Material ©Copyrights Charotar Institute of Technology, Changa 19 Department of Information Technology Friend class
  • 20. 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