C++ Tutorial
C++ Tutorial
Partha Pratim
Das
Module 07: Programming in C++
Objectives &
Outlines
Reference & Pointer
Reference
variable
I/O of a
Function
Tanwi Mallick
References vs. Srijoni Majumdar
Pointers
Himadri B G S Bhuyan
Summary
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
References vs.
Differences between References and Pointers
Pointers
Summary
Module 07
Return-by-
reference
I/O of a
Function
References vs.
Pointers
Summary
Module 07
Call-by- i ← variable
reference
Swap in C 15 ← memory content
Swap in C++
const Reference
Parameter
200 ← address
Return-by- j ← alias or reference
reference
I/O of a
Function
References vs.
Pointers
Summary
Module 07
#include <iostream>
Partha Pratim using namespace std;
Das
int main() {
int a = 10, &b = a; // b is reference of a
Objectives &
Outlines // a and b have the same memory
cout << "a = " << a << ", b = " << b << endl;
Reference
cout << "&a = " << &a << ", &b = " << &b << endl;
variable
I/O of a a = 10, b = 10
Function &a = 002BF944, &b = 002BF944
a = 11, b = 11
References vs. a = 12, b = 12
Pointers
Summary • a and b have the same memory location and hence the same value
• Changing one changes the other and vice-versa
Partha Pratim
Das int& i; no variable to refer to – must be initialized int& i = j;
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
Return-by-
reference
I/O of a
Function
References vs.
Pointers
Summary
Module 07
Return-by-
reference
I/O of a
Function
References vs.
Pointers
Summary
Return-by- const int& b = // const needed. Why? const int& b = // const optional
reference Function_Return_By_Val(a); Function_Return_By_Ref(a);
I/O of a cout <<"b = "<<b<<" &b = "<<&b<<endl; cout <<"b = "<<b<<" &b = "<<&b<<endl;
Function return 0; return 0;
} }
References vs.
Pointers a = 10 &a = 00DCFD18 a = 10 &a = 00A7F8FC
x = 10 &x = 00DCFD18 x = 10 &x = 00A7F8FC
Summary b = 10 &b = 00DCFD00 b = 10 &b = 00A7F8FC
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
Call-by-
Return Value Output Return-by-value
reference Return-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 Call
Partha Pratim
Pass parameters of built-in types by value
Das Recall: Array parameters are passed by reference in C
Objectives & Pass parameters of user-defined types by reference
Outlines
Make a reference parameter const if it is not used for
Reference output
variable
Call-by- Return
reference
Swap in C
Return built-in types by value
Swap in C++ Return user-defined types by reference
const Reference
Parameter Return value is not copied back
Return-by- May be faster than returning a value
reference
Beware: Calling function can change returned object
I/O of a
Function Never return a local variables by reference
References vs.
Pointers
Summary
Summary
Module 07
Return-by-
reference
I/O of a
Function
References vs.
Pointers
Summary
Module 07
Partha Pratim
Das Name Mail Mobile
Objectives &
Partha Pratim Das, Instructor [email protected] 9830030880
Outlines Tanwi Mallick, TA [email protected] 9674277774
Reference
Srijoni Majumdar, TA [email protected] 9674474267
variable Himadri B G S Bhuyan, TA [email protected] 9438911655
Call-by-
reference
Swap in C
Swap in C++
const Reference
Parameter
Return-by-
reference
I/O of a
Function
References vs.
Pointers
Summary