Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism

this Pointer

C++ Object Oriented Programming

The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A and class A has a nonstatic member function f().

this Pointer

• The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A and class A has a nonstatic member function f(). If you call the function x.f(), the keyword this in the body of f() stores the address of x. You cannot declare this pointer or make assignments to it.

• A static member function does not have a this pointer.

• The this pointer is passed as a hidden parameter to the member function call and it is available in the function definition as a local variable. Below is an example in which this pointer is used to refer the num variable.

C++ Program

#include<iostream>

using namespace std;

class test

{

private:

   int num;

public:

void get_val(int num)

{

    //this pointer retreives the value of obj.num

    //this pointer is hidden by automatic variable num

    this‒>num=num;

}

void print_val()

{

    cout<<"\n The value is "<<num;

}

};

int main()

{

test obj;

int num;

cout<<"\n Enter Some Value ";

cin>>num;

obj.get_val(num);

obj.print_val();

return 0;

}

Output

Enter Some Value 10

The value is 10

The this pointer points to the object for which the member function is called. Hence from above code obj.get_val(num) can be interpreted as get_val(&obj,num); That means this pointer is passed as a hidden argument to the called function by some automatic variable like num.

 

Review Question

1.What is the use of this pointer?


Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism : Tag: Oops, Computer Programming : C++ Object Oriented Programming - this Pointer


Object Oriented Programming: Chapter 4: Pointers and Runtime Polymorphism



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation