Download as pdf or txt
Download as pdf or txt
You are on page 1of 148

Module M16

L
Partha Pratim
Das
Programming in Modern C++

E
Weekly Recap

Objectives & Module M16: static Members

T
Outline

static Data

P
Member
Example
Print Task Partha Pratim Das

N
Order of Initialization

static Member
function Department of Computer Science and Engineering
Print Task Indian Institute of Technology, Kharagpur
Count Objects

Comparison ppd@cse.iitkgp.ac.in
Singleton Class

Module Summary All url’s in this module have been accessed in September, 2021 and found to be functional

Programming in Modern C++ Partha Pratim Das M16.1


Weekly Recap

Module M16

• Understood the core OOP features of C++: Class, Object, Attributes (Data members),

L
Partha Pratim
Das
Methods (Member functions), State of an Object, Encapsulation by Access Specifiers,

E
Weekly Recap get-set idioms for Information Hiding, various types of Constructors, Destructors, and
Objectives &
copy mechanisms in terms of deep & shallow copy

T
Outline

static Data • Understood the lifetime aspects of objects

P
Member
Example
Print Task
• Understood how bit-wise const-ness can be modeled for objects and leveraged in the

N
Order of Initialization design with const objects, data members, and member functions, and how mutable
static Member
function
can help model logical const-ness
Print Task
Count Objects

Comparison

Singleton Class

Module Summary

Programming in Modern C++ Partha Pratim Das M16.2


Module Objectives

Module M16

• Understand static data member and member function

L
Partha Pratim
Das

E
Weekly Recap

Objectives &

T
Outline

static Data

P
Member
Example
Print Task

N
Order of Initialization

static Member
function
Print Task
Count Objects

Comparison

Singleton Class

Module Summary

Programming in Modern C++ Partha Pratim Das M16.3


Module Outline

Module M16

1 Weekly Recap

L
Partha Pratim
Das

static Data Member

E
Weekly Recap 2
Objectives & Example

T
Outline

static Data
Print Task
Order of Initialization

P
Member
Example
Print Task

N
Order of Initialization 3 static Member function
static Member
function
Print Task
Print Task Count Objects
Count Objects

Comparison 4 Comparison
Singleton Class

Module Summary 5 Singleton Class


6 Module Summary
Programming in Modern C++ Partha Pratim Das M16.4
static Data Member

Module M16

L
Partha Pratim
Das

E
Weekly Recap

Objectives &

T
Outline

static Data

P
Member
Example
Print Task

N
Order of Initialization

static Member
function
Print Task
Count Objects

Comparison

Singleton Class
static Data Member
Module Summary

Programming in Modern C++ Partha Pratim Das M16.5


static Data Member

Module M16 • A static data member


◦ is associated with class not with object

L
Partha Pratim
Das
◦ is shared by all the objects of a class

E
Weekly Recap
◦ needs to be defined outside the class scope (in addition to the declaration within the
Objectives &
class scope) to avoid linker error

T
Outline

static Data ◦ must be initialized in a source file

P
Member
Example ◦ is constructed before main() starts and destructed after main() ends
Print Task
◦ can be private / public

N
Order of Initialization

static Member ◦ can be accessed


function
Print Task . with the class-name followed by the scope resolution operator (::)
Count Objects
. as a member of any object of the class
Comparison

Singleton Class
◦ virtually eliminates any need for global variables in OOPs environment
Module Summary • We illustrate first with a simple example and then with a Print Task where:
◦ There is a printer which can be loaded with a paper from time to time
◦ Several print jobs (each requiring a number of pages) may be fired on the printer
Programming in Modern C++ Partha Pratim Das M16.6
Program 16.01: static Data Member: Example
Non static Data Member static Data Member
Module M16
#include<iostream> #include<iostream>

L
Partha Pratim using namespace std; using namespace std;
Das
class MyClass { int x; // Non-static class MyClass { static int x; // Declare static
public: public:

E
Weekly Recap
void get() { x = 15; } void get() { x = 15; }
Objectives & void print() { x = x + 10; void print() { x = x + 10;

T
Outline
cout << "x =" << x << endl ; cout << "x =" << x << endl;
static Data } }

P
Member }; };
Example
int MyClass::x = 0; // Define static data member
Print Task
int main() { int main() {

N
Order of Initialization
MyClass obj1, obj2; // Have distinct x MyClass obj1, obj2; // Have same x
static Member obj1.get(); obj2.get(); obj1.get(); obj2.get();
function
obj1.print(); obj2.print(); obj1.print(); obj2.print();
Print Task
} }
Count Objects
--- ---
Comparison x = 25 , x = 25 x = 25 , x = 35
Singleton Class
• x is a non-static data member • x is static data member
Module Summary
• x cannot be shared between obj1 & obj2 • x is shared by all MyClass objects including obj1 & obj2
• Non-static data members do not need separate def- • static data members must be defined in the global scope
initions - instantiated with the object
• Non-static data members are initialized during ob- • static data members are initialized during program start-
ject construction up
Programming in Modern C++ Partha Pratim Das M16.7
Program 16.02: static Data Member:
Print Task (Unsafe)
#include <iostream>
Module M16 using namespace std;
class PrintJobs { int nPages_; /* # of pages in current job */ public:

L
Partha Pratim
Das static int nTrayPages_; /* # of pages in the tray */ static int nJobs_; // # of print jobs executing
PrintJobs(int nP): nPages_(nP) { ++nJobs_; cout << "Printing " << nP << " pages" << endl;

E
Weekly Recap nTrayPages_ = nTrayPages_ - nP;
Objectives &
} // Job started

T
Outline ~PrintJobs() { --nJobs_; } // Job done
};
static Data
int PrintJobs::nTrayPages_ = 500; // Definition and initialization -- load paper

P
Member
Example int PrintJobs::nJobs_ = 0; // Definition and initialization -- no job to start with
Print Task int main() {

N
Order of Initialization cout << "Jobs = " << PrintJobs::nJobs_ << endl; Output:
static Member
cout << "Pages= " << PrintJobs::nTrayPages_ << endl;
function PrintJobs job1(10); Jobs = 0
Print Task cout << "Jobs = " << PrintJobs::nJobs_ << endl; Pages= 500
Count Objects cout << "Pages= " << PrintJobs::nTrayPages_ << endl; Printing 10 pages
Comparison { Jobs = 1 // same nJobs_, nTrayPages_
PrintJobs job1(30), job2(20); // Different job1 in block scope Pages= 490
Singleton Class
cout << "Jobs = " << PrintJobs::nJobs_ << endl; Printing 30 pages
Module Summary cout << "Pages= " << PrintJobs::nTrayPages_ << endl; Printing 20 pages
PrintJobs::nTrayPages_ += 100; // Load 100 more pages Jobs = 3 // same nJobs_, nTrayPages_
} Pages= 440
cout << "Jobs = " << PrintJobs::nJobs_ << endl; Jobs = 1 // same nJobs_, nTrayPages_
cout << "Pages= " << PrintJobs::nTrayPages_ << endl; Pages= 540
} Programming in Modern C++ Partha Pratim Das M16.8
Program 16.03/04: Order of Initialization: Order of Definitions
#include <iostream> #include <iostream>
Module M16
#include <string> #include <string>
using namespace std; using namespace std;

L
Partha Pratim
Das class Data { string id_; public: class Data { string id_; public:
Data(const string& id) : id_(id) Data(const string& id) : id_(id)

E
Weekly Recap { cout << "Construct: " << id_ << endl; } { cout << "Construct: " << id_ << endl; }
Objectives & ~Data() ~Data()

T
Outline { cout << "Destruct: " << id_ << endl; } { cout << "Destruct: " << id_ << endl; }
static Data }; };
class MyClass { class MyClass {

P
Member
Example static Data d1_; // Listed 1st static Data d2_; // Order of static members swapped
Print Task static Data d2_; // Listed 2nd static Data d1_;

N
Order of Initialization }; };
static Member Data MyClass::d1_("obj_1"); // Constructed 1st Data MyClass::d1_("obj_1"); // Constructed 1st
function Data MyClass::d2_("obj_2"); // Constructed 2nd Data MyClass::d2_("obj_2"); // Constructed 2nd
Print Task
Count Objects int main() { } int main() { }
Comparison ----- -----
Construct: obj_1 Construct: obj_1
Singleton Class
Construct: obj_2 Construct: obj_2
Module Summary Destruct: obj_2 Destruct: obj_2
Destruct: obj_1 Destruct: obj_1
• Order of initialization of static data members does not depend on their order in the definition of the class. It depends
on the order their definition and initialization in the source
Programming in Modern C++ Partha Pratim Das M16.9
static Member function

Module M16

L
Partha Pratim
Das

E
Weekly Recap

Objectives &

T
Outline

static Data

P
Member
Example
Print Task

N
Order of Initialization

static Member
function
Print Task
Count Objects

Comparison

Singleton Class
static Member function
Module Summary

Programming in Modern C++ Partha Pratim Das M16.10


static Member Function

Module M16

• A static member function

L
Partha Pratim
Das
◦ does not have this pointer – not associated with any object

E
Weekly Recap
◦ cannot access non-static data members
Objectives &
◦ cannot invoke non-static member functions

T
Outline

static Data ◦ can be accessed

P
Member
Example . with the class-name followed by the scope resolution operator (::)
Print Task
. as a member of any object of the class

N
Order of Initialization

static Member
function
◦ is needed to read / write static data members
Print Task . Again, for encapsulation static data members should be private
Count Objects

Comparison
. get()-set() idiom is built for access (static member functions in public)
Singleton Class ◦ may initialize static data members even before any object creation
Module Summary ◦ cannot co-exist with a non-static version of the same function
◦ cannot be declared as const
• We repeat the Print Task with better (safer) modeling and coding
Programming in Modern C++ Partha Pratim Das M16.11
Program 16.05: static Data & Member Function:
Print Task (Safe)
Module M16
// #include <iostream> using namespace std;
class PrintJobs { int nPages_; // # of pages in current job

L
Partha Pratim static int nTrayPages_; /* # of pages in the tray */ static int nJobs_; // # of print jobs executing
Das
public: PrintJobs(int nP) : nPages_(nP) { ++nJobs_; cout << "Printing " << nP << " pages" << endl;
nTrayPages_ = nTrayPages_ - nP; } // Job started

E
Weekly Recap
~PrintJobs() { --nJobs_; } // Job done
Objectives & static int getJobs() { return nJobs_; } // get on nJobs_. Readonly. No set provided

T
Outline
static int checkPages() { return nTrayPages_; } // get on nTrayPages_
static Data static void loadPages(int nP) { nTrayPages_ += nP; } // set on nTrayPages_

P
Member };
Example
int PrintJobs::nTrayPages_ = 500; // Definition and initialization -- load paper
Print Task
int PrintJobs::nJobs_ = 0; // Definition and initialization -- no job to start with

N
Order of Initialization
int main() { cout << "Jobs = " << PrintJobs::getJobs() << endl;
static Member cout << "Pages= " << PrintJobs::checkPages() << endl; Output:
function
PrintJobs job1(10);
Print Task
cout << "Jobs = " << PrintJobs::getJobs() << endl; Jobs = 0
Count Objects
cout << "Pages= " << PrintJobs::checkPages() << endl; Pages= 500
Comparison { Printing 10 pages
Singleton Class PrintJobs job1(30), job2(20); // Different job1 in block scope Jobs = 1 // same nJobs_, nTrayPages_
cout << "Jobs = " << PrintJobs::getJobs() << endl; Pages= 490
Module Summary
cout << "Pages= " << PrintJobs::checkPages() << endl; Printing 30 pages
PrintJobs::loadPages(100); // Load 100 more pages Printing 20 pages
} Jobs = 3 // same nJobs_, nTrayPages_
cout << "Jobs = " << PrintJobs::getJobs() << endl; Pages= 440
cout << "Pages= " << PrintJobs::checkPages() << endl; Jobs = 1 // same nJobs_, nTrayPages_
} Programming in Modern C++ Partha Pratim Das Pages= 540 M16.12
Counting Objects

Module M16

• We illustrate another example and use for static data member and member function

L
Partha Pratim
Das
◦ Here we want to track the number of objects created and destroyed for a class at

E
Weekly Recap
any point in the program
Objectives &
◦ Naturally no object can keep this information. So we hold two static data

T
Outline

static Data members

P
Member
Example . nObjCons : Number of objects created since beginning. It is read-only and
Print Task
incremented in every constructor

N
Order of Initialization

static Member . nObjDes : Number of objects destroyed since beginning. It is read-only and
function
Print Task
incremented in the destructor
Count Objects
◦ At any point (nObjCons - nObjDes ) gives the number of Live objects
Comparison

Singleton Class
◦ In an alternate (less informative model) we may just maintain static data member
Module Summary
nLive which is incremented in every constructor and decremented in the destructor

Programming in Modern C++ Partha Pratim Das M16.13


Program 16.06: Count Objects
#include <iostream> int dummy1(MyClass::getObjLive()); // Before (main())
Module M16
#include <string> MyClass sObj("sObj");
using namespace std; int dummy2(MyClass::getObjLive()); // Before (main())

L
Partha Pratim
Das class MyClass { string id_; // Object ID int main() { MyClass::getObjLive();
static int nObjCons_, nObjDes_; // Object history MyClass aObj("aObj");

E
Weekly Recap public: MyClass *dObj = new MyClass("dObj");
Objectives & MyClass(const string& id) : id_(id) {

T
Outline { ++nObjCons_; MyClass bObj("bObj");
static Data cout << "ctor: " << id_ << " "; getObjLive(); } delete dObj;
}

P
Member ~MyClass() { ++nObjDes_;
Example cout << "dtor: " << id_ << " "; getObjLive(); } MyClass::getObjLive();
Print Task static int getObjConstructed() }

N
Order of Initialization
{ return nObjCons_; }
static Member static int getObjDestructed() Live Objects = 0 // Before any object (dummy1)
function { return nObjDes_; } ctor: sObj Live Objects = 1
Print Task Live Objects = 1 // Before main() (dummy2)
// Get number of live objects
Count Objects
static int getObjLive() { Live Objects = 1 // Enter main()
Comparison int nLive = nObjCons_ - nObjDes_; ctor: aObj Live Objects = 2
Singleton Class
cout << "Live Objects = " << nLive << endl; ctor: dObj Live Objects = 3
return nLive; ctor: bObj Live Objects = 4
Module Summary } dtor: dObj Live Objects = 3
}; dtor: bObj Live Objects = 2
int MyClass::nObjCons_ = 0; Live Objects = 2 // Exit main()
int MyClass::nObjDes_ = 0; : aObj Live Objects = 1
dtor: sObj Live Objects = 0 // After all objecst
Programming in Modern C++ Partha Pratim Das M16.14
static vis-a-vis non-static

Module M16

L
Partha Pratim
Das

E
Weekly Recap

Objectives &

T
Outline

static Data

P
Member
Example
Print Task

N
Order of Initialization

static Member
function
Print Task
Count Objects

Comparison

Singleton Class
static vis-a-vis non-static
Module Summary

Programming in Modern C++ Partha Pratim Das M16.15


Comparison of static vis-a-vis non-static
static Data Members Non-static Data Members
Module M16
• Declared using keyword static • Declared without using keyword static

L
Partha Pratim • All objects of a class share the same copy / instance • Each object of the class gets its own copy / instance
Das
• Accessed using the class name or object • Accessed only through an object of the class
• May be public or private • May be public or private

E
Weekly Recap
• Belongs to the namespace of the class • Belongs to the namespace of the class
Objectives & • May be const • May be const

T
Outline
• Are constructed before main() is invoked • Are constructed during object construction
static Data • Are destructed after (in reverse order) main() returns • Are destructed during object destruction

P
Member • Are constructed in the order of definitions in source • Are constructed in the order of listing in the class
Example
• Has a lifetime encompassing main() • Has a lifetime as of the lifetime of the object
Print Task
• Allocated in static memory • Allocated in static, stack, or heap memory as of the object

N
Order of Initialization

static Member static Member Functions Non-static Member Functions


function
Print Task • Declared using keyword static • Declared without using keyword static
Count Objects • Has no this pointer parameter • Has an implicit this pointer parameter
Comparison
• Invoked using the class name or object • Invoked only through an object of the class
• May be public or private • May be public or private
Singleton Class • Belongs to the namespace of the class • Belongs to the namespace of the class
Module Summary • Can access static data members and methods • Can access static data members and methods
• Cannot access non-static data members or methods • Can access non-static data members and methods
• Can be invoked anytime during program execution • Can be invoked only during lifetime of the object
• Cannot be virtual or const • May be virtual and / or const
• Constructor is static though not declared static • There cannot be a non-static Constructor
Programming in Modern C++ Partha Pratim Das M16.16
Singleton Class

Module M16

L
Partha Pratim
Das

E
Weekly Recap

