0% found this document useful (0 votes)
2 views

c++References

The document discusses C++ references, detailing their characteristics, types (lvalue, rvalue, and const references), and applications such as modifying passed parameters and avoiding object copying. It explains how references provide alternative names for existing variables, allowing direct access and manipulation of original objects. Several code examples illustrate the use of references in different scenarios.

Uploaded by

kalasingayvonne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c++References

The document discusses C++ references, detailing their characteristics, types (lvalue, rvalue, and const references), and applications such as modifying passed parameters and avoiding object copying. It explains how references provide alternative names for existing variables, allowing direct access and manipulation of original objects. Several code examples illustrate the use of references in different scenarios.

Uploaded by

kalasingayvonne
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

GROUP H

OLIVIA NJURE - T021/300704/2023


JOHN OTIENO - T021/300735/2023
EMILIO KAMAU - t021/300630/2023
NANCY NYAKUNDI – DBTCO1/4710/2022
CHRIS MOGISO – T021/300546/2023
BRIAN NDETI – T021/300655/2023
References
References are a powerful feature that allows us to create aliases or alternate names for existing
variables.
When we declare a references, we are essentially creating another name for existing object, this
reference then acts as a proxy for original object allowing us to access and manipulate its values
using different name.

Characteristics of c++ references


a) Syntax - References are declared using the ampersand symbol (&) after the variable type.
b) Initialization- references must be initialized when declared, once initialized, they cannot
be made to refer to a different object. This means that a reference is bound to object its
initialized with for its entire lifetime.
c) Aliasing- references are provided an alternative name for an existing object. This means
that any modification made to the reference will directly affect the original object.
d) Readability and simplicity- references offer a way to write cleaner and more readable
code by providing meaningful names to variables.

Types of references
1.Lvalue
Refers to a named variable. It’s declared using the & operator. It behaves syntactically .
Example:

#include <iostream>

using namespace std;

void swap(int& x, int& y)


{

Int temp = x;

X=y;

Y=temp;

Int main()

Int a{ 10 },b{ 20 };

cout << “a=” << b << endl;

swap(a, b);

cout << “a= ” << a

<< “ b= ” << b << endl;

Return 0;

OUTPUT

a = 10, b = 20

a = 20, b = 10

2. Rvalue
Refers to a temporary object. Its declared using the && operator. Are commonly used in move
semantics and perfect forwarding They can be universal reference, which can bind both R-values
and l-value.
Example:
#include<iostream>
using namespace std;
//Declaring rvalue references to the
//rvalue passed as the parameter
Void printReferenceValue(int&& x)
{
Cout << x << endl;
}

//Driver Code
Int main()
{
//Given value a
Int a{ 10 };

//Works fine as the function is


//called with rvalue
printReferenceValue(100);
return0;
}

OUTPUT
100

3.const references
Are references that provide read only access to the referred object.
Are often used as a function parameter to avoid making unnecessary copies and to express the
intention of not modifying the past object.
Example:
#include<iostream>
using namespace std;
Void my_function(const int& value) {
cout<< “The value is ” << value << endl;
}
Int main(){
Int x=42;
my_function(x);

return 0;
}

OUTPUT
42

Applications of references
1.Modifying passed parameters:
References are often used in function parameter passing to avoid unnecessary object copying.
When a function receives a reference to an object as a parameter, it can directly access and
modify the original object rather than creating a copy.
Example:
#include < iostream>
using namespace std;

void increment(const int& x) {


int& y= const_cast<int&>(x);
==y;
}

int main() {
int a = 5;
increment(a);
cout<< a <<endl;
}

OUTPUT
6

2.Avoiding object copying:


By passing objects by reference, we can avoid the overhead of creating a copy of the object
resulting in improved performance.
Example:
#include <iostream>
#include <vector>

using namespace std;


void processVector(condt vector<int>&vec){
cout<< “Processing vector with size:”
<<vec.size()<<endl;
}
int main(){
vector<int> numbers = {1,2,3,4,5};
processVector(numbers);
return 0;
}

OUTPUT
Processing vector with size: 5

3.Range-based loops:
References are often used in range-based loops to directly access and modify elements of a
container.
Example:
#include <iostream>
using namespace std;

int main(){

int numArray[] = {1,2,3,4,5};

for (int n : numArray){


cout<<n<<””;
}
return 0;
}
OUTPUT
12345

4.Data sharing and aliasing:


References can be used to create alternative names for existing variables, allowing multiple
names to refer to the same object. This can be useful when different parts of the code need to
access or modify the same data.
Example:
#include<iostream>
using namespace std;

void modifyData(int&data){
data *=2;
}
int main(){
int sharedData=10;
cout<< “Before modification:
sharedData =”<< sharedData<<endl;
modifyData(sharedData);
cout<< “After modification:sharedData = ””<<sharedData<<endl;
return 0;
}

OUTPUT
Before Modification =10
After Modification = 20

You might also like