Pointer To Object In C++
A pointer to a C++ class is done exactly the same
way as a pointer to a structure and to access members of a pointer to a class
you use the member access operator -> operator, just as you
do with pointers to structures. Also as with all pointers, you must initialize
the pointer before using it.
Fig:Pointer To Object In C++ |
Syntax:
class name *object name;
Example:
BCA
*p;
Program:
#include<iostream.h>
#include<conio.h>
class
BCA
{
char
name[30];
int
phone no;
public:
void
inputdata();
void
outputdata();
};
void
BCA::inputdata()
{
cout<<”input
name “<<endl;
cin>>name;
cout<<”input
phone no”<<endl;
cin>>phone
no;
}
void
BCA::outputdata()
{
cout<<name<<endl;
cout<<phone
no<<endl;
}
void
main()
{
BCA
*p;
p->inputdata();
p->outputdata();
getch();
}
Output:
Input
name Pramod
Input
phone 1234567889
No comments:
Post a Comment