Objectives &

T
Outline

static Data

P
Member
Example
Print Task

N
Order of Initialization

static Member
function
Print Task
Count Objects

Comparison

Singleton Class
Singleton Class
Module Summary

Programming in Modern C++ Partha Pratim Das M16.17


Singleton Class

Module M16

• Singleton is a creational design pattern

L
Partha Pratim
Das
◦ ensures that only one object of its kind exists and

E
Weekly Recap
◦ provides a single point of access to it for any other code
Objectives &

T
Outline • A class is called a Singleton if it satisfies the above conditions
static Data
• Many classes are singleton:

P
Member
Example
Print Task ◦ President of India

N
Order of Initialization
◦ Prime Minister of India
static Member
function ◦ Director of IIT Kharagpur
Print Task
Count Objects
◦ CEO of a Company
Comparison ◦ ...
Singleton Class • How to implement a Singleton Class?
Module Summary
• How to restrict that user can created only one instance?

Programming in Modern C++ Partha Pratim Das M16.18


Program 16.07: static Data & Member Function
Singleton Printer
Module M16 #include <iostream>
using namespace std;

L
Partha Pratim
Das
class Printer { /* THIS IS A SINGLETON PRINTER -- ONLY ONE INSTANCE */

E
Weekly Recap private: bool blackAndWhite_, bothSided_;
Objectives & Printer(bool bw = false, bool bs = false) : blackAndWhite_(bw), bothSided_(bs)

T
Outline { cout << "Printer constructed" << endl; } // Private -- Printer cannot be constructed!
static Data
static Printer *myPrinter_; // Pointer to the Instance of the Singleton Printer
public: ~Printer() { cout << "Printer destructed" << endl; }

P
Member
Example static const Printer& printer(bool bw = false, bool bs = false) { // Access the Printer
Print Task if (!myPrinter_) myPrinter_ = new Printer(bw, bs); // Constructed for first call

N
Order of Initialization return *myPrinter_; // Reused from next time
static Member }
function void print(int nP) const { cout << "Printing " << nP << " pages" << endl; }
Print Task };
Count Objects

Comparison Printer *Printer::myPrinter_ = 0;


Singleton Class
int main() { Output:
Module Summary Printer::printer().print(10);
Printer::printer().print(20); Printer constructed
Printing 10 pages
delete &Printer::printer(); Printing 20 pages
} Printer destructed
Programming in Modern C++ Partha Pratim Das M16.19
Program 16.08: Using function-local static Data
Singleton Printer
Module M16 #include <iostream>
using namespace std;

L
Partha Pratim
Das
class Printer { /* THIS IS A SINGLETON PRINTER -- ONLY ONE INSTANCE */
bool blackAndWhite_, bothSided_;

E
Weekly Recap
Printer(bool bw = false, bool bs = false) : blackAndWhite_(bw), bothSided_(bs)
Objectives &
{ cout << "Printer constructed" << endl; }

T
Outline
~Printer() { cout << "Printer destructed" << endl; }
static Data public:

P
Member
static const Printer& printer(bool bw = false, bool bs = false) {
Example
Print Task
static Printer myPrinter(bw, bs); // The Singleton -- constructed the first time

N
Order of Initialization
return myPrinter;
static Member
function
}
Print Task
void print(int nP) const { cout << "Printing " << nP << " pages" << endl; }
Count Objects
};
int main() { Output:
Comparison
Printer::printer().print(10);
Singleton Class Printer::printer().print(20); Printer constructed
} Printing 10 pages
Module Summary Printing 20 pages
Printer destructed
• Function local static object is used
• No memory management overhead – so destructor too get private
• This is called Meyer’s Singleton
Programming in Modern C++ Partha Pratim Das M16.20
Module Summary

Module M16

• Introduced static data member

L
Partha Pratim
Das
• Introduced static member function

E
Weekly Recap

Objectives &
• Exposed to use of static members

T
Outline

static Data
• Singleton Class discussed

P
Member
Example
Print Task

N
Order of Initialization

static Member
function
Print Task
Count Objects

Comparison

Singleton Class

Module Summary

Programming in Modern C++ Partha Pratim Das M16.21


Module M17

L
Partha Pratim
Das
Programming in Modern C++

E
Module Recap

Objectives & Module M17: friend Function and friend Class

T
Outlines

friend Function

P
Matrix-Vector
Multiplication
Linked List
Partha Pratim Das

N
friend Class
Linked List
Iterator Department of Computer Science and Engineering
Properties
Indian Institute of Technology, Kharagpur

Comparison ppd@cse.iitkgp.ac.in
Module Summary

All url’s in this module have been accessed in September, 2021 and found to be functional

Programming in Modern C++ Partha Pratim Das M17.1


Module Recap

Module M17

• Introduced static data member

L
Partha Pratim
Das
• Introduced static member function

E
Module Recap

Objectives &
• Exposed to use of static members

T
Outlines

friend Function
• Singleton Class discussed

P
Matrix-Vector
Multiplication
Linked List

N
friend Class
Linked List
Iterator

Properties

Comparison

Module Summary

Programming in Modern C++ Partha Pratim Das M17.2


Module Recap

Module M17

• Introduced static data member

L
Partha Pratim
Das
• Introduced static member function

E
Module Recap

Objectives &
• Exposed to use of static members

T
Outlines

friend Function
• Singleton Class discussed

P
Matrix-Vector
Multiplication
Linked List

N
friend Class
Linked List
Iterator

Properties

Comparison

Module Summary

Programming in Modern C++ Partha Pratim Das M17.3


Module Objectives

Module M17

• Understand friend function and class

L
Partha Pratim
Das

E
Module Recap

Objectives &

T
Outlines

friend Function

P
Matrix-Vector
Multiplication
Linked List

N
friend Class
Linked List
Iterator

Properties

Comparison

Module Summary

Programming in Modern C++ Partha Pratim Das M17.4


Module Outline

Module M17

1 Module Recap

L
Partha Pratim
Das

E
Module Recap 2 friend Function
Objectives &
Matrix-Vector Multiplication

T
Outlines

friend Function Linked List

P
Matrix-Vector
Multiplication
Linked List 3 friend Class

N
friend Class
Linked List
Linked List
Iterator Iterator
Properties

Comparison 4 Properties of friend


Module Summary

5 Comparison

6 Module Summary

Programming in Modern C++ Partha Pratim Das M17.5


friend Function

Module M17

L
Partha Pratim
Das

E
Module Recap

Objectives &

T
Outlines

friend Function

P
Matrix-Vector
Multiplication
Linked List

N
friend Class
Linked List
Iterator

Properties

Comparison

Module Summary friend Function

Programming in Modern C++ Partha Pratim Das M17.6


Program 17.01: friend function: Basic Notion
Ordinary function friend function
Module M17
#include<iostream> #include<iostream>

L
Partha Pratim
Das using namespace std; using namespace std;
class MyClass { int data_; class MyClass { int data_;

E
Module Recap public: public:
Objectives &
MyClass(int i) : data_(i) { } MyClass(int i) : data_(i) { }

T
Outlines

friend Function
friend void display(const MyClass& a);
}; };

P
Matrix-Vector
Multiplication void display(const MyClass& a) { // gbl. func. void display(const MyClass& a) { // global function
Linked List cout << "data = " << a.data_; // Error 1 cout << "data = " << a.data_; // Okay

N
friend Class } }
Linked List int main() { int main() {
Iterator MyClass obj(10); MyClass obj(10);
Properties
display(obj); display(obj);
Comparison } }
Module Summary
• display() is a non-member function • display() is a non-member function; but friend to class
MyClass
• Error 1: ’MyClass::data ’ : cannot • Able to access data even though it is private in class
access private member declared in class MyClass
’MyClass’
• Output: data = 10
Programming in Modern C++ Partha Pratim Das M17.7
friend function

Module M17

• A friend function of a class

L
Partha Pratim
Das
◦ has access to the private and protected members of the class (breaks the

E
Module Recap
encapsulation) in addition to public members
Objectives &
◦ must have its prototype included within the scope of the class prefixed with the

T
Outlines

friend Function keyword friend

P
Matrix-Vector
Multiplication ◦ does not have its name qualified with the class scope
Linked List
◦ is not called with an invoking object of the class

N
friend Class
Linked List ◦ can be declared friend in more than one classes
Iterator

Properties
• A friend function can be a
Comparison ◦ global function
Module Summary ◦ a member function of a class
◦ a function template

Programming in Modern C++ Partha Pratim Das M17.8


Program 17.02: Multiply a Matrix with a Vector
#include <iostream> class Matrix { int e_[3][3]; int m_, n_; public:
Module M17 using namespace std; Matrix(int m, int n) : m_(m), n_(n) { // Arbitrary
for(int i = 0; i < m_; ++i) // init.

L
Partha Pratim
Das class Matrix; // Forward declaration for(int j = 0; j < n_; ++j) e_[i][j] = i + j;
}

E
Module Recap class Vector { int e_[3]; int n_; public: void Show() { // Show the matrix
Objectives & Vector(int n) : n_(n) { for (int i = 0; i < m_; ++i) {

T
Outlines for (int i = 0; i < n_; ++i) // Arbitrary for (int j = 0; j < n_; ++j)
friend Function
e_[i] = i + 1; // init. cout << e_[i][j] << " ";
} cout << endl;

P
Matrix-Vector
Multiplication void Clear() { // Set a zero vector } cout << endl;
Linked List for(int i = 0; i < n_; ++i) }

N
friend Class e_[i] = 0; friend Vector Prod(Matrix *pM, Vector *pV);
Linked List } };
Iterator void Show() { // Show the vector Vector Prod(Matrix *pM, Vector *pV) {
Properties
for(int i = 0; i < n_; ++i) Vector v(pM->m_); v.Clear();
cout << e_[i] << " "; for(int i = 0; i < pM->m_; i++)
Comparison cout << endl << endl; for(int j = 0; j < pM->n_; j++)
Module Summary } v.e_[i] += pM->e_[i][j] * pV->e_[j];
friend Vector Prod(Matrix *pM, Vector *pV); return v;
}; }
• Vector Prod(Matrix*, Vector*); is a global function
• Vector Prod(Matrix*, Vector*); is friend of class Vector as well as class Matrix
• This function accesses the private data members of both these classes
Programming in Modern C++ Partha Pratim Das M17.9
Program 17.02: Multiply a Matrix with a Vector

Module M17
int main() { Output:

L
Partha Pratim Matrix M(2, 3);
Das 0 1 2 // Matrix M
Vector V(3);
1 2 3

E
Module Recap
Vector PV = Prod(&M, &V);
Objectives & 1 2 3 // Vector V

T
Outlines
M.Show();
friend Function V.Show(); 8 14 // Product Vector PV

P
Matrix-Vector PV.Show();
Multiplication
Linked List
return 0;

N
friend Class }
Linked List
Iterator

Properties • Vector Prod(Matrix*, Vector*); is a global function


• Vector Prod(Matrix*, Vector*); is friend of class Vector as well as class Matrix
Comparison
• This function accesses the private data members of both these classes
Module Summary

Programming in Modern C++ Partha Pratim Das M17.10


Program 17.03: Linked List

Module M17
#include <iostream> void List::display() { // friend of Node
using namespace std; Node *ptr = head;
while (ptr) { cout << ptr->info << " ";

L
Partha Pratim
Das class Node; // Forward declaration ptr = ptr->next;
class List { }

E
Module Recap
Node *head; // Head of the list }
Objectives & Node *tail; // Tail of the list void List::append(Node *p) { // friend of Node

T
Outlines public: if (!head) head = tail = p;
friend Function List(Node *h = 0): head(h), tail(h) { } else {

P
Matrix-Vector void display(); tail->next = p;
Multiplication
void append(Node *p); tail = tail->next;
Linked List
}; }

N
friend Class class Node { }
Linked List int info; // Data of the node int main() { List l; // Init. null list
Iterator Node *next; // Ptr. to next node Node n1(1), n2(2), n3(3); // Few nodes
Properties public: l.append(&n1); // Add nodes to list
Comparison
Node(int i): info(i), next(0) { } l.append(&n2);
friend void List::display(); l.append(&n3);
Module Summary friend void List::append(Node *); l.display(); // Show list
}; }

• List is built on Node. Hence List needs to know the internals of Node
• void List::append(Node *); needs the internals of Node – hence friend member function is used
• void List::display(); needs the internals of Node – hence friend member function is used
• We can do better with friend classes
Programming in Modern C++ Partha Pratim Das M17.11
friend Class

Module M17

L
Partha Pratim
Das

E
Module Recap

Objectives &

T
Outlines

friend Function

P
Matrix-Vector
Multiplication
Linked List

N
friend Class
Linked List
Iterator

Properties

Comparison

Module Summary friend Class

Programming in Modern C++ Partha Pratim Das M17.12


friend class

Module M17

• A friend class of a class

L
Partha Pratim
Das
◦ has access to the private and protected members of the class (breaks the

E
Module Recap
encapsulation) in addition to public members
Objectives &
◦ does not have its name qualified with the class scope (not a nested class)

T
Outlines

friend Function ◦ can be declared friend in more than one classes

P
Matrix-Vector
Multiplication
Linked List
• A friend class can be a

N
friend Class ◦ class
Linked List
Iterator
◦ class template
Properties

Comparison

Module Summary

Programming in Modern C++ Partha Pratim Das M17.13


Program 17.04: Linked List

Module M17
#include <iostream> void List::display() {
using namespace std; Node *ptr = head;
while (ptr) { cout << ptr->info << " ";

L
Partha Pratim
Das class Node; // Forward declaration ptr = ptr->next;
class List { }

E
Module Recap
Node *head; // Head of the list }
Objectives & Node *tail; // Tail of the list void List::append(Node *p) {

T
Outlines public: if (!head) head = tail = p;
friend Function List(Node *h = 0): head(h), tail(h) { } else {

P
Matrix-Vector void display(); tail->next = p;
Multiplication
void append(Node *p); tail = tail->next;
Linked List
}; }

N
friend Class class Node { }
Linked List int info; // Data of the node int main() { List l; // Init null list
Iterator Node *next; // Ptr to next node Node n1(1), n2(2), n3(3); // Few nodes
Properties public: l.append(&n1); // Add nodes to list
Comparison
Node(int i): info(i), next(0) { } l.append(&n2);
// friend void List::display(); l.append(&n3);
Module Summary // friend void List::append(Node *);
friend class List; l.display(); // Show list
}; }

• List class is now a friend of Node class. Hence it has full visibility into the internals of Node
• When multiple member functions need to be friends, it is better to use friend class
Programming in Modern C++ Partha Pratim Das M17.14
Program 17.05: Linked List with Iterator

Module M17
#include <iostream> // Iterator methods
using namespace std; void Iterator::begin(List *l) {
class Node; class List; // Forward declarations

L
Partha Pratim list = l; node = l->head; // Set list & Init
Das class Iterator { Node *node; // Current Node }
List *list; // Current List bool Iterator::end() { return node == 0; }

E
Module Recap
public: Iterator() : node(0), list(0) { } void Iterator::next() { node = node->next; }
Objectives & void begin(List *); // Init int Iterator::data() { return node->info; }

T
Outlines bool end(); // Check end
friend Function void next(); // Go to next void List::append(Node *p) {

P
Matrix-Vector int data(); // Get node data if (!head) head = tail = p;
Multiplication
}; else { tail->next = p; tail = tail->next; }
Linked List
class List { Node *head, *tail; public: }

N
friend Class List(Node *h=0): head(h), tail(h) { } int main() { List l;
Linked List void append(Node *p); Node n1(1), n2(2), n3(3);
Iterator friend class Iterator; l.append(&n1); l.append(&n2); l.append(&n3);
Properties };
class Node { int info; Node *next; public: Iterator i;
Comparison
Node(int i) : info(i), next(0) { } for(i.begin(&l); !i.end(); i.next()) {
Module Summary friend class List; cout << i.data() << " "; // Iteration Loop
friend class Iterator; }
}; }
• An Iterator now traverses over the elements of the List
• void List::display() is dropped from List and can be written in main()
• List class is a friend of Node class
• Iterator class is a friend of List and Node classes
Programming in Modern C++ Partha Pratim Das M17.15
Properties of friend

Module M17

L
Partha Pratim
Das

E
Module Recap

Objectives &

T
Outlines

friend Function

P
Matrix-Vector
Multiplication
Linked List

N
friend Class
Linked List
Iterator

Properties

Comparison

Module Summary Properties of friend

Programming in Modern C++ Partha Pratim Das M17.16


Properties of friend

Module M17

• friendship is neither commutative nor transitive

L
Partha Pratim
Das
◦ A is a friend of B does not imply that B is a friend of A

E
Module Recap
◦ A is a friend of B and B is a friend of C does not imply that A is a friend of C
Objectives &

T
Outlines • Visibility and Encapsulation
friend Function
◦ public: a declaration that is accessible to all

