Loading....
Coupon Accepted Successfully!

 

Pointer To Members

It is possible to obtain the address of a member variable and store it to a pointer. The following programs explain how to obtain address and to access member variables with pointers.
 

13.19 Write a program to initialize and display the contents of the structure using dot (.) and arrow (->) operator.

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

struct c

{

char *name;

};

c b, *bp;

bp->name=“ CPP”;

b.name=“C &”;

cout<<b.name;

cout<<bp->name;

return 0;

}

OUTPUT
C & CPP

Explanation:
 In the above program, structure c is declared. The variable b is the object, and bp is a pointer object of structure c. The elements are initialized and displayed using dot (.) and arrow (->) operators. These are the traditional operators that are commonly used in C. C++ allows two new operators .* and ->* to carry out the same task. These c++ operators are recognized as pointers of member operators.
 

13.20 Write a program to initialize and display the contents of the structure using .* and arrow ->* operators.

#include<iostream.h>

#include<conio.h>

struct data

{

int x;

float y;

};

int main()

{

clrscr();

int data ::*xp=&data::x;

float data::*yp=&data::y;

data d={11,1.14};

cout<<endl<<d.*xp<<endl<<d.*yp;

data *pp;

pp=&d;

cout<<endl<<pp->*xp<<endl<<pp->*yp;

d.*xp=22;

pp->*yp=8.25;

cout<<endl<<d.*xp<<endl<<d.*yp;

cout<<endl<<pp->*xp<<endl<<pp->*yp;

return 0;

}

OUTPUT
11
1.14
11
1.14
22
8.25
22
8.25

Explanation:
 In the above program, struct data is defined, and it has two data members of integer and float type. Consider the following statements:
 

int data ::*xp = &data::x;

float data::*yp = &data::y;


The *
xp and *yp are pointers. The class name followed by scope access operator points to the fact that they are pointers to member variables of structure int x and float y. The rest of the statement initializes the pointers with addresses of the x and y, respectively.
 
The rest of the program uses operators .* and ->* to initialize and access the member elements in the same fashion as the operator dot (.) and arrow (->).
 

13.21 Write a program to declare and initialize an array. Access and display the elements using .* and ->* operators.

#include<iostream.h>

#include<conio.h>

struct data

{

int x;

float y;

};

int main()

{

clrscr();

int data ::*xp=&data::x;

float data::*yp=&data::y;

data darr[]={{12,2.5},

{58,2.4},

{15,5.7} };

for (int j=0;j<=2;j++)

cout<<endl<<darr[j].*xp<<“ ”<<darr[j].*yp;

return 0;

}

OUTPUT
12 2.5
58 2.4
15 5.7

Explanation:
 The above program is similar to the previous one. An array darr[] is initialized. The for loop and the operator .* access the elements, and the elements are displayed on the console.
 

13.22 Write a program to declare variables and pointers as members of class and access them using pointer to members.

#include<iostream.h>

#include<conio.h>

class data

{

public:

int x;

float y;

int *z;

float *k;

int **l;

float **m;

};

void main()

{

clrscr();

int data::*xp=&data::x;

float data ::*yp=&data::y;

int *data::*zp=&data::z;

float *data::*kp=&data::k;

int **data::*lp=&data::l;

float **data::*mp=&data::m;

data ob={51,4.58,&ob.x,&ob.y,&ob.z,&ob.k};

data *pp;

pp=&ob;

cout<<endl<<ob.*xp<<endl<<ob.*yp;

cout<<endl<<*(ob.*zp)<<endl<<*(ob.*kp);

cout<<endl<<**(ob.*lp)<<endl<<**(ob.*mp);

cout<<endl<<pp->*xp<<endl<<pp->*yp;

cout<<endl<<*(pp->*zp)<<endl<<*(pp->*kp);

cout<<endl<<**(pp->*lp)<<endl<<**(pp->*mp);

*(ob.*zp)=11;

**(pp->*mp)=8.24;

cout<<endl<<ob.*xp<<endl<<ob.*yp;

}

OUTPUT
51
4.58
51
4.58
51
4.58
51
4.58
51
4.58
51
4.58
11
8.24

Explanation:
 In the above program, the class and data are declared, and all members are public. The ob is an object of the class data, and it is initialized. The variable pp is a pointer, and it is initialized with the address of the object ob. The remaining program statements use the pointers of member operators and display the contents of the class on the screen.

 

Tip: Pointers of members are not associated with any particular object.

 

 

 

13.23 Write a program that invokes member function using pointer to functions.

#include<iostream.h>

#include<conio.h>

class data

{

public:

void joy1()

{

cout<<endl<<(unsigned)this<<“ in joy1”;

}

void joy2()

{

cout<<endl<<(unsigned)this<<“ in joy2”;

}

void joy3()

{

cout<<endl<<(unsigned)this<<“ in joy3” <<endl;

}

};

void main()

{

clrscr();

data od[4];

void (data ::*m[3])()={&data::joy1,&data::joy2,&data::joy3

};

for (int x=0;x<=3;x++)

{

for (int y=0;y<=2;y++) (od[x].*m[y])();

}

}

OUTPUT
65522 in joy1
65522 in joy2
65522 in joy3
65523 in joy1
65523 in joy2
65523 in joy3
65524 in joy1
65524 in joy2
65524 in joy3
65525 in joy1
65525 in joy2
65525 in joy3

Explanation:
 In the above program, class data have three-member variables joy1joy2, and joy3. The array m[3])() holds the addresses of member functions. The nested for loop and statement within it invokes member functions.
 

13.24 Write a program to declare pointer to member variable and display the contents of the variable.

#include<iostream.h>

#include<conio.h>

class A

{

public :

int x;

int y;

int z;

};

void main()

{

clrscr();

A a;

int *p;

a.x=5;

a.y=10;

a.z=15;

p=&a.x;

cout<<endl<<“x= ”<<*p;

p++;

cout<<endl<<“y= ”<<*p;

p++;

cout<<endl<<“z= ”<<*p;

}

OUTPUT

x= 5

y= 10

z= 15

 
Explanation: In the above program, the class A is declared with three public member variables xy, and z. In function main()a is an object of class A, and p is an integer pointer. The three-member variables are initialized with 510, and 15 using the assignment operation.
 
The address of variable x is assigned to a pointer. The postfix increase operation gives successive memory locations, and values stored at those locations are accessed and displayed. Thus, member variables are stored in successive memory locations. Using the same concept, we can also access private members.




Test Your Skills Now!
Take a Quiz now
Reviewer Name