This Pointer

In C++ programming, this is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++.

  • It can be used to pass current object as a parameter to another method.
  • It can be used to refer current class instance variable.
  • It can be used to declare indexers.

 

 

Applications of this pointer

1. Return Object

One of the important applications of using this pointer is to return the object it points. For example, the statement

return *this;

inside a member function will return the object that calls the function.

2. Distinguish Data Members

Another application of this pointer is distinguishing data members from local variables of member functions if they have same name. For example,

 #include <iostream>
#include <conio.h>
using namespace std;

class sample
{
    int a,b;
    public:
        void input(int a,int b)
        {
            this->a=a+b;
            this->b=a-b;
        }
        void output()
        {
            cout<<"a = "<<a<<endl<<"b = "<<b;
        }
};

int main()
{
    sample x;
    x.input(7,12);
    x.output();
    getch();
    return 0;
}

A class sample is created in the program with data members a and b and member functions input() and output(). input() function receives two integer parameters a and b which are of same name as data member of class sample. To distinguish the local variable of input() data member of class, this pointer is used. When input() is called, the data of object inside it is represented as this->a and this->b while the local variable of function is represented simply as a and b.

Output



Example

Program to display the record of student with highest percentage.

#include<iostream>
#include<conio.h>
using namespace std;

class student
{
    char name[100];
    int age,roll;
    float percent;
    public:
        void getdata()
        {
            cout<<"Enter data"<<endl;
            cout<<"Name:";
            cin>>name;
            cout<<"Age:";
            cin>>age;
            cout<<"Roll:";
            cin>>roll;
            cout<<"Percent:";
            cin>>percent;
            cout<<endl;
        }
        student & max(student &s1,student &s2)
        {
            if(percent>s1.percent && percent>s2.percent)
                return *this;
            else if(s1.percent>percent && s1.percent>s2.percent)
                return s1;
            else if(s2.percent>percent && s2.percent>s1.percent)
                return s2;
        }
        void display()
        {
            cout<<"Name:"<<name<<endl;
            cout<<"Age:"<<age<<endl;
            cout<<"Roll:"<<roll<<endl;
            cout<<"Percent:"<<percent;            
        }
};

int main()
{
    student s,s1,s2,s3;
    s1.getdata();
    s2.getdata();
    s3.getdata();
    s=s3.max(s1,s2);
    cout<<"Student with highest percentage"<<endl;
    s.display();
    getch();
    return 0;
}

 

This program is used to compare the percentage of three students and display the highest among them. The concept of this pointer is used in this program. A class student is created with data members name, roll, age and percent and member functons getdata(), max() and display(). Data for each student is entered by calling getdata() function. Then, max() function is called by object s3 and s2 and s1 are passed as parameter in the function. The value of percent is compared and the object with highest percent is returned. If the object calling the method has the highest percentage then, it is returned by using this pointer as,

 return *this;

 

Output


 

 

No comments:

Post a Comment