×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only β‚Ή1020 / month
Limited Period Offer!

Reference Variable and this Pointer in C++



Last Updated on: 18th Dec 2025 17:42:35 PM

1. Introduction to Reference Variable

A reference variable in C++ is an alternative name (alias) for an already existing variable.
Once a reference is created, it cannot refer to another variable.

 

πŸ‘‰ A reference variable does not create new memory; it shares the same memory location.

 

2. Real-Life Example of Reference Variable 

Example: Nickname of a Person

  • A person may have a real name and a nickname

  • Both names refer to the same person

 

πŸ‘‰ Changing the nickname still refers to the same person
πŸ‘‰ Reference variable works the same way

 

3. Syntax of Reference Variable

data_type &reference_name = existing_variable;

 

4. Example: Basic Reference Variable

A reference acts as another name for the same variable. Any change made using reference affects the original variable.

 

Complete Program

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int &ref = a;

    ref = 20;

    cout << "Value of a: " << a << endl;
    cout << "Value of ref: " << ref << endl;

    return 0;
}

 

Output

Value of a: 20
Value of ref: 20

 

Explanation of Output

  • ref refers to a

  • Updating ref also updates a

  • Both share the same memory

 

5. Memory Representation of Reference Variable 

a   ---->  20
ref ----^

 

6. Reference Variable vs Pointer

Reference Pointer
Cannot be NULL Can be NULL
Cannot change reference Can change address
No * operator needed Needs dereferencing
Safer Risky if not handled properly

 

7. Reference Variable in Function (Call by Reference)

Reference variables allow actual parameters to be modified inside a function.

 

Program

#include <iostream>
using namespace std;

void update(int &x) {
    x = x + 5;
}

int main() {
    int num = 10;
    update(num);

    cout << "Updated value: " << num << endl;
    return 0;
}

 

Output

Updated value: 15

 

Explanation of Output

  • Function receives reference of num

  • Changes affect the original variable

 

Advantages of Reference Variables

  • No extra memory usage

  • Faster than pointers

  • Cleaner syntax

  • Safer than pointers

 

PART 2: this Pointer in C++ 

 

9. Introduction to this Pointer

The this pointer is an implicit pointer available inside all non-static member functions of a class.

 

πŸ‘‰ It holds the address of the current calling object.

 

10. Real-Life Example of  this  Pointer 

 

Example: Teacher Calling Herself

 

If a teacher says:

I will check the homework”

 

Here, “I” refers to the current teacher.

 

πŸ‘‰ In C++,  this  works as “I” for objects.

 

11. Why this Pointer is Needed?

  • To differentiate data members and parameters with same name

  • To return the calling object

  • To access current object’s members

 

12. Basic Example of  this  Pointer

When local variables have the same name as class variables, this helps resolve ambiguity.

 

Complete Program

#include <iostream>
using namespace std;

class Student {
    int id;
public:
    void setId(int id) {
        this->id = id;
    }

    void show() {
        cout << "Student ID: " << id << endl;
    }
};

int main() {
    Student s;
    s.setId(101);
    s.show();

    return 0;
}

 

Output

Student ID: 101

 

Explanation of Output

  •  this->id  refers to class variable

  •  id  refers to function parameter

 

13. Using  this  Pointer to Return Current Object

this pointer can return the current object, enabling method chaining.

 

Example 

#include <iostream>
using namespace std;

class Box {
    int length;
public:
    Box(int length) {
        this->length = length;
    }

    Box& increase() {
        length += 5;
        return *this;
    }

    void show() {
        cout << "Length: " << length << endl;
    }
};

int main() {
    Box b(10);
    b.increase().increase().show();
    return 0;
}

 

Output

Length: 20

 

Explanation of Output

  •  increase()  returns current object

  • Method chaining increases value twice

 

14.  this  Pointer vs Reference Variable 

 

this Pointer Reference Variable
Implicit Explicit
Points to object Alias to variable
Used in class Used anywhere
Holds address Shares memory

 

15. Interview Questions

  1. What is a reference variable?

  2. Can a reference be NULL?

  3. What is this pointer?

  4. Why this cannot be used in static functions?

  5. Difference between pointer and reference?

 

16. Summary

  • Reference variable is an alias to an existing variable

  • Used for efficient parameter passing

  • this pointer represents current object

  • Helps in resolving ambiguity and method chaining

  • Both are key concepts in OOP and modern C++

 

Keep practicing — you're doing amazing!

Happy Coding!    yes


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo