module-7
module-7
Intructors: Abir
Das and
Sourangshu
Bhattacharya
Module 07: Programming in C++
Objectives & Reference & Pointer
Outlines
Reference
variable
References vs.
Pointers Slides taken from NPTEL course on Programming in Modern C++
Summary
by Prof. Partha Pratim Das
Module 07
Reference
variable
Call-by-reference
Swap in C
Swap in C++
const Reference
Parameter
Return-by-
reference
I/O of a Function
References vs.
Pointers
Summary
Module 07
Summary
Module 07
Reference i ← variable
variable
Call-by-reference
15 ← memory content
Swap in C 200 ← address &i = &j
Swap in C++
const Reference
j ← alias or reference
Parameter
Return-by-
reference
I/O of a Function
References vs.
Pointers
Summary
Summary
• a and b have the same memory location and hence the same value
• Changing one changes the other and vice-versa
References vs. cout << i << ", " << &i << endl; // Prints: 2, 0x61fef8
Pointers cout << j << ", " << &j << endl; // Prints: 2, 0x61fef8
Summary cout << k << ", " << &k << endl; // Prints: 5, 0x61fefc
cout << l << ", " << &l << endl; // Prints: 7, 0x61ff00
}
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 6
C++ Program 07.02: Call-by-reference
References vs.
Pointers • Passing values of a=10 & b=15 • Passing Address of a & b
Summary
• In callee; c = 10 & d = 15 • In callee x = Addr(a) & y = Addr(b)
• Swapping the values of c & d • Values at the addresses is swapped
• No change for the values of a & b in caller • Desired changes for the values of a & b in caller
• Swapping the value of c & d instead of a & b • It is correct, but C++ has a better way out
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 8
Program 07.04: Swap in C & C++
Module 07
C Program: Call-by-value – wrong C++ Program: Call-by-reference – right
Intructors: Abir
Das and #include <stdio.h> #include <iostream>
Sourangshu using namespace std;
Bhattacharya
void swap(int, int); // Call-by-value void swap(int&, int&); // Call-by-reference
int main() { int a = 10, b = 15; int main() { int a = 10, b = 15;
Objectives &
Outlines printf("a= %d & b= %d to swap\n",a,b); cout<<"a= "<<a<<" & b= "<<b<<"to swap"<<endl;
swap(a, b); swap(a, b); // Natural call
Reference
variable
printf("a= %d & b= %d on swap\n",a,b); cout<<"a= "<<a<<" & b= "<<b<<"on swap"<<endl;
} }
Call-by-reference void swap(int c, int d) { int t ; void swap(int &x, int &y) { int t ;
Swap in C
t = c; c = d; d = t; t = x; x = y; y = t;
Swap in C++
} }
const Reference
Parameter
Return-by-
reference • a= 10 & b= 15 to swap • a= 10 & b= 15 to swap
• a= 10 & b= 15 on swap // No swap • a= 15 & b= 10 on swap // Correct swap
I/O of a Function
References vs.
Pointers
• Passing values of a=10 & b=15 • Passing values of a = 10 & b = 15
Summary • In callee; c = 10 & d = 15 • In callee: x = 10 & y = 15
• Swapping the values of c & d • Swapping the values of x & y
• No change for the values of a & b in caller • Desired changes for the values of a & b in caller
• Here c & d do not share address with a & b • x & y having same address as a & b respectively
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 9
Program 07.05: Reference Parameter as const
Module 07
• A reference parameter may get changed in the called function
Intructors: Abir
Das and
• Use const to stop reference parameter being changed
Sourangshu const reference – bad const reference – good
Bhattacharya
References vs.
Pointers • Error: Increment of read only Reference ’x’ a = 10 and b = 11
Summary
I/O of a Function
a = 10 and b = 10 a = 10 and b = 11
References vs. a=3 a = 10
Pointers
Summary
• Note how a value is assigned to function call • We expect a to be 3, but it has not changed
• This can change a local variable • It returns reference to local. This is risky
Module 07
I/O of a Function • In addition, we can use the Call-by-address (Call-by-value with pointer) and
References vs.
Pointers
Return-by-address (Return-by-value with pointer) as in C
Summary • But it is neither required nor advised
Module 07
Summary
▷ Never return a local variables by reference
Module 07
Pointers References
Intructors: Abir
Das and • Refers to an address (exposed) • Refers to an address (hidden)
Sourangshu
Bhattacharya • Pointers can point to NULL • References cannot be NULL
Objectives &
Outlines int *p = NULL; // p is not pointing int &j ; // wrong
Reference • Pointers can point to different variables at • For a reference, its referent is fixed
variable
different times
Call-by-reference
Swap in C int a, b, *p; int a, c, &b = a; // Okay
Swap in C++
p = &a; // p points to a ...
const Reference
Parameter ... &b = c // Error
Return-by- p = &b; // p points to b
reference
I/O of a Function
• NULL checking is required • Does not require NULL checking
References vs.
Pointers • Makes code faster
Summary • Allows users to operate on the address • Does not allow users to operate on the address
• diff pointers, increment, etc. • All operations are interpreted for the referent
• Array of pointers can be defined • Array of references not allowed
CS20202: Software Engineering Intructors: Abir Das and Sourangshu Bhattacharya 15
Module Summary
Module 07
Call-by-reference
Swap in C
Swap in C++
const Reference
Parameter
Return-by-
reference
I/O of a Function
References vs.
Pointers
Summary