Pointer to Class, Object
· We know that a pointer is a variable that holds the address of another data variable.
· The variable may be of any data type , that is int , float m or double .
· In the same way , we can also define pointer to class . Here , the starting address of the member
variable can be accessed , thus such pointer are called class pointers
Example
class book
char name[30];
int pages;
};
(a) class book *ptr;
(or)
struct book *ptr;
in the above example , *ptr is a pointer to a class book , both statements (a) and (b) are alid the syntax
for using pointes with members is as given below
1) ptr --> name 2) ptr --> author 3) ptr --> pages
by executing thses three statements , the starting address of each member can be estimated.
Pointer to Object
Similar to variable , objects also have on address . A pointe can point to a specified object
Write a program to delcare an object and pointer and class
#include<iostream.h>
#include<conio.h>
class man
public:
char name[20];
int age;
};
void main()
clrscr();
man m={"krish",34};
man *ptr;
ptr=&m;
cout<<"\n name:"<<m.name;
cout<<"\n age is:"<<m.age;
cout<<"\n name is:"<<ptr->name;
cout<<"\n age is:"<<ptr->age;
getch();