P
Matrix-Vector
Multiplication
Linked List
◦ protected: a declaration that is accessible only to the class itself and its subclasses

N
friend Class ◦ private: a declaration that is accessible only to the class itself
Linked List
Iterator
◦ friend: a declaration that is accessible only to friend’s of a class. friends tend
Properties
to break data hiding and must be used judiciously. Like:
Comparison . A function needs to access the internals of two (or more) independent classes
Module Summary (Matrix-Vector Multiplication)
. A class is built on top of another (List-Node Access, List Iterator)
. Certain situations of operator overloading (like streaming operators)

Programming in Modern C++ Partha Pratim Das M17.17


Comparison of friend vis-a-vis Member Functions

Module M17

L
Partha Pratim
Das

E
Module Recap

Objectives &

T
Outlines

friend Function

P
Matrix-Vector
Multiplication
Linked List

N
friend Class
Linked List
Iterator

Properties

Comparison

Module Summary Comparison of friend vis-a-vis Member Functions

Programming in Modern C++ Partha Pratim Das M17.18


Comparison of friend vis-a-vis Member Functions

Module M17
friend Functions static & Non-static Member Functions

L
Partha Pratim
Das
• Declared using the keyword friend • Declared in private, public, or protected specifier
• Declared in one or more classes • Declared only in scope of a particular class

E
Module Recap
• Not a part of the class, not defined in the namespace of • Part of the class definition, defined in the namespace of
Objectives & the classes the class

T
Outlines
• Has access to all private, public, and protected mem- • Has access to all private, public, and protected mem-
friend Function bers of classes bers of its class, if non-static

P
Matrix-Vector • Has access to only private, public, and protected
Multiplication
Linked List
static members of its class, if static
• May be global or member function of some other class • Member function of the class

N
friend Class • Called with an object (non-static member), an object / • Called with an object (non-static member) or an object
Linked List
a class (static member), or as a global function / a class (static member) of the defining class
Iterator
• Does not have this pointer (of the class it accesses). • Has this pointer of the defining class, if a Non-static
Properties Needs the pointer to the object and no this pointer if static
Comparison • Used in collaborative multi-class design • Used for modularity and encapsulation
• Breaks encapsulation • Ensures encapsulation
Module Summary
• Binary operation usually takes two explicit parameters • Binary operations usually take only one explicit parameter
• Unary operator takes at least one explicit parameter • Unary operator does not take any explicit parameter

Programming in Modern C++ Partha Pratim Das M17.19


Module Summary

Module M17

• Introduced the notion of friend function

L
Partha Pratim
Das
• Introduced the notion of friend class

E
Module Recap

Objectives &
• Studied the use of friend function and friend class with examples

T
Outlines

friend Function
• friend introduces visibility hole by breaking encapsulation – should be used with care

P
Matrix-Vector
Multiplication
Linked List

N
friend Class
Linked List
Iterator

Properties

Comparison

Module Summary

Programming in Modern C++ Partha Pratim Das M17.20


Module M18

L
Partha Pratim
Das
Programming in Modern C++

E
Objectives &
Outlines
Module M18: Overloading Operator for User-Defined Types: Part 1

T
Operator
Function
Non-Member

P
Member
Rules
Partha Pratim Das

N
Global Function
public data
members
private data Department of Computer Science and Engineering
members Indian Institute of Technology, Kharagpur
Member Function
operator+ ppd@cse.iitkgp.ac.in
operator=
Unary Operators

Module Summary
All url’s in this module have been accessed in September, 2021 and found to be functional

Programming in Modern C++ Partha Pratim Das M18.1


Module Recap

Module M18

• Introduced the notion of friend function

L
Partha Pratim
Das
• Introduced the notion of friend class

E
Objectives &
Outlines • Studied the use of friend function and friend class with examples

T
Operator
Function • friend introduces visibility hole by breaking encapsulation – should be used with care
Non-Member

P
Member
Rules

N
Global Function
public data
members
private data
members

Member Function
operator+
operator=
Unary Operators

Module Summary

Programming in Modern C++ Partha Pratim Das M18.2


Module Objectives

Module M18

• Understand how to overload operators for a user-defined type (class)

L
Partha Pratim
Das
• Understand the aspects of overloading by global function and member function

E
Objectives &
Outlines

T
Operator
Function
Non-Member

P
Member
Rules

N
Global Function
public data
members
private data
members

Member Function
operator+
operator=
Unary Operators

Module Summary

Programming in Modern C++ Partha Pratim Das M18.3


Module Outline

Module M18

1 Operator Function

L
Partha Pratim
Das
Non-Member Function

E
Objectives &
Outlines Member Function
Operator Overloading Rules

T
Operator
Function
Non-Member

P
Member 2 Using Global Function
Rules
public data members

N
Global Function
public data private data members
members
private data
members 3 Using Member Function
Member Function
operator+
operator+
operator= operator=
Unary Operators
Unary Operators
Module Summary

4 Module Summary

Programming in Modern C++ Partha Pratim Das M18.4


Operator Function

Module M18

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Operator
Function
Non-Member

P
Member
Rules

N
Global Function
public data
members
private data
members

Member Function
operator+
operator=
Operator Function
Unary Operators

Module Summary

Programming in Modern C++ Partha Pratim Das M18.5


How can operator functions help?

Module M18

• We have seen how overloading operator+ a C-string wrapped in struct allows us a compact

L
Partha Pratim
Das
notation for concatenation of two strings (Module 09)
• We have seen how overloading operator= can define the deep / shallow copy for a UDT and /

E
Objectives &
Outlines
or help with user-defined copy semantics (Module 14)

T
Operator
Function • In general, operator overloading helps us to build complete algebra for UDT’s much in the
Non-Member
same line as is available for built-in types:

P
Member
Rules ◦ Complex type: Add (+), Subtract (-), Multiply (*), Divide (/), Conjugate (!), Compare (==, !=,
...), etc.

N
Global Function
public data
members
◦ Fraction type: Add (+), Subtract (-), Multiply (*), Divide (/), Normalize (unary *), Compare (==,
private data
!=, ...), etc.
members ◦ Matrix type: Add (+), Subtract (-), Multiply (*), Divide (/), Invert (!), Compare (==), etc.
Member Function ◦ Set type: Union (+), Difference (-), Intersection (*), Subset (< <=), Superset (> >=), Compare
operator+
(==, !=), etc.
operator=
Unary Operators
◦ Direct IO: read (<<) and write (>>) for all types
Module Summary • Advanced examples include:
◦ Smart Pointers: De-reference (unary *), Indirection (->), Copy (=), Compare (==, !=), etc.
◦ Function Objects or Functors: Invocation (())
Programming in Modern C++ Partha Pratim Das M18.6
Operator Functions in C++: RECAP (Module 9)

Module M18 • Introduces a new keyword: operator


• Every operator is associated with an operator function that defines its behavior

L
Partha Pratim
Das

Operator Expression Operator Function

E
Objectives &
Outlines
a + b operator+(a, b)

T
Operator
Function a = b operator=(a, b)
Non-Member
operator=(c, operator+(a, b))

P
Member
c = a + b
Rules

• Operator functions are implicit for predefined operators of built-in types and cannot be

N
Global Function
public data
members redefined
private data
members
• An operator function may have a signature as:
Member Function MyType a, b; // An enum or struct
operator+
operator=
// Operator function
Unary Operators
MyType operator+(const MyType&, const MyType&);
Module Summary

a + b // Calls operator+(a, b)
• C++ allows users to define an operator function and overload it
Programming in Modern C++ Partha Pratim Das M18.7
Non-Member Operator Function

Module M18 • A non-member operator function may be a


◦ Global Function

L
Partha Pratim
Das
◦ friend Function
• Binary Operator:

E
Objectives &
Outlines
MyType a, b; // An enum, struct or class

T
Operator
Function MyType operator+(const MyType&, const MyType&); // Global
Non-Member friend MyType operator+(const MyType&, const MyType&); // Friend

P
Member
Rules
• Unary Operator:
MyType operator++(const MyType&); // Global

N
Global Function
public data friend MyType operator++(const MyType&); // Friend
members
private data • Note: The parameters may not be constant and may be passed by value. The return may also
members
be by reference and may be constant
Member Function
operator+ • Examples:
operator= Operator Expression Operator Function
Unary Operators
a + b operator+(a, b)
Module Summary a = b operator=(a, b)
++a operator++(a)
a++ operator++(a, int) Special Case
c = a + b operator=(c, operator+(a, b))
Programming in Modern C++ Partha Pratim Das M18.8
Member Operator Function

Module M18 • Binary Operator:


MyType a, b; // MyType is a class

L
Partha Pratim
Das MyType operator+(const MyType&); // Operator function
• The left operand is the invoking object – right is taken as a parameter

E
Objectives &
Outlines
• Unary Operator:

T
Operator
Function
MyType operator-(); // Operator function for Unary minus
Non-Member MyType operator++(); // For Pre-Incrementer

P
Member MyType operator++(int); // For post-Incrementer
Rules
• The only operand is the invoking object

N
Global Function
public data • Note: The parameters may not be constant and may be passed by value. The return may also
members
private data be by reference and may be constant
members
• Examples:
Member Function
operator+
Operator Expression Operator Function
operator= a + b a.operator+(b)
Unary Operators
a = b a.operator=(b)
Module Summary
++a a.operator++()
a++ a.operator++(int) // Special Case
c = a + b c.operator =(a.operator+(b))
Programming in Modern C++ Partha Pratim Das M18.9
Operator Overloading – Summary of Rules: RECAP (Module 9)

Module M18 • No new operator such as **, <>, or &| can be defined for overloading
• Intrinsic properties of the overloaded operator cannot be change

L
Partha Pratim
Das
◦ Preserves arity

E
Objectives &
Outlines
◦ Preserves precedence
◦ Preserves associativity

T
Operator
Function
Non-Member
• These operators can be overloaded:

P
Member [] + - * / % ^ & | ~ ! = += -= *= /= %= ^= &= |=
Rules
<< >> >>= <<= == != < > <= >= && || ++ -- , ->* -> ( ) [ ]

N
Global Function
public data • The operators :: (scope resolution), . (member access), .* (member access through pointer
members
private data to member), sizeof, and ?: (ternary conditional) cannot be overloaded
members
• The overloads of operators &&, ||, and , (comma) lose their special properties: short-circuit
Member Function
operator+
evaluation and sequencing
operator= • The overload of operator-> must either return a raw pointer or return an object (by reference
Unary Operators
or by value), for which operator-> is in turn overloaded
Module Summary
• For a member operator function, invoking object is passed implicitly as the left operand but
the right operand is passed explicitly
• For a non-member operator function (Global/friend) operands are always passed explicitly
Programming in Modern C++ Partha Pratim Das M18.10
Using Global Function

Module M18

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Operator
Function
Non-Member

P
Member
Rules

N
Global Function
public data
members
private data
members

Member Function
operator+
operator=
Using Global Function
Unary Operators

Module Summary

Programming in Modern C++ Partha Pratim Das M18.11


Program 18.01: Using Global Function: public Data members
(Unsafe)
Overloading + for complex addition Overloading + for string cat
Module M18 #include <iostream>
#include <iostream> #include <cstdlib>

L
Partha Pratim using namespace std;
Das #include <cstring>
struct complx { // public data member using namespace std;
double re, im;

E
Objectives & typedef struct _String { char *str; } String;
Outlines } ; String operator+(const String& s1, const String& s2) {
complx operator+ (complx &a, complx &b) {

T
Operator String s;
Function complx r; s.str = (char *) malloc(strlen(s1.str) +
Non-Member r.re = a.re + b.re; strlen(s2.str) + 1);

P
Member r.im = a.im + b.im; strcpy(s.str, s1.str); strcat(s.str, s2.str);
Rules return r; return s;
}

N
Global Function }
public data int main() { complx d1 , d2 , d; int main() { String fName, lName, name;
members d1.re = 10.5; d1.im = 12.25; fName.str = strdup("Partha ");
private data d2.re = 20.5; d2.im = 30.25;
members lName.str = strdup("Das");
d = d1 + d2; // Overload operator + name = fName + lName; // Overload operator +
Member Function cout << "Real:" << d.re << ", ";
operator+
cout << "First Name: " << fName.str << endl;
cout << "Imag:" << d.im; cout << "Last Name: " << lName.str << endl;
operator=
} cout << "Full Name: " << name.str << endl;
Unary Operators
}
Module Summary
• Output: Real: 31, Imag: 42.5 • Output: First Name: Partha, Last Name: Das, Full name:
Partha Das
• operator+ is overloaded to perform addition of two • operator+ is overloaded to perform concat of first name
complex numbers which are of struct complx type and last to form full name. The data type is String
Programming in Modern C++ Partha Pratim Das M18.12
Program 18.02: Using Global Function: private Data members
(Safe)
Module M18 #include <iostream> Complex operator+(Complex &t1, Complex &t2) {
using namespace std; Complex sum;

L
Partha Pratim
Das
class Complex { // Private data members sum.set_real(t1.real() + t2.real());
double re, im; sum.set_img(t1.img() + t2.img());
public:

E
Objectives & return sum;
Outlines Complex(double a=0.0, double b=0.0): }
re(a), im(b) { } ~Complex() { } int main() {

T
Operator
Function void display(); Complex c1(4.5, 25.25), c2(8.3, 10.25), c3;
Non-Member double real() { return re; } cout << "1st complex No:"; c1.display();

P
Member double img() { return im; } cout << "2nd complex No:"; c2.display();
Rules double set_real(double r) { re = r; } c3 = c1 + c2; // Overload operator +
double set_img(double i) { im = i; }

N
Global Function cout << "Sum = "; c3.display();
public data } ; }
members void Complex::display() {
private data cout << re << " +j " << im << endl;
members
}
Member Function
operator+ • Output:
operator=
Unary Operators 1st complex No: 4.5 +j 25.25
2nd complex No: 8.3 +j 10.25
Module Summary
Sum = 12.8 +j 35.5
• Accessing private data members inside operator functions is clumsy
• Critical data members need to be exposed (get/set) violating encapsulation
• Solution: Member operator function or friend operator function
Programming in Modern C++ Partha Pratim Das M18.13
Using Member Function

Module M18

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Operator
Function
Non-Member

P
Member
Rules

N
Global Function
public data
members
private data
members

Member Function
operator+
operator=
Using Member Function
Unary Operators

Module Summary

Programming in Modern C++ Partha Pratim Das M18.14


Program 18.03: Using Member Function

Module M18
#include <iostream> void Complex::display() {
using namespace std; cout << re;
class Complex { // Private data members

L
Partha Pratim cout << " +j " << im << endl;
Das double re, im; }
public: int main() {

E
Objectives &
Outlines
Complex(double a=0.0, double b=0.0): Complex c1(4.5, 25.25), c2(8.3, 10.25), c3;
re(a), im(b) { } ~Complex() { } cout << "1st complex No:";

T
Operator void display();
Function
c1.display();
Non-Member
Complex operator+(const Complex &c) { cout << "2nd complex No:";

P
Member
Complex r; c2.display();
Rules r.re = re + c.re; c3 = c1 + c2; // Overloaded operator +
r.im = im + c.im; cout << "Sum = ";

N
Global Function
return r; c3.display();
public data
members } return 0;
private data } ; }
members

Member Function • Output:


operator+
1st complex No: 4.5 +j 25.25
operator=
Unary Operators
2nd complex No: 8.3 +j 10.25
Sum = 12.8 +j 35.5
Module Summary
• Performing c1 + c2 is equivalent to c1.operator+(c2)
• c1 invokes the operator+ function and c2 is passed as an argument
• Similarly we can implement all binary operators (%, -, *, etc..)
• Note: No need of two arguments in overloading
Programming in Modern C++ Partha Pratim Das M18.15
Program 14.14: Overloading operator=: RECAP (Module 14)

Module M18 #include <iostream>


#include <cstdlib>

L
Partha Pratim
Das #include <cstring>
using namespace std;

E
Objectives & class String { public: char *str_; size_t len_;
Outlines
String(char *s) : str_(strdup(s)), len_(strlen(str_)) { } // ctor

T
Operator String(const String& s) : str_(strdup(s.str_)), len_(s.len_) { } // cctor
Function ~String() { free(str_); } // dtor
Non-Member String& operator=(const String& s) {

P
Member
if (this != &s) { free(str_); str_ = strdup(s.str_); len_ = s.len_; }
Rules
return *this;

N
Global Function }
public data void print() { cout << "(" << str_ << ": " << len_ << ")" << endl; }
members
private data
};
members int main() { String s1 = "Football", s2 = "Cricket";
Member Function
s1.print(); s2.print();
operator+ s1 = s1; s1.print();
operator= }
Unary Operators (Football: 8)
(Cricket: 7)
Module Summary
(Football: 8)

