Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 13

References (alias) in C++, Parameter Passing

CSC 404
Complex Networks Research
Object Oriented Programming
Group (CNeRG),
Department of Computer Science
Definition of alias:
and: Engineering
otherwise called
otherwise known as —
used to indicate an additional name that a
person (such as a criminal) sometimes uses Subrata Nandi
John Smith alias Richard Jones was identified
Dept. of Computer Science & Engineering
as the suspect Team Members: NIT Durgapur
National
INDIAN InstituteOF
INSTITUTE of TECHNOLOGY
Technology Sandip Chakraborty, Niloy Ganguly, Basabdatta Palit,
KHARAGPUR
Durgapur Nibir Pal, Somesh Khandelia
Indian Institute of Technology Kharagpur
Outline
• Basics of Pointers
• Pointer Variables – use of & and * operator
• Multiple indirections
• Legal & Illegal Operations on Pointers

• Constants in C++
• Pointers vs Constants
• Arrays vs Pointers

• Memory Layout of C++ program

• Dynamic memory allocation in C++ - use of new and delete operators


• Dynamic allocation of 1-D arrays
• Dynamic allocation of 2-D arrays

• Reference variables
• Use of reference variables
• Reference vs constants

• Function Pointers

• Pointer to structures – self-referential structures


• Pointers and Class (objects, member methods, member data)
• Institute
Indian this pointer
of Technology Kharagpur
Outline
• Basics of Pointers
• Pointer Variables – use of & and * operator
• Multiple indirections
• Legal & Illegal Operations on Pointers

• Constants in C++
• Pointers vs Constants
• Arrays vs Pointers

• Memory Layout of C++ program

• Dynamic memory allocation in C++ - use of new and delete operators


• Dynamic allocation of 1-D arrays
• Dynamic allocation of 2-D arrays

• Reference variables
• Use of reference variables
• Reference vs constants

• Function pointers

• Pointer to structures – self-referential structures


• Pointers and Class (objects, member methods, member data)
• Institute
Indian this pointer
of Technology Kharagpur
Pseudonym

Pseudonyms include stage-names(actors) and user names, ring names, pen-names(writers), nicknames,
aliases, superhero/villain identities(actors) and code names(hackers), gamer identifications

A pseudonym or alias is a fictitious name that a person or group assumes for a particular purpose, which
differs from their original or true name (orthonym)

Objective to remain anonymous

Pseudonyms are "part-time" names, used only in certain contexts


• to provide a more clear-cut separation between one's private and professional lives,
• to showcase or enhance a particular persona,
• or to hide an individual's real identity

In programming language a pseudonym is created by using the feature called ‘reference’

Indian Institute of Technology Kharagpur


References in C++
• A reference is a feature that allows a programmer to provide an
additional name to an existing data item (variable/constant)

• If you think of a variable name as a label attached to the x

2
variable’s location in memory then you may consider reference
to be another label attached to that memory location

• One may access the location either by the original name or by


it’s reference

int x=2; // original name label ‘x’

Indian Institute of Technology Kharagpur


References in C++
• A reference is a feature that allows a programmer to provide an
additional name to an existing data item (variable/constant)
y
• If you think of a variable name as a label attached to the
x
variable’s location in memory then you may consider reference

2
to be another label attached to that memory location

• One may access the location either by the original name or by


it’s reference

int x=2; // original name label ‘x’

int &y=x; // additional name (alias) label ‘y’

Indian Institute of Technology Kharagpur


References in C++
• A reference is a feature that allows a programmer to provide an
additional name to an existing data item (variable/constant)
y
• If you think of a variable name as a label attached to the
x
variable’s location in memory then you may consider reference

2
to be another label attached to that memory location
z
• One may access the location either by the original name or by
it’s reference

int x=2; // original name label ‘x’

int &y=x; // additional name (alias) label ‘y’

int &z=y; // additional name (alias) label ‘y’

cout<<x<<y<<z<<endl;

Indian Institute of Technology Kharagpur


References in C++
• A reference is a feature that allows a programmer to provide an
additional name to an existing data item (variable/constant)
y
• If you think of a variable name as a label attached to the
x
variable’s location in memory then you may consider reference

3
to be another label attached to that memory location
z
• One may access the location either by the original name or by
it’s reference

int x=2; // original name label ‘x’

int &y=x; // additional name (alias) label ‘y’

int &z=y; // additional name (alias) label ‘y’

cout<<x<<y<<z<<endl;

y++;
cout<<x<<y<<z<<endl;

Indian Institute of Technology Kharagpur


References in C++
• One can create multiple references of an existing variable

• One can create reference of a particular type y


x
• A reference must be initialized when declared


Once a reference name is assigned it cannot be used by others

Creating reference does not create additional space in RAM


z
3
• NULL references are not possible; ref must be connected to a legitimate piece of storage

• Dynamic allocation to a reference var is NOT POSSIBLE

• Creating an array of references is NOT POSSIBLE

int &a[10];

• int &a[4]={2,3,4,5}; // error: array of references not possible

• int &a=3; // error: cannot bind non-const lvalue reference of type ‘int&’ to an rvalue of type ‘int’

Indian Institute of Technology Kharagpur


Indian Institute of Technology Kharagpur
References in C++

Indian Institute of Technology Kharagpur


References in C++

Indian Institute of Technology Kharagpur


Indian Institute of Technology Kharagpur

You might also like