SlideShare a Scribd company logo
Object Oriented Programming
Lecture # 11
Dr. Shafiq Hussain
Associate Professor & Chairperson
Department of Computer Science
1
Learning Objective
• To demonstrate the concept of Friend Functions in OOP.
• To demonstrate the concept of Inline Functions in OOP.
• To demonstrate This Pointer in OOP.
• To demonstrate the Static Members in OOP.
2
Friend Functions
• A friend function of a class is defined outside that class' scope
but it has the right to access all private and protected members
of the class.
• Even though the prototypes for friend functions appear in the
class definition, friends are not member functions.
• A friend can be a function, function template, or member
function, or a class or class template, in which case the entire
class and all of its members are friends.
Friend Functions (Cont..)
To declare a function as a friend of a class, precede the function
prototype in the class definition with keyword friend as follows:
class Box
{
double width;
public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};
Friend Functions (Cont..)
To declare all member functions of class ClassTwo as friends of
class ClassOne, place a following declaration in the definition of
class ClassOne:
friend class ClassTwo;
Friend Functions (Cont..)
#include <iostream>
#include <conio.h>
using namespace std;
class Box
{
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
Friend Functions (Cont..)
void Box::setWidth( double wid )
{
width = wid;
}
// Note: printWidth() is not a member function of any class.
void printWidth( Box box )
{
/* Because printWidth() is a friend of Box, it can directly access any
member of this class */
cout << "Width of box : " << box.width <<endl;
}
Friend Functions (Cont..)
main( )
{
Box box;
// set box width without member function
box.setWidth(10.0);
// Use friend function to print the wdith.
printWidth( box );
getch();
}
Friend Functions (Cont..)
When the above code is compiled and executed, it produces the
following result:
• Width of box : 10
Inline Functions
• C++ inline function is powerful concept that is commonly
used with classes.
• If a function is inline, the compiler places a copy of the code
of that function at each point where the function is called at
compile time.
• Any change to an inline function could require all clients of
the function to be recompiled because compiler would need to
replace all the code once again otherwise it will continue with
old functionality.
Inline Functions (Cont..)
• To inline a function, place the keyword inline before the
function name and define the function before any calls are
made to the function. The compiler can ignore the inline
qualifier in case defined function is more than a line.
• A function definition in a class definition is an inline function
definition, even without the use of the inline specifier.
Inline Functions (Cont..)
#include <iostream>
#include <conio.h>
using namespace std;
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
main( )
{
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
getch();
}
Inline Functions (Cont..)
• Max (20,10): 20
• Max (0,200): 200
• Max (100,1010): 1010
This Pointer
• Every object in C++ has access to its own address through an
important pointer called this pointer.
• The this pointer is an implicit parameter to all member
functions.
• Therefore, inside a member function, this may be used to refer
to the invoking object.
This Pointer (Cont..)
• Friend functions do not have a this pointer, because friends are
not members of a class.
• Only member functions have a this pointer.
• The ‘this’ pointer is passed as a hidden argument to all non-
static member function calls and is available as a local variable
within the body of all nonstatic functions.
This Pointer (Cont..)
• The ‘this’ pointer is a constant pointer that holds the memory
address of the current object.
• The ‘this’ pointer is not available in static member functions as
static member functions can be called without any object (with
class name).
• ‘this’ pointer is used when local variable’s name is same as
member’s name
This Pointer (Cont..)
#include<iostream>
#include<conio.h>
using namespace std;
class Test
{
private:
int x;
public:
void setX (int x)
{
this->x = x;
}
void print()
{ cout << "x = " << x << endl; }
};
This Pointer (Cont..)
main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
getch();
}
Static Members
• We can define class members static using static keyword.
• When we declare a member of a class as static it means no
matter how many objects of the class are created, there is only
one copy of the static member.
• A static member is shared by all objects of the class. All static
data is initialized to zero when the first object is created, if no
other initialization is present
Static Members (Cont..)
#include <iostream>
#include <conio.h>
using namespace std;
class Box
{
public:
static int objectCount;
// Constructor definition
Static Members (Cont..)
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// Increase every time object is created
objectCount++;
}
Static Members (Cont..)
double Volume()
{
return length * breadth * height;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Initialize static member of class Box
int Box::objectCount = 0;
Static Members (Cont..)
main(void)
{
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
// Print total number of objects.
cout << "Total objects: " << Box::objectCount << endl;
getch();
}
Static Members (Cont..)
• By declaring a function member as static, you make it
independent of any particular object of the class.
• A static member function can be called even if no objects of
the class exist and the static functions are accessed using only
the class name and the scope resolution operator ::.
• A static member function can only access static data member,
other static member functions and any other functions from
outside the class.
Static Members (Cont..)
#include <iostream>
#include <conio.h>
using namespace std;
class Box
{
public:
static int objectCount;
Static Members (Cont..)
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// Increase every time object is created
objectCount++;
}
Static Members (Cont..)
double Volume()
{
return length * breadth * height;
}
static int getCount()
{
return objectCount;
}
Static Members (Cont..)
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Initialize static member of class Box
int Box::objectCount = 0;
Static Members (Cont..)
main(void)
{
// Print total number of objects before creating object.
cout << "Inital Stage Count: " << Box::getCount() << endl;
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
// Print total number of objects after creating object.
cout << "Final Stage Count: " << Box::getCount() << endl;
getch();
}
Questions
Any Question Please?
You can contact me at: drshafiq@uosahiwal.edu.pk
Your Query will be answered within one working day.
30
Further Readings
• C++ How to Program by Dietel, Chapter 6 and 9.
31
Thanks
32

More Related Content

PDF
Prompt Engineering - an Art, a Science, or your next Job Title?
PDF
Quality Engineering and Testing with TMAP in DevOps IT delivery
PPTX
Automation testing & Unit testing
PDF
Tutorial on Online User Engagement: Metrics and Optimization
PDF
Google & FIDO Authentication
PPTX
ALS= amyotrophe Lateralsklerose
PDF
API Testing
PPTX
Rest API Security
Prompt Engineering - an Art, a Science, or your next Job Title?
Quality Engineering and Testing with TMAP in DevOps IT delivery
Automation testing & Unit testing
Tutorial on Online User Engagement: Metrics and Optimization
Google & FIDO Authentication
ALS= amyotrophe Lateralsklerose
API Testing
Rest API Security

What's hot (20)

PDF
Test Driven Development
PDF
An Introduction To Automated API Testing
PPTX
Postman An Introduction for Testers, October 26 2022.pptx
PPS
Unit Testing
PDF
Gamestorming, not Brainstorming
PDF
API Lifecycle, Part 2: Monitor and Deploy an API
PPTX
CICD Pipeline Using Github Actions
PPTX
Automation testing
PPTX
Software testing metrics | David Tzemach
PPTX
Python in Test automation
PDF
API Security - Everything You Need to Know To Protect Your APIs
PDF
Rest api 테스트 수행가이드
PPTX
Negative Testing
PDF
4 Major Advantages of API Testing
PDF
Robot framework
PDF
OpenID Connect入門
PPTX
KeycloakでFAPIに対応した高セキュリティなAPIを公開する
PDF
Automate REST API Testing
PDF
SAML Protocol Overview
PDF
Customizing LLMs
Test Driven Development
An Introduction To Automated API Testing
Postman An Introduction for Testers, October 26 2022.pptx
Unit Testing
Gamestorming, not Brainstorming
API Lifecycle, Part 2: Monitor and Deploy an API
CICD Pipeline Using Github Actions
Automation testing
Software testing metrics | David Tzemach
Python in Test automation
API Security - Everything You Need to Know To Protect Your APIs
Rest api 테스트 수행가이드
Negative Testing
4 Major Advantages of API Testing
Robot framework
OpenID Connect入門
KeycloakでFAPIに対応した高セキュリティなAPIを公開する
Automate REST API Testing
SAML Protocol Overview
Customizing LLMs
Ad

Similar to Lecture-11 Friend Functions and inline functions.pptx (20)

PPTX
Class and object
PPT
Unit vi(dsc++)
PPT
DS Unit 6.ppt
PPTX
OOPs & C++ UNIT 3
PPTX
cse l 5.pptx
PPT
static member and static member fumctions.ppt
PPTX
ExamRevision_FinalSession_C++ notes.pptx
PDF
22 scheme OOPs with C++ BCS306B_module1.pdf
PPTX
object oriented programming language.pptx
PPTX
C++ Chapter 11 OOP - Part 2
PPTX
Classes and objects in c++
PPTX
Class and object
PPT
Classes in C++ computer language presentation.ppt
PPT
Mca 2nd sem u-2 classes & objects
PPTX
OOP C++
PPT
Bca 2nd sem u-2 classes & objects
PPTX
Data members and member functions
PPT
12-170211073924.ppthihello heoobchdhdhdhhd
PDF
A COMPLETE FILE FOR C++
PPT
friend function(c++)
Class and object
Unit vi(dsc++)
DS Unit 6.ppt
OOPs & C++ UNIT 3
cse l 5.pptx
static member and static member fumctions.ppt
ExamRevision_FinalSession_C++ notes.pptx
22 scheme OOPs with C++ BCS306B_module1.pdf
object oriented programming language.pptx
C++ Chapter 11 OOP - Part 2
Classes and objects in c++
Class and object
Classes in C++ computer language presentation.ppt
Mca 2nd sem u-2 classes & objects
OOP C++
Bca 2nd sem u-2 classes & objects
Data members and member functions
12-170211073924.ppthihello heoobchdhdhdhhd
A COMPLETE FILE FOR C++
friend function(c++)
Ad

Recently uploaded (20)

PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
Paper PDF World Game (s) Great Redesign.pdf
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPTX
SAP Ariba Sourcing PPT for learning material
PPTX
introduction about ICD -10 & ICD-11 ppt.pptx
PDF
WebRTC in SignalWire - troubleshooting media negotiation
PPTX
Funds Management Learning Material for Beg
PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PDF
The Internet -By the Numbers, Sri Lanka Edition
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
PptxGenJS_Demo_Chart_20250317130215833.pptx
PPTX
Introuction about ICD -10 and ICD-11 PPT.pptx
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPTX
Introduction to Information and Communication Technology
PDF
Sims 4 Historia para lo sims 4 para jugar
DOCX
Unit-3 cyber security network security of internet system
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PPTX
artificial intelligence overview of it and more
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
Design_with_Watersergyerge45hrbgre4top (1).ppt
Paper PDF World Game (s) Great Redesign.pdf
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
SAP Ariba Sourcing PPT for learning material
introduction about ICD -10 & ICD-11 ppt.pptx
WebRTC in SignalWire - troubleshooting media negotiation
Funds Management Learning Material for Beg
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
The Internet -By the Numbers, Sri Lanka Edition
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PptxGenJS_Demo_Chart_20250317130215833.pptx
Introuction about ICD -10 and ICD-11 PPT.pptx
Introuction about WHO-FIC in ICD-10.pptx
Introduction to Information and Communication Technology
Sims 4 Historia para lo sims 4 para jugar
Unit-3 cyber security network security of internet system
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
artificial intelligence overview of it and more
An introduction to the IFRS (ISSB) Stndards.pdf

Lecture-11 Friend Functions and inline functions.pptx

  • 1. Object Oriented Programming Lecture # 11 Dr. Shafiq Hussain Associate Professor & Chairperson Department of Computer Science 1
  • 2. Learning Objective • To demonstrate the concept of Friend Functions in OOP. • To demonstrate the concept of Inline Functions in OOP. • To demonstrate This Pointer in OOP. • To demonstrate the Static Members in OOP. 2
  • 3. Friend Functions • A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. • Even though the prototypes for friend functions appear in the class definition, friends are not member functions. • A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.
  • 4. Friend Functions (Cont..) To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows: class Box { double width; public: double length; friend void printWidth( Box box ); void setWidth( double wid ); };
  • 5. Friend Functions (Cont..) To declare all member functions of class ClassTwo as friends of class ClassOne, place a following declaration in the definition of class ClassOne: friend class ClassTwo;
  • 6. Friend Functions (Cont..) #include <iostream> #include <conio.h> using namespace std; class Box { double width; public: friend void printWidth( Box box ); void setWidth( double wid ); };
  • 7. Friend Functions (Cont..) void Box::setWidth( double wid ) { width = wid; } // Note: printWidth() is not a member function of any class. void printWidth( Box box ) { /* Because printWidth() is a friend of Box, it can directly access any member of this class */ cout << "Width of box : " << box.width <<endl; }
  • 8. Friend Functions (Cont..) main( ) { Box box; // set box width without member function box.setWidth(10.0); // Use friend function to print the wdith. printWidth( box ); getch(); }
  • 9. Friend Functions (Cont..) When the above code is compiled and executed, it produces the following result: • Width of box : 10
  • 10. Inline Functions • C++ inline function is powerful concept that is commonly used with classes. • If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. • Any change to an inline function could require all clients of the function to be recompiled because compiler would need to replace all the code once again otherwise it will continue with old functionality.
  • 11. Inline Functions (Cont..) • To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line. • A function definition in a class definition is an inline function definition, even without the use of the inline specifier.
  • 12. Inline Functions (Cont..) #include <iostream> #include <conio.h> using namespace std; inline int Max(int x, int y) { return (x > y)? x : y; } main( ) { cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; getch(); }
  • 13. Inline Functions (Cont..) • Max (20,10): 20 • Max (0,200): 200 • Max (100,1010): 1010
  • 14. This Pointer • Every object in C++ has access to its own address through an important pointer called this pointer. • The this pointer is an implicit parameter to all member functions. • Therefore, inside a member function, this may be used to refer to the invoking object.
  • 15. This Pointer (Cont..) • Friend functions do not have a this pointer, because friends are not members of a class. • Only member functions have a this pointer. • The ‘this’ pointer is passed as a hidden argument to all non- static member function calls and is available as a local variable within the body of all nonstatic functions.
  • 16. This Pointer (Cont..) • The ‘this’ pointer is a constant pointer that holds the memory address of the current object. • The ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name). • ‘this’ pointer is used when local variable’s name is same as member’s name
  • 17. This Pointer (Cont..) #include<iostream> #include<conio.h> using namespace std; class Test { private: int x; public: void setX (int x) { this->x = x; } void print() { cout << "x = " << x << endl; } };
  • 18. This Pointer (Cont..) main() { Test obj; int x = 20; obj.setX(x); obj.print(); getch(); }
  • 19. Static Members • We can define class members static using static keyword. • When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. • A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present
  • 20. Static Members (Cont..) #include <iostream> #include <conio.h> using namespace std; class Box { public: static int objectCount; // Constructor definition
  • 21. Static Members (Cont..) Box(double l=2.0, double b=2.0, double h=2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; // Increase every time object is created objectCount++; }
  • 22. Static Members (Cont..) double Volume() { return length * breadth * height; } private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; // Initialize static member of class Box int Box::objectCount = 0;
  • 23. Static Members (Cont..) main(void) { Box Box1(3.3, 1.2, 1.5); // Declare box1 Box Box2(8.5, 6.0, 2.0); // Declare box2 // Print total number of objects. cout << "Total objects: " << Box::objectCount << endl; getch(); }
  • 24. Static Members (Cont..) • By declaring a function member as static, you make it independent of any particular object of the class. • A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::. • A static member function can only access static data member, other static member functions and any other functions from outside the class.
  • 25. Static Members (Cont..) #include <iostream> #include <conio.h> using namespace std; class Box { public: static int objectCount;
  • 26. Static Members (Cont..) Box(double l=2.0, double b=2.0, double h=2.0) { cout <<"Constructor called." << endl; length = l; breadth = b; height = h; // Increase every time object is created objectCount++; }
  • 27. Static Members (Cont..) double Volume() { return length * breadth * height; } static int getCount() { return objectCount; }
  • 28. Static Members (Cont..) private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; // Initialize static member of class Box int Box::objectCount = 0;
  • 29. Static Members (Cont..) main(void) { // Print total number of objects before creating object. cout << "Inital Stage Count: " << Box::getCount() << endl; Box Box1(3.3, 1.2, 1.5); // Declare box1 Box Box2(8.5, 6.0, 2.0); // Declare box2 // Print total number of objects after creating object. cout << "Final Stage Count: " << Box::getCount() << endl; getch(); }
  • 30. Questions Any Question Please? You can contact me at: drshafiq@uosahiwal.edu.pk Your Query will be answered within one working day. 30
  • 31. Further Readings • C++ How to Program by Dietel, Chapter 6 and 9. 31