In this article this pointer we give the information about this Keyword is used to represent an object that invokes a member function. It is points to the object for which this function was called.
this pointer:
- It is used to represent an object that invokes a member function.
- It is points to the object for which this function was called.
- Example, the function call P. main() will set the pointer this to the address of object P. The starting address is the same as the address of the first variable in the class structure.
- It is automatically passed to a member function when it is called. The pointer this acts as an implicit argument to all member functions.
- Whenever the name of instance variable and local variable both are same and if we initialize instance variable with the help of local variable then our compiler totally confused that which one is local variable and which one is instance variable .
- To avoid this problem we should use this
- Use of this pointer is holds the address of current object, in simple words you can say that this pointer points to the current object of the class
Program based on this pointer:
// Source Code
#include<iostream.h>
#include<conio.h>
class P
{
int x,y;
public:
P(int x, int y)
{
this -> x=x, this->y=y;
}
void display()
{
cout<<”\n Value of X: ”<<x;
cout<<”\n Value of Y: ”<<y;
}
};
void main()
{
P obj(10,20);
Clrscr();
p.display();
getch();
}
OUTPUT:
Value of X: 10
Value of Y: 20
Pointer to derived classes:-
Pointers can be used to point to base class objects and derived class objects. The pointers of the objects of the base class correspond to the objects of the derived class. Single pointer variables can be made to point to different classes of objects. If B is the base and D is the derivative class, then the pointer declared as a pointer to B can also be a pointer for D.
Example:
B *cptr; // pointer to of class
B b; //Base object
D d; // Derived object
cptr=&b; //cptr store address of object b of base class
cptr=&d; //cptr store address of object d of derived class
Class D public members generated using cptr cannot be accessed. Using the base class pointer to the object cptr, only members inherited from B can access it, not originally members of D. If the member of D has the same name as any member of B, then the cptr reference of that member will always reach the base class member. While C ++ allows the base pointer to point to any object derived from that base, the pointer cannot be used to directly access all members of the derivative class.
Example:
#include<iostream.h>
#include<conio.h>
class base
{
int num;
public:
void getnum()
{
cout<<“\nEnter a value for Number: “;
cin>>num;
}
void putnum()
{
cout<<“\nThe Value for Number: “<<num;
}
};
class derived : public base
{
int num;
public:
void getnum()
{
cout<<“\nEnter a value for Number : “;
cin>>num;
}
void putnum()
{
cout<<“\nThe Value for Number: “<<num;
}
};
void main()
{
base *bptr,b;
derived d;
clrscr();
cout<<“\nPointing to the base class “;
bptr=&b;
bptr->getnum();
bptr->putnum();
cout<<“\nPointing to the derived class “;
bptr=&d;
bptr->getnum();
bptr->putnum();
getch();
}
OUTPUT:
Pointing to the base class
Enter a value for Number: 100
The Value for Number: 100
Pointing to the derived class: 200
The Value for Number: 200
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining