
IIT Bombay Computer Programming Lecture on Friend Functions
"Explore IIT Bombay's lecture on friend functions in computer programming, covering topics such as data encapsulation, member accessibility, and the role of friend declarations in C++. Dr. Deepak B. Phatak and Dr. Supratik Chakraborty delve into examples and explanations to enhance your understanding."
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
IIT Bombay Computer Programming Computer Programming Dr. Deepak B Phatak Dr. Supratik Chakraborty Department of Computer Science and Engineering IIT Bombay Session: Friends and Static Members Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 1
Quick Recap of Relevant Topics Quick Recap of Relevant Topics IIT Bombay Object-oriented programming with structures and classes Accessing data members and member functions Constructors and destructors Function calls with structures/classes Operator overloading 2 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
Overview of This Lecture Overview of This Lecture IIT Bombay Friend classes and functions Static data members and static member functions 3 Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay
Acknowledgment Acknowledgment IIT Bombay Much of this lecture is motivated by the treatment in An Introduction to Programming Through C++ by Abhiram G. Ranade McGraw Hill Education 2014 Examples taken from this book are indicated in slides by the citation AGRBook Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 4
Friend Functions Friend Functions IIT Bombay Normally, private members of a class are accessible only to member functions of the class Data encapsulation or hiding Occasionally it may be desirable to bypass this access restriction for a few specific non-member functions Should these functions be made members of the class? Should we make all members of the class public? C++ provides a better solution: A friend declaration allows a class to explicitly allow specific non-member functions to access its private members Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 5
Friend Functions Friend Functions IIT Bombay class Point { private: double x, y; public: Member functions }; bool collinear(Point &p1, Point &p2, Point &p3) { // Not a member of class Point double temp; temp = p1.x*(p2.y p3.y) + p2.x*(p3.y p1.y) + p3.x* (p1.y p2.y); return (temp == 0); } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 6
Friend Functions Friend Functions IIT Bombay class Point { private: double x, y; public: friend bool collinear(Point &p1, Point &p2, Point &p3); Member functions }; Can be in public or private section of class Point bool collinear(Point &p1, Point &p2, Point &p3) { // Not a member of class Point double temp; temp = p1.x*(p2.y p3.y) + p2.x*(p3.y p1.y) + p3.x* (p1.y p2.y); return (temp == 0); } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 7
Friend Functions Friend Functions IIT Bombay class Point { private: double x, y; friend bool collinear(Point &p1, Point &p2, Point &p3); public: Member functions }; bool collinear(Point &p1, Point &p2, Point &p3) { // Not a member of class Point double temp; temp = p1.x*(p2.y p3.y) + p2.x*(p3.y p1.y) + p3.x* (p1.y p2.y); return (temp == 0); } Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 8
Friend Functions Friend Functions IIT Bombay In general, A function funccan be friend of several classes C1, C2, func can access private members of classes C1, C2, A class C can have several friend functions func1, func2, ... Each of func1, func2, can access private members of C Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 9
Friend Classes Friend Classes IIT Bombay Various members of class C1 may need access to private members of class C2 class Point { private: double x, y; public: Member functions }; class PointsInPlane { private: int numPoints; Point pointArray[100]; public: bool collinear(Point &p1, Point &p2, Point &p3) { } bool isEquiLateral(Point &p1, Point &p2, Point &p3) { } Other member functions }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 10
Friend Classes Friend Classes IIT Bombay Entire class C1can be declared friend of class C2 class Point { private: double x, y; public: friend class PointsInPlane; Member functions }; class PointsInPlane { private: int numPoints; Point pointArray[100]; public: bool collinear(Point &p1, Point &p2, Point &p3) { } bool isEquiLateral(Point &p1, Point &p2, Point &p3) { } Other member functions }; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 11
Static Data Members [Ref. Static Data Members [Ref. AGRBook AGRBook] ] IIT Bombay class Point { private: double x, y; public: static int count; Point() { count++; return; } Point(double a, double b) { x = a; y = b; count++; return; } }; C++ keyword int Point::count = 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 12
Static Data Members [Ref. Static Data Members [Ref. AGRBook AGRBook] ] IIT Bombay Declaration of static public data member class Point { private: double x, y; public: static int count; Point() { count++; return; } Point(double a, double b) { x = a; y = b; count++; return; } }; Single copy of static data member count shared across all objects of class Point Inside class Point, referred to as simply count int Point::count = 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 13
Static Data Members [Ref. Static Data Members [Ref. AGRBook AGRBook] ] IIT Bombay class Point { private: double x, y; public: static int count; Point() { count++; return; } Point(double a, double b) { x = a; y = b; count++; return; } }; Referring to member count of class Point Note use of scope resolution operator :: Necessary when referring to a member outside the class definition int Point::count = 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 14
Static Data Members [Ref. Static Data Members [Ref. AGRBook AGRBook] ] IIT Bombay class Point { private: double x, y; public: static int count; Point() { count++; return; } Point(double a, double b) { x = a; y = b; count++; return; } }; Creation and initialization of static public data member Note this is not tied to creation of objects of class Point int Point::count = 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 15
Static Data Members [Ref. Static Data Members [Ref. AGRBook AGRBook] ] IIT Bombay class Point { private: double x, y; public: static int count; Point() { count++; return; } Point(double a, double b) { x = a; y = b; count++; return; } }; int main () { Point a, b, c(0.0, 0.0); cout << Count of points ; cout << Point::count << endl; return 0; } All constructor calls update the same static data member. So this counts the number of points created. int Point::count = 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 16
Static Data Members [Ref. Static Data Members [Ref. AGRBook AGRBook] ] IIT Bombay class Point { private: double x, y; public: static int count; Point() { count++; return; } Point(double a, double b) { x = a; y = b; count++; return; } }; int main () { Point a, b, c(0.0, 0.0); cout << Count of points ; cout << Point::count << endl; return 0; } Accessing count outside the class Point requires scope resolution operator int Point::count = 0; Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 17
Static Member Functions [Ref. Static Member Functions [Ref. AGRBook AGRBook] ] IIT Bombay class Point { private: double x, y; static int count; public: Point() { count++; return; } Point(double a, double b) {x = a; y = b; count++; return;} static void resetCount() { count = 0; return; } void printCount() {cout << count << endl; return;} }; int Point::count; Creation of static private data member Declaration of static private data member Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 18
Static Member Functions [Ref. Static Member Functions [Ref. AGRBook AGRBook] ] IIT Bombay class Point { private: double x, y; static int count; public: Point() { count++; return; } Point(double a, double b) {x = a; y = b; count++; return;} static void resetCount() { count = 0; return; } void printCount() {cout << count << endl; return;} }; int Point::count; Declaration of static public member function Declaration of non-static public member function Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 19
Use of Static Member Functions Use of Static Member Functions IIT Bombay Static member function not invoked on object of class Point int main () { Point::resetCount(); Point a, b, c(0.0, 0.0); cout << Count of points ; cout << Point::count << endl; a.printCount(); return 0; } Invocation of static public member function in main Requires scope resolution operator Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 20
Summary Summary IIT Bombay Friend functions and friend classes and their usage Static data members, static member functions and their usage Dr. Deepak B. Phatak & Dr. Supratik Chakraborty, IIT Bombay 21