Unit - 2(part-2)_pointers & references.pptx

sontibhanuprasad 8 views 43 slides Sep 16, 2025
Slide 1
Slide 1 of 43
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43

About This Presentation

Unit - 2(part-2)_pointers & references.pptx


Slide Content

Pointers and References in C++

Overview of Pointers

Pointer Declaration

Pointer Assignment

Pointer Arithmetic

Pointer Arithmetic (Cont.)

Pointer Arithmetic (Cont.)

What is a Reference? 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

Reference - More Explanation 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;

Key Differences

Conclusion Pointers : Use when 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

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

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.

Classes Defined using the 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.

Structures: Defined using the 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

FRIEND FUNCTIONS A friend 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.

How to declare Friend 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.

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

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.

Defining an Inline Function 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 Objects to functions

Passing an Object as 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 );

Returning objects from functions 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.

Output:

Pass by Value & Pass by Reference in C++

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

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.

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; }

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.

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; }

Differences
Tags