0% found this document useful (0 votes)
10 views6 pages

Group H

Uploaded by

brianndeti83
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)
10 views6 pages

Group H

Uploaded by

brianndeti83
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/ 6

1.

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>

Void increment ( int& num ) {

num++;

int main() {

int x = 5;

std: : cout << “Before increment: x =” << x << std: :endl;

increment(x);

std: : cout<< “After increment: x =”<< x << std: : endl;

return 0;
}

Output:

Before x= 5

After x= 6

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>
#include<utility>
Void processValue(int&& num) {
Std::cout << “Processing rvalue:” << num << std::endl;
}
int main(){
int x = 5;
processValue(10);
processValue(std::move(x));
return 0;
}
Output:
Processing R-value: 10
Processing r-value:5

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>
Void printValue(const int& num){
Std::cout<<”The value is:”<<num << std::endl;
}
int main(){
int x = 5;
const int& ref = x;
x = 10;
printValue(ref);
return 0;
}
Output:
The value is:10

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>
Void modifyParameter(int& num){
Num *=2;
}
Int main(){
int value = 5;
std::cout<< “Before modification: value”<<value<< std::endl;
modifyParameter(value);
std::cout << “After modification: value =”<< value << std::endl;
return 0;
}
Output:
Before modification: =10
After modification: = 5

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>
Void processVector(const std::vector<int>& vec){
Std::cout<< “Processing vector with size:”<<vec.size() <<std::endl;
}
int main(){
std::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>
#include <vector>
int main(){
std::cout<< “Original numbers:”;
for(const int& num : numbers){
std::cout << num << “”;
}
Std::cout<< std::endl;
Std::cout << “Modified numbers:’;
for(int& num : numbers){
num* = 2;
std::cout << num << std::endl;
return 0;
}
Output:
Original numbers = 1,2,3,4,5
Modified numbers= 2,4,6,8,10

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>
Void modifyData(int& data){
Data *= 2;
}
int main(){
int sharedData = 10;
std::cout<< “Before modification: sharedData = ”<<sharedData << std::endl;
modifyData(sharedData);
std::cout << “After modification: sharedData = ” << sharedData << std::endl;
return 0;
}
Output:
Before modification: =10
After modification:= 20

You might also like