• Check for self-copy (this != &s)


• In case of self-copy, do nothing
Programming in Modern C++ Partha Pratim Das M18.16
Notes on Overloading operator=: RECAP (Module 14)

Module M18

• Overloaded operator= may choose between Deep and Shallow Copy for Pointer

L
Partha Pratim
Das
Members

E
Objectives &
Outlines ◦ Deep copy allocates new space for the contents and copies the pointed data
◦ Shallow copy merely copies the pointer value – hence, the new copy and the original

T
Operator
Function
Non-Member
pointer continue to point to the same data

P
Member
Rules • If operator= is not overloaded by the user, compiler provides a free one.

N
Global Function • Free operator= can makes only a shallow copy
public data
members
private data
• If the constructor uses operator new, operator= should be overloaded
members

Member Function
• If there is a need to define a copy constructor then operator= must be overloaded and
operator+ vice-versa
operator=
Unary Operators

Module Summary

Programming in Modern C++ Partha Pratim Das M18.17


Program 18.04: Overloading Unary Operators

Module M18 #include <iostream> • Output


using namespace std; Data = 8

L
Partha Pratim
Das Data = 8
class MyClass { int data; public: Data = 9

E
Objectives & MyClass(int d): data(d) { } Data = 10
Outlines Data = 10

T
Operator MyClass& operator++() { // Pre-increment:
Function ++data; // Operate and return the operated object
Non-Member return *this;

P
Member
}
Rules
MyClass operator++(int) { // Post-Increment:

N
Global Function MyClass t(data); // Return the (copy of) object; operate the object
public data ++data; • The pre-operator should first perform the oper-
members
return t; ation (increment / decrement / other) and then
private data
members } return the object. Hence its return type should be
Member Function
void disp() { cout << "Data = " << data << endl; } MyClass& and it should return *this;
operator+
};
operator= int main() { • The post-operator should perform the operation
Unary Operators MyClass obj1(8); obj1.disp(); (increment / decrement / other) after it returns
MyClass obj2 = obj1++; obj2.disp(); obj1.disp(); the original value. Hence it should copy the original
Module Summary
object in a temporary MyClass t; and then return
obj2 = ++obj1; t;. Its return type should be MyClass - by value
obj2.disp(); obj1.disp();
}
Programming in Modern C++ Partha Pratim Das M18.18
Program 18.05: Overloading Unary Operators:
Pre-increment & Post Increment
#include <iostream>
Module M18
using namespace std; • Output
Data = 12

L
Partha Pratim
Das class MyClass { int data; Data = 12
public: Data = 4

E
Objectives &
Outlines
MyClass(int d) : data(d) { } Data = 8
Data = 8

T
Operator MyClass& operator++() { // Pre-Operator
Function
data *= 2;
Non-Member

P
Member
return *this;
Rules
}
MyClass operator++(int) { // Post-Operator

N
Global Function
MyClass t(data);
public data
members data /= 3; • The pre-operator and the post-operator need not
private data return t; merely increment / decrement
members
}
Member Function void disp() { cout << "Data = " << data << endl; } • They may be used for any other computation as
operator+ }; this example shows
operator= int main() {
Unary Operators MyClass obj1(12); obj1.disp(); • However, it is a good design practice to keep
Module Summary MyClass obj2 = obj1++; obj2.disp(); obj1.disp(); close to the native semantics of the operator
obj2 = ++obj1;
obj2.disp(); obj1.disp();
}
Programming in Modern C++ Partha Pratim Das M18.19
Module Summary

Module M18

• Introduced operator overloading for user-defined types

L
Partha Pratim
Das
• Illustrated methods of overloading operators using global functions and member

E
Objectives &
Outlines functions

T
Operator
Function
• Outlined semantics for overloading binary and unary operators
Non-Member

P
Member
Rules

N
Global Function
public data
members
private data
members

Member Function
operator+
operator=
Unary Operators

Module Summary

Programming in Modern C++ Partha Pratim Das M18.20


Module M19

L
Partha Pratim
Das
Programming in Modern C++

E
Objectives &
Outlines
Module M19: Overloading Operator for User-Defined Types: Part 2

T
Issues in Operator
Overloading

P
operator+

operator==
Partha Pratim Das

N
operator<<,
operator>>

Guidelines Department of Computer Science and Engineering


Module Summary Indian Institute of Technology, Kharagpur

ppd@cse.iitkgp.ac.in

All url’s in this module have been accessed in September, 2021 and found to be functional

Programming in Modern C++ Partha Pratim Das M19.1


Module Recap

Module M19

• Introduced operator overloading for user-defined types

L
Partha Pratim
Das
• Illustrated methods of overloading operators using global functions and member

E
Objectives &
Outlines functions

T
Issues in Operator
Overloading
• Outlined semantics for overloading binary and unary operators

P
operator+

operator==

N
operator<<,
operator>>

Guidelines

Module Summary

Programming in Modern C++ Partha Pratim Das M19.2


Module Objectives

Module M19

• Understand how to overload operators for a user-defined type (class)

L
Partha Pratim
Das
• Understand the aspects of overloading by friend function and its advantages

E
Objectives &
Outlines

T
Issues in Operator
Overloading

P
operator+

operator==

N
operator<<,
operator>>

Guidelines

Module Summary

Programming in Modern C++ Partha Pratim Das M19.3


Module Outline

Module M19

L
Partha Pratim
Das 1 Issues in Operator Overloading

E
Objectives &
Outlines
2 operator+

T
Issues in Operator
Overloading

P
operator+

operator==
3 operator==

N
operator<<,
operator>>

Guidelines 4 operator<<, operator>>


Module Summary

5 Guidelines for Operator Overloading

6 Module Summary

Programming in Modern C++ Partha Pratim Das M19.4


Operator Function for UDT: RECAP (Module 18)

Module M19 • Operator Function options:


◦ Global Function

L
Partha Pratim
Das
◦ Member Function

E
Objectives &
Outlines
◦ friend Function
• Binary Operator:

T
Issues in Operator
Overloading MyType a, b; // An enum, struct or class

P
operator+ MyType operator+(const MyType&, const MyType&); // Global
operator== MyType operator+(const MyType&); // Member
friend MyType operator+(const MyType&, const MyType&); // Friend

N
operator<<,
operator>>
• Unary Operator:
Guidelines
MyType operator++(const MyType&); // Global
Module Summary
MyType operator++(); // Member
friend MyType operator++(const MyType&); // Friend
• Examples:
Expression Function Remarks
a + b operator+(a, b) global / friend
++a operator++(a) global / friend
a + b a.operator+(b) member
++a a.operator++() member
Programming in Modern C++ Partha Pratim Das M19.5
Issues in Operator Overloading

Module M19

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Issues in Operator
Overloading

P
operator+

operator==

N
operator<<,
operator>>

Guidelines

Module Summary

Issues in Operator Overloading

Programming in Modern C++ Partha Pratim Das M19.6


Issue 1: Extending operator+

Module M19 • Consider a Complex class. We have learnt how to overload operator+ to add two Complex
numbers:

L
Partha Pratim
Das

E
Objectives & Complex d1(2.5, 3.2), d2(1.6, 3.3), d3;
Outlines

T
Issues in Operator d3 = d1 + d2; // d3 = 4.1 +j 6.5
Overloading

P
operator+
• Now we want to extend the operator so that a Complex number and a real number (no
operator==
imaginary part) can be added together:

N
operator<<,
operator>>

Guidelines
Complex d1(2.5, 3.2), d2(1.6, 3.3), d3;
Module Summary
d3 = d1 + 6.2; // d3 = 8.7 +j 3.2

d3 = 4.2 + d2; // d3 = 5.8 +j 3.3

• We show why global operator function is not good for this


• We show why member operator function cannot do this
• We show how friend function achieves this
Programming in Modern C++ Partha Pratim Das M19.7
Issue 2: Overloading IO Operators: operator<<, operator>>

Module M19 • Consider a Complex class. Suppose we want to overload the streaming operators for this class
so that we can write the following code:

L
Partha Pratim
Das

E
Objectives & Complex d;
Outlines

T
Issues in Operator cin >> d;
Overloading

P
operator+
cout << d;
operator==

N
operator<<,
operator>> • Let us note that these operators deal with stream types defined in iostream, ostream, and
Guidelines istream:
Module Summary ◦ cout is an ostream object
◦ cin is an istream object
• We show why global operator function is not good for this
• We show why member operator function cannot do this
• We show how friend function achieves this

Programming in Modern C++ Partha Pratim Das M19.8


Extending operator+

Module M19

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Issues in Operator
Overloading

P
operator+

operator==

N
operator<<,
operator>>

Guidelines

Module Summary

Extending operator+

Programming in Modern C++ Partha Pratim Das M19.9


Program 19.01: Extending operator+ with Global Function
#include <iostream>
Module M19
using namespace std;
class Complex { public: double re, im;

L
Partha Pratim
Das explicit Complex(double r = 0, double i = 0): re(r), im(i) { } // No implicit conversion is allowed
void disp() { cout << re << " +j " << im << endl; }

E
Objectives &
Outlines
};
Complex operator+(const Complex &a, const Complex &b) { // Overload 1

T
Issues in Operator return Complex(a.re + b.re, a.im + b.im);
Overloading
}

P
operator+ Complex operator+(const Complex &a, double d) { // Overload 2
operator== Complex b(d); return a + b; // Create temporary object and use Overload 1
}

N
operator<<,
operator>>
Complex operator+(double d, const Complex &b) { // Overload 3
Complex a(d); return a + b; // Create temporary object and use Overload 1
Guidelines }
Module Summary int main() { Complex d1(2.5, 3.2), d2(1.6, 3.3), d3;
d3 = d1 + d2; d3.disp(); // d3 = 4.1 +j 6.5. Overload 1
d3 = d1 + 6.2; d3.disp(); // d3 = 8.7 +j 3.2. Overload 2
d3 = 4.2 + d2; d3.disp(); // d3 = 5.8 +j 3.3. Overload 3
}
• Works fine with global functions - 3 separate overloading are provided
• A bad solution as it breaks the encapsulation – as discussed in Module 18
• Let us try to use member function
• Note: A simpler solution uses Overload 1 and implicit casting (for this we need to remove explicit before constructor).
But that too breaks encapsulation. We discuss this when we take up cast operators
Programming in Modern C++ Partha Pratim Das M19.10
Program 19.02: Extending operator+ with Member Function
#include <iostream>
Module M19
using namespace std;
class Complex { double re, im;

L
Partha Pratim
Das public:
explicit Complex(double r = 0, double i = 0) : re(r), im(i) { } // No implicit conversion is allowed

E
Objectives &
Outlines
void disp() { cout << re << " +j " << im << endl; }
Complex operator+(const Complex &a) { // Overload 1

T
Issues in Operator return Complex(re + a.re, im + a.im);
Overloading
}

P
operator+ Complex operator+(double d) { // Overload 2
operator== Complex b(d); // Create temporary object
return *this + b; // Use Overload 1

N
operator<<,
operator>>
}
};
Guidelines int main() { Complex d1(2.5, 3.2), d2(1.6, 3.3), d3;
Module Summary d3 = d1 + d2; d3.disp(); // d3 = 4.1 +j 6.5. Overload 1
d3 = d1 + 6.2; d3.disp(); // d3 = 8.7 +j 3.2. Overload 2

//d3 = 4.2 + d2; // Overload 3 is not possible - needs an object on left


//d3.disp();
}
• Overload 1 and 2 works
• Overload 3 cannot be done because the left operand is double – not an object
• Let us try to use friend function
• Note: This solution too avoids the feature of cast operators
Programming in Modern C++ Partha Pratim Das M19.11
Operator Overloading using friend

Module M19 • Using global function, accessing private data members inside operator function is gets difficult
• It increases writing overhead, makes code complicated, else violates encapsulation

L
Partha Pratim
Das
• As we saw till now most operators can actually be overloaded either by global function or

E
Objectives & member function, But If the left operand is not an object of the class type then it cannot be
Outlines
overloaded through member function

T
Issues in Operator
Overloading
• To handle such situation, we require friend function
◦ Example: For two objects d1 & d2 of the same class, we cannot overload (constant + d2) using

P
operator+

operator==
member function. However, using friend function we can overload (d1 + d2), (d1 + constant),
or (constant + d2)

N
operator<<,
operator>>

Guidelines
◦ Reason: While computing (d1 + d2) with member function, d1 calls the operator+() and d2 is
Module Summary
passed as an argument. Similarly in (d1 + constant), d1 calls the operator+() and constant is
passed as an argument. But while calling (constant + d2) a constant cannot call the member
function

Similar analysis will also hold when d1 & d2 are objects of different classes and we cannot add the
operator to the class of d1
• So operators like <<, >>, relational (<, >, ==, !=, <=, >=) should be overloaded through friend

Programming in Modern C++ Partha Pratim Das M19.12


Program 19.03: Extending operator+ with friend Function
#include <iostream>
Module M19
using namespace std;
class Complex { double re, im; public:

L
Partha Pratim
Das explicit Complex(double r = 0, double i = 0) : re(r), im(i) { } // No implicit conversion is allowed
void disp() { cout << re << " +j " << im << endl; }

E
Objectives &
Outlines
friend Complex operator+(const Complex &a, const Complex &b) { // Overload 1
return Complex(a.re + b.re, a.im + b.im);

T
Issues in Operator }
Overloading
friend Complex operator+(const Complex &a, double d) { // Overload 2

P
operator+ Complex b(d); // Create temporary object
operator== return a + b; // Use Overload 1
}

N
operator<<,
operator>>
friend Complex operator+(double d, const Complex &b) { // Overload 3
Complex a(d); // Create temporary object
Guidelines return a + b; // Use Overload 1
Module Summary }
};
int main() { Complex d1(2.5, 3.2), d2(1.6, 3.3), d3;
d3 = d1 + d2; d3.disp(); // d3 = 4.1 +j 6.5. Overload 1
d3 = d1 + 6.2; d3.disp(); // d3 = 8.7 +j 3.2. Overload 2
d3 = 4.2 + d2; d3.disp(); // d3 = 5.8 +j 3.3. Overload 3
}
• Works fine with friend functions - 3 separate overloading are provided and Preserves the encapsulation too
• Note: A simpler solution uses only Overload 1 and implicit casting (for this we need to remove explicit before
constructor) will be discussed when we take up cast operators
Programming in Modern C++ Partha Pratim Das M19.13
Overloading Comparison Operators

Module M19

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Issues in Operator
Overloading

P
operator+

operator==

N
operator<<,
operator>>

Guidelines

Module Summary

Overloading Comparison Operators

Programming in Modern C++ Partha Pratim Das M19.14


Program 19.04: Overloading operator== for strings with friend
Function
#include <iostream>
Module M19
#include <string>
#include <cstdlib>

L
Partha Pratim
Das #include <cstring>
using namespace std;

E
Objectives & class MyStr { const char *name_; public:
Outlines
explicit MyStr(const char *s) : name_(strdup(s)) { } ~MyStr() { free((void *)name_); }

T
Issues in Operator friend bool operator==(const MyStr& s1, const MyStr& s2) { return !strcmp(s1.name_, s2.name_); } // 1
Overloading
friend bool operator==(const MyStr& s1, const string& s2) { return !strcmp(s1.name_, s2.c_str()); } // 2
friend bool operator==(const string& s1, const MyStr& s2) { return !strcmp(s1.c_str(), s2.name_); } // 3

P
operator+

operator==
};
int main() {

N
operator<<, MyStr mS1("red"), mS2("red"), mS3("blue"); string sS1("red"), sS2("red"), sS3("blue");
operator>>
if (mS1 == mS2) cout << "Match "; else cout << "Mismatch "; // MyStr, MyStr: Overload 1
Guidelines if (mS1 == mS3) cout << "Match "; else cout << "Mismatch "; // MyStr, MyStr: Overload 1
Module Summary if (mS1 == sS2) cout << "Match "; else cout << "Mismatch "; // MyStr, string: Overload 2
if (mS1 == sS3) cout << "Match "; else cout << "Mismatch "; // MyStr, string: Overload 2
if (sS1 == mS2) cout << "Match "; else cout << "Mismatch "; // string, MyStr: Overload 3
if (sS1 == mS3) cout << "Match "; else cout << "Mismatch "; // string, MyStr: Overload 3
if (sS1 == sS2) cout << "Match "; else cout << "Mismatch "; // string, string: C++ Lib
if (sS1 == sS3) cout << "Match "; else cout << "Mismatch "; // string, string: C++ Lib
}
Output: Match Mismatch Match Mismatch Match Mismatch Match Mismatch
• MyStr is a user-defined string class while string is from C++ Standard Library. These are compared here by operator==.
We can extend this to include comparison with char* and for other comparison operators
Programming in Modern C++ Partha Pratim Das M19.15
Overloading IO Operators

Module M19

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Issues in Operator
Overloading

