What is aReference?
A reference is an alias for an already existing variable.
Declared using the ampersand (&) operator (e.g., int& ref;)
Must be initialized when declared
Cannot be nullptr
Cannot be reassigned to another variable
Dereferenced implicitly
Arithmetic operations not supported
10.
Reference - MoreExplanation
References provide safer, simpler access to variables.
Commonly used in function parameters and return types
Ideal for avoiding copying of large data
Often used in operator overloading
int b = 20;
int& ref = b;
Conclusion
Pointers: Usewhen dynamic memory allocation or pointer arithmetic
is needed.
References: Use when you need a simple alias for passing parameters
or returning values without copying.
Prefer references over pointers when null values and reassignment
are not needed.
Function Objects (Functors)
These are objects of a class that overload the
function call operator (operator()), allowing them
to be called like functions.
Definition: A class is created with a public
operator() member function.
Usage: Instances of this class can then be "called"
with arguments, just like regular functions.
Benefits: Functors can maintain state (data
members) between calls, unlike plain
functions. They are widely used in the C++
Standard Library, particularly with algorithms and
containers for custom operations (e.g., custom
sorting criteria).
Classes & Structures
In C++, both classes and structures are user-defined data
types that allow grouping data members and member
functions into a single unit.
They serve as blueprints for creating objects, which are
instances of these types.
19.
Classes
Defined usingthe class Keyword.
By default, members (data and functions) within a
class are private.
This means they can only be accessed from within
the class itself or by its friend functions/classes.
Classes are fundamental to Object-Oriented
Programming (OOP) in C++ and are commonly used
to encapsulate data and behavior, providing
controlled access through public interfaces.
Support advanced OOP concepts like inheritance
and polymorphism.
21.
Structures:
Defined usingthe struct keyword.
By default, members within a structure are
public. This means they can be accessed directly
from outside the structure.
Historically, structures in C were primarily used to
group related data. In C++, structures have been
extended to also include member functions,
constructors, destructors, and support
inheritance, making them almost identical to
classes in functionality.
Often used for simple data structures where
public access to members is acceptable or when
interfacing with C code that uses structures.
FRIEND FUNCTIONS
Afriend function is a function which is declared
within a class and is defined outside the class.
It does not require any scope resolution operator
for defining.
It can access private members of a class. It is
declared by using keyword “friend”.
The scope resolution operator :: allows us to
define a member function of a class outside
the class definition.
25.
How to declareFriend Function :
Friend function declaration should be inside the
class using the Friend key word.
Syntax:
friend ret_type
func_name(arguments);
Definition of friend function is specified outside
the class.
Function definition must not use keyword friend.
26.
Friend function characterstics:
It is not in scope of class.
It cannot be called using object of that class.
It can be invoked like a normal function.
It has objects as arguments.
Friend Functions are majorly used in operator
overlading.
Friendship is not inherited by derived classes.
Inline Functions:
C++inline function is powerful concept that is
commonly used with classes.
If a function is inline, the compiler places a copy
of the code of that function at each point where
the function is called at compile time.
Any change to an inline function could require all
clients of the function to be recompiled because
compiler would need to replace all the code once
again otherwise it will continue with old
functionality.
30.
Defining an InlineFunction
To define an inline function, place the keyword
inline before the function name and define the
function before any calls are made to the
function.
The compiler can ignore the inline qualifier in
case defined function is more than a line.
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
Passing an Objectas argument
To pass an object as an argument we write the
object name as the argument while calling the
function the same way we do it for other
variables.
Syntax:
function_name(object_name);
35.
Returning objects fromfunctions
In C++, a function can return an object of a class.
This helps send complete data (like multiple
values) from the function back to the main
program.
The returned object can be used to call its
methods or access its data.
This is useful in object-oriented programming for
better data handling.
In C++,data can be sent to functions when they are called in
order to perform operations. This data is called parameters or
arguments and there are various parameter passing methods
available in C++.
Before you see the techniques, first understand the difference
between the following terms:
Formal Parameters: Variables used in parameter list of a function
as placeholders. Also called only parameters.
Actual Parameters: The expressions or values passed in during a
function call. Also called arguments.
There are 3 different methods using which we can pass
parameters to a function in C++. These are:
Table of Content
Pass by Value
Pass by Reference
Pass by Pointer
39.
Pass by Value
In pass by value method, a variable's value is
copied and then passed to the function.
As the result, any changes to the parameter inside
the function will not affect the variable's original
value in the caller.
This method is simple, easy to understand and
implement but it is not preferred for large size of
data structures at it involves copying the value.
40.
Pass By Value
#include<iostream>
using namespace std;
int change(int a) {
a = 22;
return a;
}
int main() {
int x = 5;
cout << x<<endl;
cout << change(x)<<endl;
cout << x;
return 0;
}
41.
Pass By Reference
In pass-by-reference method, instead of passing
the value of the argument, we pass the reference
of an argument to the function.
This allows the function to change the value of
the original argument.
This is useful when you have to pass large size
data.
It is Suitable when you want to modify the original
data or avoid copying large data.
42.
Pass By Reference
#include<iostream>
using namespace std;
int change(int &a) {
a = 22;
return a;
}
int main() {
int x = 5;
cout << x<<endl;
cout << change(x)<<endl;
cout << x;
return 0;
}