C++ Pointer and This Pointer

Table of Contents

C++ Pointer and This Pointer

Pointer

  • Pointer is a memory variable that stores a memory address.
  • Pointer can have any name that is legal to other variable.
  • It can be declared like normal variable it is always denoted by star(*)operator
  • C++ variables are used to hold data during program execution.
  • Every variable can occupy some memory location to hold the data.
  • Memory is arranged in a sequence of byte.
  • The number specification to each byte is called memory address.
  • The sequence starts from 0 onwords.
  • It is possible to access and display the address of memory location using the pointer variable.

 

Feature of pointer
  • Pointer saves memory space.
  • Execution time with pointer is faster.
  • The memory is accessed efficiently with the pointer.
  • Pointers are used with data structure and they are useful for representing two dimensional array.
  • A pointer declared to a base class can access the object of derived class.

 

Pointer Declaration

Pointer can be declared as follows:

Syntax:

Data type * pointer variable name.

Example:

int *a;

int *b;

float*c;

  • It inform to the compiler that hold the address of any variable.
  • The in-direction (*) operator is also called de-reference operator.
  • Whereas a pointer indirectly access to their own values.
  • The (&) is the address operator represented the address of a variable.
  • The address of any variable is a whole number.

 

Example:

Display the value and address of the variable using pointer.

#include<iostream.h>

#include<conio.h>

void main()

{

int *p;

int x=10;

p=&x;

cout<<x<<&x;

cout<<*p<<&p;

}

 

Void pointer

  • Pointer can be also be declared as void type.
  • Void pointer cannot be reference without explicit type conversion.
  • A void pointer can pointer any type of variable with proper type casting.

Example:

#include<iostream.h>

#include<conio.h>

int p;

float d;

char c;

void *pt=&p;

void main()

{

*(int*)pt=12;

cout<<p;

pt=&d;

*(float*)pt=5.4;

cout<<d;

pt=&c;

*(char*)pt=’x’;

cout<<c;

}

 

Pointer to class

  • The pointer is a variable that hold the address of another data variable.
  • The variable may be of int, float and double in the same way we can define pointer to class.
  • This type of pointer is called class pointers.

Syntax:

Class name *pointer variable name

Example:

Student *str;

 

Example:

#include<iostream.h>

#include<conio.h>

Class man

{

public:

char name[20];

int age;

void getdata(char s[10], int a)

{

name = s;

age = a;

}

Void putdata()

{

cout<<name<<age;

            }

};

Void main()

{

man *ptr, m;

ptr = &m;

ptr->getdata(“xyz”,m);

ptr->putdata();

}

 

Pointer to object

  • Like variable object can also have an address.
  • The pointer can point to a specified object.
  • Using the pointer it is possible to access different type of classes.

Syntax:

Class name *ptr variable name;

Example:

Student *str;

 

Example:

#include<iostream.h>

#include<conio.h>

class bill

{

intqty;

float price;

float amount;

public:

Void getdata (int a, float b, float c)

{

qty = a;

price = b;

amount = c;

}

Void show()

{

cout<<”QUANTITY:”<<qty<<”\n”;

cout<<”PRICE”<<price<<”\n”;

cout<<”AMOUNT”<<amount<<”\n”;

            }

};

Int main()

{

            clrscr();

            bill s;

            bill *ptr;

            ptr=&s;

            ptr->getdata(45,10.25,45*10.25);

(*ptr).show();

return 0;

}

 

This pointer

  • The objects are used to invoke non static member function of the class.
  • The pointer this is transferred as an unseen parameters to call non static member function.
  • The keyword “this” is a local variable always present in the body of non-static member function.
  • The keyword this does not need to be declared.
  • The pointer is rarely referred explicitly in a member function.
  • It is used implicitly with in the function for member reference.
  • Using this pointer it is possible to access the individual member variables of the object.

Example:

Program to use this pointer and return pointer reference (minimum or smallest value of two numbers)

#include<iostream.h>

#include<conio.h>

Class number

{

Public: intnum;

void input()

{

cin>>num;

}

void show()

{

cout<<num;

}

number min(number t)

{

            If(t.num<num)

                        Return t;

            else

                        return *this;

}

};

Void main()

{

            number n, n1, n2;

            n1.input();

            n2.input();

            n=n1.min(n2);

            n.show();

}

 

Pointer to derived classes and base class

  • It is possible to declare a pointer, which points to the base class as well as the derived class.
  • One pointer can point different classes.

Example:

Write a program to declare a pointer to the base class and access the member variable of base and derived class.

#include<iostream.h>

#include<conio.h>

class A

{

public:

int b;

void display()

{

cout<<<”b=”<<b<<”\n”;

            }

};

Class B:public A

{

            Public:

            Int d;

            Void display()

            {

                        cout<<”b=”<<”\n”<<”d=”<<d<<”\n”;

            }

};

void main()

{

            clrscr();

            A *CP;

            A base;

            CP=&base;

            CP->b=100;

            //CP->d=200; Not accessible

            cout<<”\n CP points to the base object\n”;

            CP->display();

            B b;

            cout<<”\n CP points to the derived class \n”;

            CP=&b;

            CP->b=150;

            //CP->d=300; Not Accessible

            CP->display();

return 0;

}

 

Pointer to the derived class

#include<iostream.h>

#include<conio.h>

class A

{

public:

int b;

void display()

{

cout<<”b=”<<b<<”\n”;

            }

};

Class B : public A

{

            Public:

            int d;

            void display()

            {

cout<<”\t b=”<<”\n”<<”\t d=”<<d<<”\n”;

            }

};

Void main()

{

            clrscr();

            B*CP;

            B b;

            CP=&b;

            CP->b=100;

            CP->d=350;

            cout<<”\n CP points to the derived object \n”;

            cout<<display();

return 0;

}

You May Like to Browers More