P
operator+

operator==

N
operator<<,
operator>>

Guidelines

Module Summary

Overloading IO Operators

Programming in Modern C++ Partha Pratim Das M19.16


Overloading IO Operators: operator<<, operator>>

Module M19 • Consider operator<< for Complex class. This operator should take an ostream object (stream
to write to) and a Complex (object to write). Further it allows to chain the output. So for the

L
Partha Pratim
Das
following code

E
Objectives &
Outlines
Complex d1, d2;

T
Issues in Operator
Overloading
cout << d1 << d2; // (cout << d1) << d2;

P
operator+

operator==
the signature of operator<< may be one of:

N
operator<<,
operator>> // Global function
Guidelines ostream& operator<< (ostream& os, const Complex &a);
Module Summary
// Member function in ostream
ostream& ostream::operator<< (const Complex &a);

// Member function in Complex


ostream& Complex::operator<< (ostream& os);
• Object to write is passed by constant reference
• Return by reference for ostream object is used so that chaining would work
Programming in Modern C++ Partha Pratim Das M19.17
Program 19.05: Overloading IO Operators with Global Function
#include <iostream>
Module M19
using namespace std;
class Complex {

L
Partha Pratim
Das public: double re, im;
Complex(double r = 0, double i = 0): re(r), im(i) { }

E
Objectives &
Outlines
};
ostream& operator<<(ostream& os, const Complex &a) {

T
Issues in Operator os << a.re << " +j " << a.im << endl;
Overloading
return os;

P
operator+ }
operator== istream& operator>>(istream& is, Complex &a) {
is >> a.re >> a.im;

N
operator<<,
operator>>
return is;
}
Guidelines int main() {
Module Summary Complex d;

cin >> d;

cout << d;
}

• Works fine with global functions


• A bad solution as it breaks the encapsulation – as discussed in Module 18
• Let us try to use member function
Programming in Modern C++ Partha Pratim Das M19.18
Overloading IO Operators with Member Function

Module M19
• Case 1: operator<< is a member in ostream class:

L
Partha Pratim
Das
ostream& ostream::operator<< (const Complex &a);

E
Objectives &
Outlines
This is not possible as ostream is a class in C++ standard library and we are not allowed to

T
Issues in Operator
Overloading edit it to include the above signature
• Case 2: operator<< is a member in Complex class:

P
operator+

operator==

N
operator<<, ostream& Complex::operator<< (ostream& os);
operator>>

Guidelines
In this case, the invocation of streaming will change to:
Module Summary

d << cout; // Left operand is the invoking object

This certainly spoils the natural syntax


• IO operators cannot be overloaded by member functions
• Let us try to use friend function
Programming in Modern C++ Partha Pratim Das M19.19
Program 19.06: Overloading IO Operators with friend Function
#include <iostream>
Module M19
using namespace std;
class Complex { double re, im;

L
Partha Pratim
Das public:
Complex(double r = 0, double i = 0): re(r), im(i) { }

E
Objectives &
Outlines
friend ostream& operator<<(ostream& os, const Complex &a);
friend istream& operator>>(istream& is, Complex &a);

T
Issues in Operator
Overloading
};

P
operator+ friend ostream& operator<<(ostream& os, const Complex &a) {
operator== os << a.re << " +j " << a.im << endl;
return os;

N
operator<<,
operator>>
}
friend istream& operator>>(istream& is, Complex &a) {
Guidelines is >> a.re >> a.im;
Module Summary return is;
}
int main() { Complex d;

cin >> d;

cout << d;
}

• Works fine with friend functions


Programming in Modern C++ Partha Pratim Das M19.20
Guidelines for Operator Overloading

Module M19

L
Partha Pratim
Das

E
Objectives &
Outlines

T
Issues in Operator
Overloading

P
operator+

operator==

N
operator<<,
operator>>

Guidelines

Module Summary

Guidelines for Operator Overloading

Programming in Modern C++ Partha Pratim Das M19.21


Guidelines for Operator Overloading

Module M19
• Use global function when encapsulation is not a concern. For example, using struct String

L
Partha Pratim
Das { char* str; } to wrap a C-string and overload operator+ to concatenate strings and build
a String algebra

E
Objectives &
Outlines • Use member function when the left operand is necessarily an object of a class where the
operator function is a member. Specifically operator=, operator new, operator new[], operator

T
Issues in Operator
Overloading
delete etc. must be member functions

P
operator+
• Use friend function, otherwise for operators like <<, >>, relational (<, >, ==, !=, <=, >=)
operator==
should be overloaded through friend

N
operator<<,
operator>> • While overloading an operator, try to preserve its natural semantics for built-in types as much
Guidelines as possible. For example, operator+ in a Set class should compute union and NOT
Module Summary intersection
• Usually stick to the parameter passing conventions (built-in types by value and UDT’s by
constant reference)
• Decide on the return type based on the natural semantics for built-in types as illustrated in the
examples
• Consider the effect of casting on operands
• Only overload the operators that you may need (minimal design)
Programming in Modern C++ Partha Pratim Das M19.22
Module Summary

Module M19

• Several issues in operator overloading has been discussed

L
Partha Pratim
Das
• Use of friend is illustrated in versatile forms of overloading with examples

E
Objectives &
Outlines • Discussed the overloading IO (streaming) operators

T
Issues in Operator
Overloading • Guidelines for operator overloading is summarized

P
operator+
• Use operator overloading to build algebra for:
operator==
◦ Complex numbers

N
operator<<,
operator>>
◦ Fractions
Guidelines
◦ Strings
Module Summary
◦ Vector and Matrices
◦ Sets
◦ and so on ...

Programming in Modern C++ Partha Pratim Das M19.23


Module M20

L
Partha Pratim
Das
Programming in Modern C++

E
Objectives &
Outlines
Module M20: Namespace

T
namespace
Fundamental

P
namespace
Scenarios

namespace Partha Pratim Das

N
Features
Nested namespace
using namespace Department of Computer Science and Engineering
Global namespace
Indian Institute of Technology, Kharagpur
std namespace
namespaces are
Open ppd@cse.iitkgp.ac.in
namespace
vis-a-vis class
All url’s in this module have been accessed in September, 2021 and found to be functional
Lexical Scope

Module Summary

Programming in Modern C++ Partha Pratim Das M20.1


Module Recap

Module M20

• Several issues in operator overloading has been discussed

L
Partha Pratim
Das
• Use of friend is illustrated in versatile forms of overloading with examples

E
Objectives &
Outlines • Discussed the overloading IO (streaming) operators

T
namespace
Fundamental • Guidelines for operator overloading is summarized

P
namespace
Scenarios • Use operator overloading to build algebra for:
namespace ◦ Complex numbers

N
Features
Nested namespace ◦ Fractions
namespace
using
Global namespace
◦ Strings
std namespace ◦ Vector and Matrices
namespaces are
Open ◦ Sets
namespace
vis-a-vis class
◦ and so on ...
Lexical Scope

Module Summary

Programming in Modern C++ Partha Pratim Das M20.2


Module Objectives

Module M20

• Understand namespace as a free scoping mechanism to organize code better

L
Partha Pratim
Das

E
Objectives &
Outlines

T
namespace
Fundamental

P
namespace
Scenarios

namespace

N
Features
Nested namespace
using namespace
Global namespace
std namespace
namespaces are
Open

namespace
vis-a-vis class

Lexical Scope

Module Summary

Programming in Modern C++ Partha Pratim Das M20.3


Module Outline

Module M20

1 namespace Fundamental

L
Partha Pratim
Das

namespace Scenarios

E
Objectives & 2
Outlines

T
namespace
Fundamental
3 namespace Features
Nested namespace

P
namespace
Scenarios
using namespace
namespace

N
Features Global namespace
Nested namespace
using namespace
std namespace
Global namespace namespaces are Open
std namespace
namespaces are
Open 4 namespace vis-a-vis class
namespace
vis-a-vis class

Lexical Scope
5 Lexical Scope
Module Summary
6 Module Summary
Programming in Modern C++ Partha Pratim Das M20.4
namespace Fundamental

Module M20

L
Partha Pratim
Das

E
Objectives &
Outlines

T
namespace
Fundamental

P
namespace
Scenarios

namespace

N
Features
Nested namespace
using namespace
Global namespace
std namespace
namespaces are
Open

namespace
namespace Fundamental
vis-a-vis class

Lexical Scope

Module Summary

Programming in Modern C++ Partha Pratim Das M20.5


namespace Fundamental

Module M20

• A namespace is a declarative region that provides a scope to the identifiers (the names

L
Partha Pratim
Das
of types, functions, variables, etc) inside it

E
Objectives &
Outlines • It is used to organize code into logical groups and to prevent name collisions that can
occur especially when your code base includes multiple libraries

T
namespace
Fundamental
• namespace provides a class-like modularization without class-like semantics

P
namespace
Scenarios

namespace
• Obliviates the use of File Level Scoping of C (file) static

N
Features
Nested namespace
using namespace
Global namespace
std namespace
namespaces are
Open

namespace
vis-a-vis class

Lexical Scope

Module Summary

Programming in Modern C++ Partha Pratim Das M20.6


Program 20.01: namespace Fundamental

Module M20 • Example:


#include <iostream>

L
Partha Pratim
Das using namespace std;

E
Objectives & namespace MyNameSpace {
Outlines int myData; // Variable in namespace
void myFunction() { cout << "MyNameSpace myFunction" << endl; } // Function in namespace

T
namespace
Fundamental class MyClass { int data; // Class in namespace
public:

P
namespace
Scenarios MyClass(int d) : data(d) { }
namespace
void display() { cout << "MyClass data = " << data << endl; }

N
Features };
Nested namespace }
using namespace int main() {
Global namespace MyNameSpace::myData = 10; // Variable name qualified by namespace name
std namespace cout << "MyNameSpace::myData = " << MyNameSpace::myData << endl;
namespaces are
Open
MyNameSpace::myFunction(); // Function name qualified by namespace name
namespace
vis-a-vis class
MyNameSpace::MyClass obj(25); // Class name qualified by namespace name
Lexical Scope obj.display();
Module Summary }
• A name in a namespace is prefixed by the name of it
• Beyond scope resolution, all namespace items are treated as global
Programming in Modern C++ Partha Pratim Das M20.7
namespace Scenarios

Module M20

L
Partha Pratim
Das

E
Objectives &
Outlines

T
namespace
Fundamental

P
namespace
Scenarios

namespace

N
Features
Nested namespace
using namespace
Global namespace
std namespace
namespaces are
Open

namespace
namespace Scenarios
vis-a-vis class

Lexical Scope

Module Summary

Programming in Modern C++ Partha Pratim Das M20.8


Scenario 1: Redefining a Library Function
Program 20.02
Module M20
• cstdlib has a function int abs(int n); that returns the absolute value of parameter n
• You need a special int abs(int n); function that returns the absolute value of parameter n if n is between -128 and
127. Otherwise, it returns 0

L
Partha Pratim
Das
• Once you add your abs, you cannot use the abs from library! It is hidden and gone!
• namespace comes to your rescue

E
Objectives &
Outlines
Name-hiding: abs() namespace: abs()

T
namespace #include <iostream> #include <iostream>
Fundamental #include <cstdlib> #include <cstdlib>
namespace myNS {

P
namespace
Scenarios int abs(int n) { int abs(int n) {
namespace if (n < -128) return 0; if (n < -128) return 0;

N
Features if (n > 127) return 0; if (n > 127) return 0;
Nested namespace if (n < 0) return -n; if (n < 0) return -n;
using namespace return n; return n;
Global namespace
} }
std namespace }
namespaces are
Open int main() { std::cout << abs(-203) << " " int main() { std::cout << myNS::abs(-203) << " "
<< abs(-6) << " " << myNS::abs(-6) << " "
namespace
vis-a-vis class << abs(77) << " " << myNS::abs(77) << " "
<< abs(179) << std::endl; << myNS::abs(179) << std::endl;
Lexical Scope // Output: 0 6 77 0
// Output: 0 6 77 0
Module Summary } std::cout << abs(-203) << " " << abs(-6) << " "
<< abs(77) << " " << abs(179) << std::endl;
// Output: 203 6 77 179
Programming in Modern C++ } Partha Pratim Das M20.9
Scenario 2: Students’ Record Application: The Setting
Program 20.03
• An organization is developing an application to process students records
Module M20
• class St for Students and class StReg for list of Students are:

L
Partha Pratim #include <iostream>
Das #include <cstring>
using namespace std;

E
Objectives &
Outlines
class St { public: // A Student
typedef enum GENDER { male = 0, female };

T
namespace
Fundamental
St(char *n, GENDER g) : name(strcpy(new char[strlen(n) + 1], n)), gender(g) { }
void setRoll(int r) { roll = r; } // Set roll while adding the student

P
namespace GENDER getGender() { return gender; } // Get the gender for processing
Scenarios
friend ostream& operator<< (ostream& os, const St& s) { // Print a record
namespace cout << ((s.gender == St::male) ? "Male " : "Female ")

N
Features
<< s.name << " " << s.roll << endl;
Nested namespace
return os;
using namespace
}
Global namespace
std namespace
private: char *name; GENDER gender; // name and gender provided for the student
namespaces are int roll; // roll is assigned by the system
Open };
namespace class StReg { // Students’ Register
vis-a-vis class St **rec; /* List of students */ int nStudents; // Number of student
Lexical Scope
public: StReg(int size) : rec(new St*[size]), nStudents(0) { }
void add(St* s) { rec[nStudents] = s; s->setRoll(++nStudents); }
Module Summary St *getStudent(int r) { return (r == nStudents + 1) ? 0 : rec[r - 1]; }
};
• The classes are included in a header file Students.h
Programming in Modern C++ Partha Pratim Das M20.10
Scenario 2: Students’ Record Application: Team at Work
Program 20.03
Module M20
• Two engineers – Sabita and Niloy – are assigned to develop processing applications for male and female students
respectively. Both are given the Students.h file
• The lead Purnima of Sabita and Niloy has the responsibility to integrate what they produce and prepare a single

L
Partha Pratim
Das application for both male and female students. The engineers produce:
Processing for males by Sabita Processing for females by Niloy

E
Objectives &
Outlines ////////////// App1.cpp ////////////// ////////////// App2.cpp //////////////
#include <iostream> #include <iostream>

T
namespace
Fundamental using namespace std; using namespace std;
#include "Students.h" #include "Students.h"

P
namespace
Scenarios extern StReg *reg; extern StReg *reg;
void ProcSt() { cout << "MALE STUDENTS: " << endl; void ProcSt() { cout << "FEMALE STUDENTS: " << endl;
namespace
int r = 1; St *s; int r = 1; St *s;

N
Features
Nested namespace
while (s = reg->getStudent(r++)) while (s = reg->getStudent(r++))
using namespace if (s->getGender() == St::male) cout << *s; if (s->getGender() == St::female) cout << *s;
Global namespace cout << endl << endl; cout << endl << endl;
std namespace return; return;
namespaces are } }
Open
////////////// Main.cpp ////////////// ////////////// Main.cpp //////////////
namespace #include <iostream> #include <iostream>
vis-a-vis class using namespace std; using namespace std;
Lexical Scope #include "Students.h" #include "Students.h"
StReg *reg = new StReg(1000); StReg *reg = new StReg(1000);
Module Summary
int main() int main()
{ St s("Ravi", St::male); reg->add(&s); ProcSt(); } { St s("Rhea", St::female); reg->add(&s); ProcSt(); }
Programming in Modern C++ Partha Pratim Das M20.11
Scenario 2: Students’ Record Application: Integration Nightmare:
Program 20.03
Module M20 • To integrate, Purnima prepares the following main() in her Main.cpp where she intends to call the processing
functions for males (as prepared by Sabita) and for females (as prepared by Niloy) one after the other:

L
Partha Pratim
Das #include <iostream>
using namespace std;

E
Objectives & #include "Students.h"
Outlines

T
namespace void ProcSt(); // Function from App1.cpp by Sabita
Fundamental void ProcSt(); // Function from App2.cpp by Niloy

P
namespace
Scenarios StReg *reg = new StReg(1000);
namespace

N
Features int main() {
Nested namespace St s1("Rhea", St::female); reg->add(&s1);
using namespace St s2("Ravi", St::male); reg->add(&s2);
Global namespace
std namespace ProcSt(); // Function from App1.cpp by Sabita
namespaces are ProcSt(); // Function from App2.cpp by Niloy
Open
}
namespace
vis-a-vis class • But the integration failed due to name clashes
Lexical Scope
• Both use the same signature void ProcSt(); for their respective processing function. Actually, they have several
functions, classes, and variables in their respective development with the same name and with same / different
Module Summary purposes
• How does Purnima perform the integration without major changes in the codes? – namespace

Programming in Modern C++ Partha Pratim Das M20.12


Scenario 2: Students’ Record Application: Wrap in namespace
Program 20.03
Module M20 • Introduce two namespaces – App1 for Sabita and App2 for Niloy
• Wrap the respective codes:

L
Partha Pratim
Das Processing for males by Sabita Processing for females by Niloy
////////////// App1.cpp ////////////// ////////////// App2.cpp //////////////

E
Objectives & #include <iostream> #include <iostream>
Outlines using namespace std; using namespace std;

T
namespace #include "Students.h" #include "Students.h"
Fundamental
extern StReg *reg; extern StReg *reg;

P
namespace
Scenarios

namespace namespace App1 { namespace App2 {

N
Features void ProcSt() { void ProcSt() {
Nested namespace cout << "MALE STUDENTS: " << endl; cout << "FEMALE STUDENTS: " << endl;
using namespace int r = 1; int r = 1;
Global namespace St *s; St *s;
std namespace
namespaces are
Open
while (s = reg->getStudent(r++)) while (s = reg->getStudent(r++))
if (s->getGender() == St::male) if (s->getGender() == St::female)
namespace
vis-a-vis class
cout << *s; cout << *s;
Lexical Scope cout << endl << endl; cout << endl << endl;
Module Summary return; return;
} }
}; };
Programming in Modern C++ Partha Pratim Das M20.13
Scenario 2: Students’ Record Application: A Good Night’s Sleep
Program 20.03
Module M20
• Now the integration gets smooth:

L
Partha Pratim
Das using namespace std;

E
Objectives & #include "Students.h"
Outlines

T
namespace namespace App1 { void ProcSt(); } // App1.cpp by Sabita
Fundamental
namespace App2 { void ProcSt(); } // App2.cpp by Niloy

P
namespace
Scenarios

namespace StReg *reg = new StReg(1000);

N
Features
Nested namespace int main() {
using namespace St s1("Ravi", St::female); reg->add(&s1);
Global namespace St s2("Rhea", St::male); reg->add(&s2);
std namespace
namespaces are
Open
App1::ProcSt(); // App1.cpp by Sabita
App2::ProcSt(); // App2.cpp by Niloy
namespace
vis-a-vis class
return 0;
Lexical Scope }
Module Summary • Clashing names are made distinguishable by distinct names

Programming in Modern C++ Partha Pratim Das M20.14


namespace Features

Module M20

L
Partha Pratim
Das

E
Objectives &
Outlines

T
namespace
Fundamental

P
namespace
Scenarios

namespace

N
Features
Nested namespace
using namespace
Global namespace
std namespace
namespaces are
Open

namespace
namespace Features
vis-a-vis class

Lexical Scope

Module Summary

Programming in Modern C++ Partha Pratim Das M20.15


Program 20.04: Nested namespace

Module M20
• A namespace may be nested in another namespace

L
Partha Pratim
Das #include <iostream>
using namespace std;

E
Objectives &
Outlines int data = 0; // Global name ::

T
namespace
Fundamental namespace name1 {
int data = 1; // In namespace name1

P
namespace
Scenarios namespace name2 {
namespace
int data = 2; // In nested namespace name1::name2

N
Features }
Nested namespace }
using namespace
Global namespace int main() {
std namespace cout << data << endl; // 0
namespaces are cout << name1::data << endl; // 1
Open
cout << name1::name2::data << endl; // 2
namespace
vis-a-vis class
return 0;
Lexical Scope }
Module Summary

Programming in Modern C++ Partha Pratim Das M20.16


Program 20.05: Using using namespace and using for shortcut

Module M20
• Using using namespace we can avoid lengthy prefixes

L
Partha Pratim
Das #include <iostream>
using namespace std;

E
Objectives &
Outlines
namespace name1 {

T
namespace int v11 = 1;
Fundamental int v12 = 2;
}

P
namespace
Scenarios namespace name2 {
namespace int v21 = 3;

N
Features int v22 = 4;
Nested namespace }
using namespace
Global namespace
using namespace name1; // All symbols of namespace name1 will be available
std namespace
using name2::v21; // Only v21 symbol of namespace name2 will be available
namespaces are
Open
int main() {
namespace
vis-a-vis class cout << v11 << endl; // name1::v11
cout << name1::v12 << endl; // name1::v12
Lexical Scope
cout << v21 << endl; // name2::v21
Module Summary cout << name2::v21 << endl; // name2::v21
cout << v22 << endl; // Treated as undefined
}
Programming in Modern C++ Partha Pratim Das M20.17
Program 20.06: Global namespace

Module M20
• using or using namespace hides some of the names

L
Partha Pratim
Das
#include <iostream>
using namespace std;

E
Objectives &
Outlines

T
namespace int data = 0; // Global Data
Fundamental

P
namespace namespace name1 {
Scenarios
int data = 1; // namespace Data
namespace
}

N
Features
Nested namespace
using namespace int main() {
Global namespace
std namespace
using name1::data;
namespaces are
Open
cout << data << endl; // 1 // name1::data -- Hides global data
namespace
vis-a-vis class
cout << name1::data << endl; // 1
cout << ::data << endl; // 0 // ::data -- global data
Lexical Scope
}
Module Summary
• Items in Global namespace may be accessed by scope resolution operator (::)

Programming in Modern C++ Partha Pratim Das M20.18


Program 20.07: std Namespace

Module M20
• Entire C++ Standard Library is put in its own namespace, called std

L
Partha Pratim
Das Without using using std With using using std

E
Objectives & #include <iostream> #include <iostream>
Outlines
using namespace std;

T
namespace
Fundamental int main() { int main() {
int num; int num;

P
namespace
Scenarios std::cout << "Enter a value: " ; cout << "Enter a value: " ;
namespace std::cin >> num; cin >> num;

N
Features std::cout << "value is: " ; cout << "value is: " ;
Nested namespace std::cout << num ; cout << num ;
using namespace } }
Global namespace
std namespace • Here, cout, cin are explicitly qualified by their • By the statement using namespace std; std
namespaces are
Open
namespace. So, to write to standard output, we spec- namespace is brought into the current namespace,
ify std::cout; to read from standard input, we use which gives us direct access to the names of the func-
namespace
vis-a-vis class
std::cin tions and classes defined within the library without hav-
ing to qualify each one with std::
Lexical Scope • It is useful if a few library is to be used; no need to • When several libraries are to be used it is a convenient
Module Summary add entire std library to the global namespace method

Programming in Modern C++ Partha Pratim Das M20.19


Program 20.08: namespaces are Open

Module M20 • namespace are open: New Declarations can be added

L
Partha Pratim
Das
#include <iostream>

E
Objectives & using namespace std;
Outlines

T
namespace namespace open // First definition
Fundamental
{ int x = 30; }

P
namespace
Scenarios

namespace
namespace open // Additions to the last definition

N
Features { int y = 40; }
Nested namespace
using namespace
Global namespace
int main() {
std namespace using namespace open; // Both x and y would be available
namespaces are
Open
x = y = 20;
namespace
vis-a-vis class cout << x << " " << y ;
Lexical Scope
}
Module Summary
Output: 20 20

Programming in Modern C++ Partha Pratim Das M20.20


namespace vis-a-vis class

Module M20

L
Partha Pratim
Das

E
Objectives &
Outlines

T
namespace
Fundamental

P
namespace
Scenarios

namespace

N
Features
Nested namespace
using namespace
Global namespace
std namespace
namespaces are
Open

namespace
namespace vis-a-vis class
vis-a-vis class

Lexical Scope

Module Summary

Programming in Modern C++ Partha Pratim Das M20.21


namespace vis-a-vis class

Module M20
namespace class

L
Partha Pratim
Das

• Every namespace is not a class • Every class defines a namespace

E
Objectives &
Outlines
• A namespace can be reopened and more • A class cannot be reopened

T
namespace
Fundamental declaration added to it
• No instance of a namespace can be created • A class has multiple instances

P
namespace
Scenarios
• using-declarations can be used to short- • No using-like declaration for a class
namespace

N
Features cut namespace qualification
Nested namespace
using namespace
• A namespace may be unnamed • An unnamed class is not allowed
Global namespace
std namespace
namespaces are
Open

namespace
vis-a-vis class

Lexical Scope

Module Summary

Programming in Modern C++ Partha Pratim Das M20.22


Lexical Scope

Module M20

L
Partha Pratim
Das

E
Objectives &
Outlines

T
namespace
Fundamental

P
namespace
Scenarios

namespace

N
Features
Nested namespace
using namespace
Global namespace
std namespace
namespaces are
Open

namespace
Lexical Scope
vis-a-vis class

Lexical Scope

Module Summary

Programming in Modern C++ Partha Pratim Das M20.23


Lexical Scope

Module M20

• The scope of a name binding – an association of a name to an entity, such as a variable – is

L
Partha Pratim
Das
the part of a computer program where the binding is valid: where the name can be used to

E
Objectives & refer to the entity
Outlines
• C++ supports a variety of scopes:

T
namespace
Fundamental
◦ Expression Scope – restricted to one expression, mostly used by compiler

P
namespace
Scenarios ◦ Block Scope – create local context
namespace ◦ Function Scope – create local context associated with a function

N
Features
Nested namespace ◦ Class Scope – context for data members and member functions
using namespace
Global namespace
◦ Namespace Scope – grouping of symbols for code organization
std namespace ◦ File Scope – limit symbols to a single file
namespaces are
Open
◦ Global Scope – outer-most, singleton scope containing the whole program
namespace
vis-a-vis class

Lexical Scope

Module Summary

Programming in Modern C++ Partha Pratim Das M20.24


Lexical Scope

Module M20
• Scopes may be named or Unnamed

L
Partha Pratim
Das ◦ Named Scope – Option to refer to the scope from outside
. Class Scope – class name

E
Objectives &
Outlines
. Namespace Scope – namespace name or unnamed
. Global Scope – ”::”

T
namespace
Fundamental
◦ Unnamed Scope
. Expression Scope

P
namespace
Scenarios . Block Scope
. Function Scope
namespace
. File Scope

N
Features
Nested namespace • Scopes may or may not be nested
using namespace
Global namespace
◦ Scopes that may be nested
std namespace . Block Scope
namespaces are . Class Scope
Open
. Namespace Scope
namespace
vis-a-vis class
◦ Scopes that cannot be nested
. Expression Scope
Lexical Scope
. Function Scope – may contain Class Scopes
Module Summary . File Scope – will contain several other scopes
. Global Scope – will contain several other scopes
Programming in Modern C++ Partha Pratim Das M20.25
Module Summary

Module M20

• Understood namespace as a scoping tool in c++

L
Partha Pratim
Das
• Analyzed typical scenarios that namespace helps to address

E
Objectives &
Outlines • Studied several features of namespace

T
namespace
Fundamental • Understood how namespace is placed in respect of different lexical scopes of C++

P
namespace
Scenarios

namespace

N
Features
Nested namespace
using namespace
Global namespace
std namespace
namespaces are
Open

namespace
vis-a-vis class

Lexical Scope

Module Summary

Programming in Modern C++ Partha Pratim Das M20.26


Tutorial T04

L
Partha Pratim
Das
Programming in Modern C++

E
Tutorial Recap

Objectives & Tutorial T04: How to build a C/C++ program?: Part 4: Static and Dynamic Library

T
Outline

What is a

P
Library?
Static vs Shared

Our Library Partha Pratim Das

N
Project

Static Library
Build Steps
Department of Computer Science and Engineering
Execution Trace
Indian Institute of Technology, Kharagpur
Shared Library
ppd@cse.iitkgp.ac.in
Build Steps
Execution Trace
Set Library Path
Dynamic Loading
All url’s in this module have been accessed in September, 2021 and found to be functional
Windows DLL

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.1


Tutorial Recap

Tutorial T04

• Understood the build process and pipeline for C/C++ projects

L
Partha Pratim
Das
• Learnt make for build automation

E
Tutorial Recap

Objectives &

T
Outline

What is a

P
Library?
Static vs Shared

Our Library

N
Project

Static Library
Build Steps
Execution Trace

Shared Library
Build Steps
Execution Trace
Set Library Path
Dynamic Loading

Windows DLL

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.2


Tutorial Objective

Tutorial T04

• To understand the role of libraries in C/C++ projects

L
Partha Pratim
Das
• To learn about Static and Shared Libraries - how to build and use them

E
Tutorial Recap

Objectives &

T
Outline

What is a

P
Library?
Static vs Shared

Our Library

N
Project

Static Library
Build Steps
Execution Trace

Shared Library
Build Steps
Execution Trace
Set Library Path
Dynamic Loading

Windows DLL

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.3


Tutorial Outline

Tutorial T04
1 Tutorial Recap

L
Partha Pratim
Das 2 What is a Library?
Static vs Shared

E
Tutorial Recap

Objectives &
3 Our Library Project

T
Outline

What is a
4 Static Library Project

P
Library?
Static vs Shared Build Steps
Our Library Execution Trace

N
Project

Static Library 5 Shared / Dynamic Library Project


Build Steps
Execution Trace
Build Steps
Shared Library Execution Trace
Build Steps Set Library Path
Execution Trace
Set Library Path
Late Binding and Dynamic Loading
Dynamic Loading

Windows DLL
6 Dynamic Link Library (DLL) Project: Windows
Tutorial Summary 7 Tutorial Summary
Programming in Modern C++ Partha Pratim Das T04.4
What is a Library?

Tutorial T04

L
Partha Pratim
Das

E
Tutorial Recap

Objectives &

T
Outline

What is a

P
Library?
Static vs Shared

Our Library

N
Project

Static Library
Build Steps
Execution Trace

Shared Library
What is a Library?
Build Steps
Execution Trace
Set Library Path Source: All Accessed 23-Sep-21
Dynamic Loading Static library, Wikipedia
Dynamic-link library, Wikipedia
Windows DLL Library (computing), Wikipedia
C standard library, Wikipedia
Tutorial Summary C++ Standard Library, Wikipedia

Programming in Modern C++ Partha Pratim Das T04.5


What is a Library?

Tutorial T04
• A library is a package of code that is meant to be reused by many programs. Typically,
a C / C++ library comes in two pieces:

L
Partha Pratim
Das

◦ A header file that defines the functionality the library is exposing (offering) to the

E
Tutorial Recap

Objectives &
programs using it. For example, stdio.h, math.h, etc. in C and iostream,

T
Outline
vector, etc. in C++
What is a
◦ A pre-compiled binary that contains the implementation of that functionality

P
Library?
Static vs Shared
pre-compiled into machine language. For example, glibc is the GNU C (Standard)
Our Library
Library on Unix

N
Project

Static Library Some libraries may be split into multiple files and/or have multiple header files
Build Steps
Execution Trace • Libraries are pre-compiled because
Shared Library
Build Steps
◦ As libraries rarely change, they do not need to be recompiled often. They can just
Execution Trace be reused in binary
Set Library Path
Dynamic Loading ◦ As pre-compiled objects are in machine language, it prevents people from accessing
Windows DLL or changing the source code protecting IP
Tutorial Summary
Source: A.1 — Static and dynamic libraries (Accessed 23-Sep-21)

Programming in Modern C++ Partha Pratim Das T04.6


Types of Library

Tutorial T04 • Static Library


◦ Consists of routines that are compiled and linked into the program. A program compiled with a

L
Partha Pratim
Das
static library would have the functionality of the library as a part of the executable

E
Tutorial Recap Extensions:
Objectives & . Unix: .a (archive)
.

T
Outline Windows: .lib
What is a
• Dynamic / Shared Library

P
Library?
Static vs Shared
◦ Consists of routines that are loaded into the application at run time
Our Library
◦ Extensions:

N
Project

Static Library
. Unix: .so (shared object)
Build Steps
. Windows: .dll (dynamic link library)
Execution Trace
• Import Library
Shared Library
Build Steps ◦ An import library automates the process of loading and using a dynamic library
Execution Trace ◦ Extensions:
Set Library Path
Dynamic Loading
. Unix: Shared object (.so) file doubles as both a dynamic and an import library
. Windows: A small static library (.lib) of the same name as the dynamic library (.dll). The static library is
Windows DLL
linked into the program at compile time, and then the functionality of the dynamic library can effectively be
Tutorial Summary used as if it were a static library

Programming in Modern C++ Partha Pratim Das T04.7


Static Library

Tutorial T04 Static Library: Library Code is internal to Application

L
Partha Pratim
Das
• Application needs to recompile - difficult
version management

E
Tutorial Recap

Objectives & ◦ If library implementation changes -

T
Outline
regular with version upgrade / bug
What is a fixes

P
Library?
Static vs Shared
◦ And naturally, if library interface
Our Library
changes - infrequent

N
Project • Large footprint - especially bad for mo-
Static Library bile apps
Build Steps
• Multiple copies of the same library may
Execution Trace
be loaded as part of different applica-
Shared Library
tions - bad for mobile apps
Build Steps
Execution Trace
• Fast in speed as the library is already
Set Library Path loaded and linked
Dynamic Loading

