Computer Science (083) Pointers to Classes
Pointers to classes (Example)
#include <iostream.h>
#include <conio.h>
class A // ********** BASE CLASS ************
{ int x2,x3;
public :
int x1;
void initialize(int a,int b,int c)
{x1=a;x2=b;x3=c;
cout <<"Data members initialized ....\n";
getch();
}
void display()
{cout<<x1<<'\t'<<x2<<'\t'<<x3<<endl;}
};
void main()
{
clrscr();
A obj;
obj.initialize(10,20,30);
A * ptr=&obj;
ptr->display();
cout<<"\nptr->x1 = "<<ptr->x1;
cout<<"\n(*ptr).x1 = "<<(*ptr).x1 ; // NOTE x1 is declared under public section
//otherwise will not be accessible
getch();
(*ptr).display();
getch();
}
Summary of pointer and operators (*, &, ., ->)
expression can be read as
*x pointed by x
&x address of x
x.y member y of object x
x->y member y of object pointed by x
(*x).y member y of object pointed by x (equivalent to the previous one)
NOTE : What has been discussed for classes is also valid for pointers to
structures.
By Nita Arora