Windows DLL

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.8


Shared / Dynamic Library

Tutorial T04 Shared / Dynamic Library: Only Library reference is internal to Application - Library Code is external

L
Partha Pratim
Das • Application does not need to recompile
- easy version management

E
Tutorial Recap
◦ If library implementation changes -
Objectives &
regular with version upgrade / bug

T
Outline
fixes
What is a
◦ However, it will need to recompile

P
Library?
Static vs Shared (like the static library), if library in-
Our Library terface changes - infrequent

N
Project
• Small footprint - especially good for mo-
Static Library
Build Steps
bile apps
Execution Trace • Single copy of the library will be loaded
Shared Library
for different applications - good for mo-
Build Steps bile apps
Execution Trace • The functions in the library needs to be
Set Library Path
Dynamic Loading
re-entrant. Care is needed with static
variables
Windows DLL
• Slow in speed as the library may need to
Tutorial Summary
be loaded and linked at run-time
Programming in Modern C++ Partha Pratim Das T04.9
Static vs Shared Library

Tutorial T04

L
Partha Pratim
Das

E
Tutorial Recap

Objectives &

T
Outline

What is a

P
Library?
Static vs Shared

Our Library

N
Project

Static Library
Build Steps
Execution Trace

Shared Library
Build Steps
Execution Trace
Set Library Path
Dynamic Loading

Windows DLL

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.10


Static vs Shared Library

Tutorial T04
Property Static Library Shared Library

L
Partha Pratim
Das Recompilation is required for changes in ex- No need to recompile the executable
Compilation ternal files

E
Tutorial Recap
Happens as the last step of the compilation Are added during linking when executable
Objectives & Linking time process file and libraries are added to the memory

T
Outline
Are resolved in a caller at compile-time and Get imported at the time of execution of
What is a Import / copied into a target application by the linker target program by the OS

P
Library?
Static vs Shared
Mechanism
Our Library
Are bigger in size, because external pro- Are smaller, because there is only one copy

N
Project
Size grams are built in the executable file of shared library that is kept in memory
Static Library Executable file will have to be recompiled if No need to recompile the executable - only
Build Steps
External file any changes were applied to external files the shared library is replaced
Execution Trace changes
Shared Library Takes longer to execute, as loading into Faster because shared library is already in
Build Steps Time / memory happens every time while executing the memory
Execution Trace Performance
Set Library Path
Never has compatibility issue, since all code Programs are dependent on having a com-
Dynamic Loading
Compatibility is in one executable module patible library
Windows DLL

Tutorial Summary Source: Difference between Static and Shared libraries and Difference between Static and Shared libraries (Accessed 23-Sep-21)

Programming in Modern C++ Partha Pratim Das T04.11


Our Library Project

Tutorial T04

L
Partha Pratim
Das

E
Tutorial Recap

Objectives &

T
Outline

What is a

P
Library?
Static vs Shared

Our Library

N
Project

Static Library
Build Steps
Execution Trace

Shared Library
Build Steps
Execution Trace
Our Library Project
Set Library Path
Dynamic Loading
Sources: All Accessed 26-Sep-21
Windows DLL Building And Using Static And Shared ”C” Libraries
Shared libraries with GCC on Linux
Tutorial Summary MinGW Static and Dynamic Libraries
Static and Dynamic Libraries in C Language
Programming in Modern C++ Partha Pratim Das T04.12
Our Library Project

Tutorial T04

• We present a tiny project to illustrate the ideas of static and shared / dynamic libraries

L
Partha Pratim
Das
• We use the same set of header and source files to create and use

E
Tutorial Recap

Objectives &
◦ Static Library

T
Outline ◦ Shared / Dynamic Library
What is a
and compare them

P
Library?
Static vs Shared

Our Library
• First the projects are created with gcc. These can work on Unix as well as Windows

N
Project (with minGW: MinGW - Minimalist GNU for Windows)
Static Library
Build Steps • Then we show Microsoft-specific process on Windows
Execution Trace

Shared Library
Build Steps
Execution Trace
Set Library Path
Dynamic Loading

Windows DLL

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.13


Project Files
/* lib_myMath.h: Header for my mathematical functions */ /* CPP guards omitted for brevity */
Tutorial T04
int myMax(int, int);
int myMin(int, int);

L
Partha Pratim
Das
/* lib_myMath.c: Implementation for my mathematical functions */

E
Tutorial Recap
#include "lib_myMath.h"
Objectives & int myMax(int a, int b) { return a>b? a: b; }

T
Outline int myMin(int a, int b) { return a<b? a: b; }
What is a

P
Library? /* lib_myPrint.h: Header for my printing function */ /* CPP guards omitted for brevity */
Static vs Shared void myPrint(const char*, int);
Our Library

N
Project /* lib_myPrint.c: Implementation for my printing function */
Static Library #include <stdio.h>
Build Steps #include "lib_myPrint.h"
Execution Trace void myPrint(const char *name, int a) { printf("%s: %d\n", name, a); }
Shared Library
Build Steps
/* myApp.c: My application */
Execution Trace
#include <stdio.h>
Set Library Path #include "lib_myMath.h"
Dynamic Loading #include "lib_myPrint.h"
int main() {
Windows DLL
myPrint("Max(3, 5)", myMax(3, 5));
Tutorial Summary myPrint("Min(3, 5)", myMin(3, 5));
}
Programming in Modern C++ Partha Pratim Das T04.14
Project: Folders and Code Organization
Home // Library demonstration project with library as well as application Home = Static or Shared
Tutorial T04
|
---- app // Application files - will use library headers from ../inc and library from ../lib

L
Partha Pratim
Das | ---- myApp.c // Application Source file
| ---- myApp.exe // Application Executable

E
Tutorial Recap | ---- myApp.o // Application Object file
Objectives & | ---- myMakeApp.txt // Application Makefile

T
Outline |
What is a ---- inc // Headers to be included in application and library build

P
Library? | ---- lib_myMath.h
Static vs Shared | ---- lib_myPrint.h
Our Library |

N
Project ---- lib // Library files
Static Library
|
Build Steps
---- obj // Library object files
Execution Trace
| ---- lib_myMath.o
| ---- lib_myPrint.o
Shared Library
|
Build Steps
---- src // Library source files - will use library headers from ../../inc
Execution Trace
Set Library Path
| ---- lib_myMath.c
Dynamic Loading
| ---- lib_myPrint.c
|
Windows DLL
---- lib_mylib.a // Static Library binary file linked by the application
Tutorial Summary ---- lib_mylib.so // Shared Library binary file linked by the application
---- myMakeLibrary.txt // Library Makefile
Programming in Modern C++ Partha Pratim Das T04.15
Static Library Project

Tutorial T04

L
Partha Pratim
Das

E
Tutorial Recap

Objectives &

T
Outline

What is a

P
Library?
Static vs Shared

Our Library

N
Project

Static Library
Build Steps
Execution Trace

Shared Library
Build Steps
Execution Trace
Static Library Project
Set Library Path
Dynamic Loading
Sources: All Accessed 26-Sep-21
Windows DLL A.1 — Static and dynamic libraries
A.2 — Using libraries with Visual Studio
Tutorial Summary
A.3 — Using libraries with Code::Blocks

Programming in Modern C++ Partha Pratim Das T04.16


Static Library Project: Build Steps

Tutorial T04 • We can build this project by


$ gcc lib_myMath.c lib_myPrint.c myApp.c -o myApp

L
Partha Pratim
Das
• Every time myApp.c is updated, we build lib myMath.c and lib myPrint.c even if

E
Tutorial Recap
there is no change. We can avoid the recompile by retaining the object files as:
Objectives &

T
Outline $ gcc -c lib_myMath.c lib_myPrint.c
What is a $ gcc lib_myMath.o lib_myPrint.o myApp.c -o myApp

P
Library?
Static vs Shared
• When we have many such files that rarely change, we would have a lot of such .o files
Our Library to maintain. These can be bundled into an archive lib mylib.a for ease of reference

N
Project
$ ar rcs lib_mylib.a lib_myMath.o lib_myPrint.o
Static Library
Build Steps
◦ GNU ar utility creates, modifies, and extracts from archives (like ZIP) - holding a collection of
Execution Trace multiple files in a structure that makes it possible to retrieve the individual files (called members)
Shared Library ◦ Option rcs asks to create (c) an archive with replacement (r) of members and indexing (s)
Build Steps ◦ For details check: ar(1) — Linux manual page
Execution Trace
Set Library Path • Finally we use the .a file in place of the .o’s to link to myApp.o
Dynamic Loading
$ gcc -o myApp myApp.c lib_myLib.a -L.
Windows DLL

Tutorial Summary
Alternately, we can place lib myLib.a in default library path and link by -l mylib
$ gcc -o myApp myApp.c -l_mylib -L.
Programming in Modern C++ Partha Pratim Das T04.17
Static Library Project: Makefiles
Static Library makefile Application makefile
Tutorial T04

L
Partha Pratim # Variables # Variables
Das CC=gcc CC=gcc
AR=ar IDIR=inc

E
Tutorial Recap
SDIR=src LDIR=lib
Objectives & IDIR=../inc CFLAGS=-I../$(IDIR)

T
Outline ODIR=obj LFLAGS=-L../$(LDIR)
What is a CFLAGS=-I$(IDIR) DEPS=

P
Library? LFLAGS=-L.
Static vs Shared AFLAGS=rcs
Our Library # Macros

N
Project _DEPS = lib_myMath.h lib_myPrint.h
Static Library DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
Build Steps _SRC = lib_myMath.c lib_myPrint.c
Execution Trace SRC = $(patsubst %,$(SDIR)/%,$(_SRC))
Shared Library
_OBJ = lib_myMath.o lib_myPrint.o
Build Steps
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
Execution Trace
# Rules # Rules
Set Library Path $(ODIR)/%.o: $(SDIR)/%.c $(DEPS) %.o: %.c $(DEPS)
Dynamic Loading $(CC) -c -o $@ $< $(CFLAGS) -I. $(CC) -c -o $@ $< $(CFLAGS)
Windows DLL
%.o: $(SDIR)/%.c $(DEPS) myApp: myApp.o
$(CC) -c -o $@ $< $(CFLAGS) $(CC) -o myApp myApp.o -l_mylib $(LFLAGS)
Tutorial Summary lib_mylib.a: $(OBJ)
$(AR) $(AFLAGS) -o $@ $^
Programming in Modern C++ Partha Pratim Das T04.18
Static Library Project: Execution Trace

Tutorial T04 • Let us build and execute the project

L
Partha Pratim
Das
// Build Library
D:\Library\Static\lib $ make -f myMakeLibrary.txt

E
Tutorial Recap

Objectives &
gcc -c -o obj/lib_myMath.o src/lib_myMath.c -I../inc -I.

T
Outline gcc -c -o obj/lib_myPrint.o src/lib_myPrint.c -I../inc -I.
What is a ar rcs -o lib_mylib.a obj/lib_myMath.o obj/lib_myPrint.o

P
Library?
Static vs Shared
// Build Application
Our Library

N
Project
D:\Library\Static\app $ make -f myMakeApp.txt
gcc -c -o myApp.o myApp.c -I../inc
Static Library
Build Steps
gcc -o myApp myApp.o -l_mylib -L../lib
Execution Trace

Shared Library // Run Application


Build Steps D:\Library\Static\app $ myApp.exe
Execution Trace
Max(3, 5): 5
Set Library Path
Dynamic Loading
Min(3, 5): 3
Windows DLL • So the static library is working as expected
Tutorial Summary
• Next we check on the contents of various folders
Programming in Modern C++ Partha Pratim Das T04.19
Static Library Project: Directory Listing

Tutorial T04 Directory of D:\Library\Static


26-09-2021 14:58 <DIR> app

L
Partha Pratim
Das
26-09-2021 14:58 <DIR> inc
26-09-2021 14:58 <DIR> lib
Directory of D:\Library\Static\app

E
Tutorial Recap
24-09-2021 15:53 179 myApp.c
Objectives &
26-09-2021 11:35 42,348 myApp.exe

T
Outline
26-09-2021 11:35 954 myApp.o
What is a 25-09-2021 13:23 215 myMakeApp.txt

P
Library?
Directory of D:\Library\Static\inc
Static vs Shared
24-09-2021 13:17 66 lib_myMath.h
Our Library

N
24-09-2021 13:17 54 lib_myPrint.h
Project
Directory of D:\Library\Static\lib
Static Library 26-09-2021 11:33 1,722 lib_mylib.a
Build Steps 25-09-2021 13:22 524 myMakeLibrary.txt
Execution Trace 26-09-2021 14:58 <DIR> obj
Shared Library 26-09-2021 14:58 <DIR> src
Build Steps Directory of D:\Library\Static\lib\obj
Execution Trace 26-09-2021 11:33 716 lib_myMath.o
Set Library Path 26-09-2021 11:33 778 lib_myPrint.o
Dynamic Loading Directory of D:\Library\Static\lib\src
Windows DLL 24-09-2021 13:16 143 lib_myMath.c
24-09-2021 13:18 144 lib_myPrint.c
Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.20


Shared / Dynamic Library Project

Tutorial T04

L
Partha Pratim
Das

E
Tutorial Recap

Objectives &

T
Outline

What is a

P
Library?
Static vs Shared

Our Library

N
Project

Static Library
Build Steps
Execution Trace

Shared Library
Build Steps
Execution Trace
Shared / Dynamic Library Project
Set Library Path
Dynamic Loading
Sources: All Accessed 26-Sep-21
Windows DLL
A.1 — Static and dynamic libraries
Tutorial Summary A.2 — Using libraries with Visual Studio
A.3 — Using libraries with Code::Blocks

Programming in Modern C++ Partha Pratim Das T04.21


Shared Library Project: Build Steps

Tutorial T04
• As in the static case, first we compile lib myMath.c and lib myPrint.c to create the
object (.o) files using the option -fPIC:

L
Partha Pratim
Das

$ gcc -fPIC -c lib_myMath.c lib_myPrint.c

E
Tutorial Recap

Objectives & ◦ -fPIC stands to mean: Compile for Position Independent Code (PIC)

T
Outline

What is a
. For a shared library, the binary of the library and the application are separate
and will be separately loaded at run time

P
Library?
Static vs Shared

Our Library
. So when the object files are generated, we need that all jump calls and

N
Project subroutine calls to use relative addresses, and not absolute addresses
Static Library
Build Steps
. -fPIC flag tells gcc to generate this type of code
Execution Trace

Shared Library
Build Steps
Execution Trace
Set Library Path
Dynamic Loading

Windows DLL

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.22


Shared Library Project: Build Steps

Tutorial T04
• Next step of building the library gets different now as we do not use ar. Rather we use
gcc with the -shared option

L
Partha Pratim
Das

$ gcc -shared -o lib_mylib.so lib_myMath.o lib_myPrint.o

E
Tutorial Recap

Objectives & • This creates a shared library lib mylib.so where the extension .so stands for a

T
Outline
shared object
What is a
• Finally we use the .so file to link to myApp.o

P
Library?
Static vs Shared

Our Library
$ gcc -o myApp myApp.c lib_myLib.so -L.

N
Project
• Alternately, we can place lib myLib.so in default library path and link by -l mylib
Static Library
Build Steps $ gcc -o myApp myApp.c -l_mylib
Execution Trace

Shared Library
Build Steps
Execution Trace
Set Library Path
Dynamic Loading

Windows DLL

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.23


Shared Library Project: Makefiles
Shared Library makefile Application makefile
Tutorial T04

L
Partha Pratim # Variables # Variables
Das CC=gcc
CC=gcc
SDIR=src IDIR=inc

E
Tutorial Recap
IDIR=../inc LDIR=lib
Objectives & ODIR=obj CFLAGS=-I../$(IDIR)

T
Outline LFLAGS=-L../$(LDIR)
CFLAGS=-I$(IDIR)
What is a LFLAGS=-L. DEPS=

P
Library?
Static vs Shared
# Macros
Our Library _DEPS = lib_myMath.h lib_myPrint.h

N
Project DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))
Static Library _SRC = lib_myMath.c lib_myPrint.c
Build Steps SRC = $(patsubst %,$(SDIR)/%,$(_SRC))
Execution Trace _OBJ = lib_myMath.o lib_myPrint.o
Shared Library
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))
Build Steps
Execution Trace # Rules # Rules
Set Library Path $(ODIR)/%.o: $(SDIR)/%.c $(DEPS) %.o: %.c $(DEPS)
Dynamic Loading $(CC) -fPIC -c -o $@ $< $(CFLAGS) -I. $(CC) -c -o $@ $< $(CFLAGS)
Windows DLL
lib_mylib.so: $(OBJ) myApp: myApp.o
$(CC) -shared -o $@ $^ $(CC) -o myApp myApp.o ../$(LDIR)/lib_mylib.so
Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.24


Shared Library Project: Execution Trace

Tutorial T04 • Let us build and execute the project


// Build Library

L
Partha Pratim
Das
D:\Library\Shared\lib $ make -f myMakeLibrary.txt
gcc -fPIC -c -o obj/lib_myMath.o src/lib_myMath.c -I../inc -I.

E
Tutorial Recap

Objectives &
gcc -fPIC -c -o obj/lib_myPrint.o src/lib_myPrint.c -I../inc -I.

T
Outline gcc -shared -o lib_mylib.so obj/lib_myMath.o obj/lib_myPrint.o
What is a

P
Library? // Build Application
Static vs Shared
D:\Library\Shared\app $ make -f myMakeApp.txt
Our Library

N
Project
gcc -c -o myApp.o myApp.c -I../inc
gcc -o myApp myApp.o ../lib/lib_mylib.so
Static Library
Build Steps
Execution Trace // Run Application
Shared Library D:\Library\Shared\app $ myApp.exe
Build Steps
Execution Trace
Set Library Path
Dynamic Loading Oops! The shared library is not found! The
Windows DLL system does not know that lib mylib.so is in
Tutorial Summary D:\Library\Shared\lib

Programming in Modern C++ Partha Pratim Das T04.25


Shared Library Project: Execution Trace

Tutorial T04
• If we copy lib mylib.so to the application folder D:\Library\Shared\app (where
myApp.exe resides), the problem goes away and the application runs successfully

L
Partha Pratim
Das
// Run Application

E
Tutorial Recap D:\Library\Shared\app $ myApp.exe
Max(3, 5): 5
Objectives &
Min(3, 5): 3

T
Outline

What is a • So the shared library is working as expected

P
Library?
Static vs Shared ◦ However, copying the shared library to the application folder is not preferred as we would
Our Library need to copy lib mylib.so to every application that would use it.

N
Project
◦ We shall discuss a solution to this library path problem in the next section
Static Library
Build Steps • Next we check on the contents of various folders and compare the size of the libraries and
Execution Trace
applications in static and shared cases
Shared Library
Build Steps Static Library Shared Library Remarks
Execution Trace
lib mylib.a 1,722 lib mylib.so 27,749 File .so is larger than .a due to the overhead of ex-
Set Library Path
ported references. With large number of functions in
Dynamic Loading
the library (as opposed to just 3) the relative overhead
Windows DLL will go down
Tutorial Summary myApp.exe 42,348 myApp.exe 41,849 Shared .exe would be relatively much smaller with more
functions in the library
Programming in Modern C++ Partha Pratim Das T04.26
Shared Library Project: Directory Listing

Tutorial T04 Directory of D:\Library\Shared


26-09-2021 14:58 <DIR> app

L
Partha Pratim
Das
26-09-2021 14:58 <DIR> inc
26-09-2021 14:58 <DIR> lib
Directory of D:\Library\Shared\app

E
Tutorial Recap
24-09-2021 15:53 179 myApp.c
Objectives &
26-09-2021 11:45 41,849 myApp.exe

T
Outline
26-09-2021 11:45 954 myApp.o
What is a 26-09-2021 09:41 220 myMakeApp.txt

P
Library?
Directory of D:\Library\Shared\inc
Static vs Shared
24-09-2021 13:17 66 lib_myMath.h
Our Library

N
24-09-2021 13:17 54 lib_myPrint.h
Project
Directory of D:\Library\Shared\lib
Static Library 26-09-2021 11:44 27,749 lib_mylib.so
Build Steps 26-09-2021 10:38 457 myMakeLibrary.txt
Execution Trace 26-09-2021 14:58 <DIR> obj
Shared Library 26-09-2021 14:58 <DIR> src
Build Steps Directory of D:\Library\Shared\lib\obj
Execution Trace 26-09-2021 11:44 716 lib_myMath.o
Set Library Path 26-09-2021 11:44 778 lib_myPrint.o
Dynamic Loading Directory of D:\Library\Shared\lib\src
Windows DLL 24-09-2021 13:16 143 lib_myMath.c
24-09-2021 13:18 144 lib_myPrint.c
Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.27


Shared Library Project: Set Library Path

Tutorial T04
• We managed to make the application work by copying the shared library to the
application folder. However, this is not preferred as we would need to copy

L
Partha Pratim
Das
lib mylib.so to every application that would use it

E
Tutorial Recap
• So we need to tell the location for a shared library to the system. This can be done in
Objectives &

T
Outline one of three ways:
What is a
◦ Place the shared library file in one of the default library folders like /usr/local/lib,

P
Library?
Static vs Shared
/usr/local/lib64, /usr/lib and /usr/lib64 (on Unix) or like C:\Windows\system32
Our Library
and C:\Windows (on Windows). Refer to the system manual details on these folders

N
Project

Static Library ◦ Add the folder of the library to the library search folders by setting path /
Build Steps
Execution Trace
environment variables:
Shared Library . Windows: Use PATH
Build Steps
Execution Trace
. Unix: Use LD LIBRARY PATH
Set Library Path
Dynamic Loading
◦ We may also Dynamically Load / Unload a library at the run-time. This is known as
Windows DLL
late binding. This is achieved by using dlopen(), dlsym(), and dlclose() from
Tutorial Summary dlfcn.h header
Programming in Modern C++ Partha Pratim Das T04.28
Shared Library Project: Set Library Path: Windows

Tutorial T04 • Windows: Set environment variables PATH to include the folder of the shared library
// Check PATH

L
Partha Pratim
Das
D:\Library\Shared\app $ PATH
PATH=C:\Windows\system32;...

E
Tutorial Recap

Objectives &

T
Outline // Set PATH
What is a D:\Library\Shared\app $ SET PATH=%PATH%;D:\Library\Shared\lib

P
Library?
Static vs Shared
// Check PATH
Our Library

N
Project
D:\Library\Shared\app $ PATH
PATH=C:\Windows\system32;...;D:\Library\Shared\app
Static Library
Build Steps
Execution Trace // Run Application
Shared Library D:\Library\Shared\app $ myApp.exe
Build Steps Max(3, 5): 5
Execution Trace
Min(3, 5): 3
Set Library Path
Dynamic Loading
◦ PATH would be reset when we end the command session. Need to set it every time
Windows DLL
◦ We may set the folder in PATH Environment Variable - Use (preferred) or System to retain
Tutorial Summary
it across sessions. Refer: How to set the path and environment variables in Windows
Programming in Modern C++ Partha Pratim Das T04.29
Shared Library Project: Set Library Path: Unix

Tutorial T04
• Unix: Set environment variable LD LIBRARY PATH to include the directory of the
shared library as follows:

L
Partha Pratim
Das

◦ For tcsh or csh:

E
Tutorial Recap

Objectives &
$ setenv LD_LIBRARY_PATH /full/path/to/library/directory:${LD_LIBRARY_PATH}

T
Outline ◦ For sh, bash and similar:
What is a LD_LIBRARY_PATH=/full/path/to/library/directory:${LD_LIBRARY_PATH}

P
Library?
Static vs Shared export LD_LIBRARY_PATH
Our Library Note:

N
Project

Static Library ◦ LD LIBRARY PATH is a list of directories in which to search for ELF libraries at
Build Steps
Execution Trace
execution time
Shared Library
◦ The items in the list are separated by either colons or semicolons, and there is no
Build Steps support for escaping either separator
Execution Trace
Set Library Path ◦ A zero-length directory name indicates the current working directory
Dynamic Loading

Windows DLL

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.30


Shared Library Project: Dynamic Loading

Tutorial T04

• We can link shared libraries to a process anytime during its life without automatically

L
Partha Pratim
Das
loading the shared library by the dynamic loader

E
Tutorial Recap
• We can do this by using the ’dl’ library – load a shared library, reference any of its
Objectives &
symbols, call any of its functions, and finally detach it from the process when no longer

T
Outline

What is a needed

P
Library?
Static vs Shared
• This is useful when there may be multiple shared libraries for the same (similar) purpose
Our Library

N
Project and we get the know which one to link only at the run-time
Static Library • The steps invoved are:
Build Steps
Execution Trace ◦ Load the library from its path using dlopen() and get its handle
Shared Library
Build Steps
◦ Use the handle to get access (function pointers) to the specific functions we intend
Execution Trace to use by dlsym()
Set Library Path
Dynamic Loading
◦ Use the function pointers to call the functions in the shared library
Windows DLL ◦ Finally, unload the library with dlclose()
Tutorial Summary
• We first present a simple schematic example
Programming in Modern C++ Partha Pratim Das T04.31
Shared Library Project: Late Binding

Tutorial T04 • Known as Late Binding as the actual functions to be called are decided only at the run-time

L
Partha Pratim Application Library
Das

#include <dlfcn.h> #include <iostream>

E
Tutorial Recap
using namespace std;
Objectives & int main() {

T
Outline
void* handle = extern "C" void hello() {
What is a dlopen("hello.so", RTLD_LAZY); cout << "hello" << endl;

P
Library?
// RTLD_LAZY: Relocations shall be performed at an }
Static vs Shared
// implementation-defined time, ranging from the
Our Library // time of the dlopen() call until the first

N
Project // reference to a given symbol occurs
Static Library
Build Steps typedef void (*hello_t)();
Execution Trace hello_t myHello = 0;
Shared Library
Build Steps myHello = (hello_t) dlsym(handle, "hello");
Execution Trace
Set Library Path myHello();
Dynamic Loading

Windows DLL dlclose(handle);


}
Tutorial Summary
• Now we present our project in the context of late binding
Programming in Modern C++ Partha Pratim Das T04.32
Shared Library Project: Dynamic Loading
/* myDynamicApp.c */
Tutorial T04
#include <stdio.h>
#include <stdlib.h>

L
Partha Pratim
Das #include "lib_myMath.h"
#include "lib_myPrint.h"

E
Tutorial Recap
#include <dlfcn.h> /* defines dlopen(), dlsym(), and dlclose() etc. */
Objectives & int main() { void* lib_handle; /* handle of the opened library */

T
Outline const char* error_msg; /* Pointer to an error string */
What is a /* Load the shared library */

P
Library? lib_handle = dlopen("D:\Library\Shared\lib\lib_mylib.so", RTLD_LAZY); /* Library path - dynamic */
Static vs Shared if (!lib_handle) { error_msg = dlerror(); goto Error; }
Our Library /* Define function pointers */

N
Project int (*myMax)(int, int); int (*myMin)(int, int); void (*myPrint)(const char*, int);
Static Library /* Locate the functions in the library. Pick by name and assign to the function pointer */
Build Steps myMax = dlsym(lib_handle, "myMax"); if (error_msg = dlerror()) goto Error;
Execution Trace myMin = dlsym(lib_handle, "myMin"); if (error_msg = dlerror()) goto Error;
myPrint = dlsym(lib_handle, "myPrint"); if (error_msg = dlerror()) goto Error;
Shared Library
Build Steps
/* Call the functions */
Execution Trace
(*myPrint)("Max(3, 5)", (*myMax)(3, 5));
Set Library Path (*myPrint)("Min(3, 5)", (*myMin)(3, 5));
Dynamic Loading /* Unload the shared library */
dlclose(lib_handle);
Windows DLL
return 0; /* Success */
Tutorial Summary Error: fprintf(stderr, "%s\n", error_msg); exit(1); /* Exit on error */
}
Programming in Modern C++ Partha Pratim Das T04.33
Dynamic Link Library (DLL) Project: Windows

Tutorial T04

L
Partha Pratim
Das

E
Tutorial Recap

Objectives &

T
Outline

What is a

P
Library?
Static vs Shared

Our Library

N
Project

Static Library
Build Steps
Execution Trace

Shared Library
Build Steps
Execution Trace
Dynamic Link Library (DLL) Project: Windows
Set Library Path
Dynamic Loading
Sources: All Accessed 27-Sep-21
Windows DLL Microsoft Visual C++ Static and Dynamic Libraries
Walkthrough: Create and use a static library
Tutorial Summary Walkthrough: Create and use your own Dynamic Link Library (C++)
How to link DLLs to C++ Projects
Programming in Modern C++ Partha Pratim Das T04.34
Microsoft Visual C++ Static and Dynamic Libraries

Tutorial T04
• While static and shared library of Unix (specifically GNU) is available on Windows through
minGW, Windows provides Microsoft specific support through MSVC

L
Partha Pratim
Das
◦ Static Library: To work with a static library:

E
Tutorial Recap
. We need to create a static library and an application project (that refers to the library
Objectives &
project) using the MSVS IDE

T
Outline

What is a − Check: Walkthrough: Create and use a static library

P
Library?
Static vs Shared
. No change in the library or application codes is needed
Our Library ◦ Dynamic Link Library (DLL): To work with a DLL:

N
Project
. We need to create a DLL and an application project (that refers to the DLL project)
Static Library
Build Steps
using the MSVS IDE
Execution Trace − Check: Walkthrough: Create and use your own Dynamic Link Library (C++)
Shared Library . We need to change the library and / or application codes with dllexport and dllimport -
Build Steps
Execution Trace
the Microsoft-specific storage-class attributes for C and C++
Set Library Path − Check: dllexport, dllimport
Dynamic Loading

Windows DLL
. These can be specified by declspec() to export and import functions, data, and
Tutorial Summary
objects to or from a DLL
. We modify the library project file to illustrate
Programming in Modern C++ Partha Pratim Das T04.35
Library Project Files: Using dllexport
/* lib_myMath.h: Header for my mathematical functions */ /* File changed for export */
Tutorial T04
__declspec(dllexport) int myMax(int, int); // storage-class attribute __declspec(dllexport) used
__declspec(dllexport) int myMin(int, int); // to export function from DLL. This is MS-specific

L
Partha Pratim
Das
/* lib_myMath.c: Implementation for my mathematical functions */ /* No change */

E
Tutorial Recap
#include "lib_myMath.h"
Objectives & int myMax(int a, int b) { return a>b? a: b; }

T
Outline int myMin(int a, int b) { return a<b? a: b; }
What is a

P
Library? /* lib_myPrint.h: Header for my printing function */ /* File changed for export */
Static vs Shared __declspec(dllexport) void myPrint(const char*, int);
Our Library

N
Project /* lib_myPrint.c: Implementation for my printing function */ /* No change */
Static Library #include <stdio.h>
Build Steps #include "lib_myPrint.h"
Execution Trace void myPrint(const char *name, int a) { printf("%s: %d\n", name, a); }
Shared Library
Build Steps
/* myApp.c: My application */ /* No change */
Execution Trace
#include <stdio.h>
Set Library Path #include "lib_myMath.h"
Dynamic Loading #include "lib_myPrint.h"
int main() {
Windows DLL
myPrint("Max(3, 5)", myMax(3, 5));
Tutorial Summary myPrint("Min(3, 5)", myMin(3, 5));
}
Programming in Modern C++ Partha Pratim Das T04.36
Library Project Files: Using dllimport

Tutorial T04
• In the context of the DLL headers,
/* lib_myMath.h */

L
Partha Pratim
Das __declspec(dllexport) int myMax(int, int);
__declspec(dllexport) int myMin(int, int);

E
Tutorial Recap
/* lib_myPrint.h */
Objectives &

T
Outline __declspec(dllexport) void myPrint(const char*, int);
What is a the application project may include the DLL headers or can just directly import the symbols by dllimport

P
Library?
Static vs Shared

Our Library
Including DLL headers Using dllexport

N
Project

Static Library /* myApp.c: My application */ /* myApp.c: My application */


Build Steps #include <stdio.h> #include <stdio.h>
Execution Trace #include "lib_myMath.h" #include "lib_myMath.h"
Shared Library
Build Steps
#include "lib_myPrint.h" // storage-class attribute __declspec(dllimport) used
Execution Trace
// to import function from DLL. This is MS-specific
Set Library Path
__declspec(dllimport) void myPrint(const char*, int);
Dynamic Loading
int main() { int main() {
Windows DLL
myPrint("Max(3, 5)", myMax(3, 5)); myPrint("Max(3, 5)", myMax(3, 5));
Tutorial Summary myPrint("Min(3, 5)", myMin(3, 5)); myPrint("Min(3, 5)", myMin(3, 5));
} }
Programming in Modern C++ Partha Pratim Das T04.37
Tutorial Summary

Tutorial T04

• Understood the role of libraries in C/C++ projects in reuse

L
Partha Pratim
Das
• Learn about Static and Shared Libraries in Unix and Windows - how to build and use

E
Tutorial Recap
them
Objectives &

T
Outline
• Learnt about DLLs
What is a

P
Library?
Static vs Shared

Our Library

N
Project

Static Library
Build Steps
Execution Trace

Shared Library
Build Steps
Execution Trace
Set Library Path
Dynamic Loading

Windows DLL

Tutorial Summary

Programming in Modern C++ Partha Pratim Das T04.38

You might also like