pointers, pointer of class, pointer to class, pointer to object

Pointer is a special type of variable present in 'C++' language, which stores memory address of a variable as its value. Pointer must occupy two bytes in computer memory. Pointer stores unsigned integer value only. Updation (increment/ decrement) of pointer is according to its datatype.
सी प्लस प्लस लैंग्वेज में पॉइंटर एक विशेष प्रकार का वेरिएबल होता है जो उसी प्रकार के किसी दुसरे वेरिएबल का मेमोरी एड्रेस (वैल्यू के रूप में) रखता है। पॉइंटर सदैव मेमोरी में दो बाइट ग्रहण करते है। पॉइंटर केवल अनसाइंड इन्टिजर वैल्यू स्टोर करते है। पॉइंटर का अपडेशन उसके डाटा टाइप के अनुसार होता है।      

Declaration of pointer:- 

Syntax:- 
data _ type * pointer _ name;

Example:- 
char *p; 
int *link;
float *ptr; 
double *a;

Initialization of pointer:- 

Providing initial value to a pointer variable at the time of its declaration is called initialization of pointer.
पॉइंटर को प्रारंभिक मान प्रदान करना इनिशियलाइजेशन कहलाता है। 
Syntax:- 
datatype *pointer _ name= &variable _ name;

Example:- 
1. float *ptr=&a;

2. float *ptr, x=10, y=25;
ptr=&x;
cout<<"x="<<*ptr<<endl;
ptr=&y;
cout<<"y="<<*ptr<<endl;

Pointer to Class:-

In C++, A pointer to a class is similar as a pointer to a structure. It uses -> arrow operator to access data members and member functions of class. we must initialize the pointer with an object of class before using it.
C++ में किसी क्लास का पॉइंटर, स्ट्रक्चर के पॉइंटर के समान ही होता है। यह तीर के चिन्ह -> का प्रयोग कर क्लास के डाटा मेम्बर्स एवं मेम्बर फंक्शन को एक्सेस करता है। पॉइंटर का प्रयोग करने से पूर्व उसे क्लास के ऑब्जेक्ट से इनिशियलाइज़ किया जाता है।   
Syntax:-

Declaration of pointer:
class_name *pointer_name;  
Test *ptr;
 
Initialization of pointer:
Test obj;
Test *ptr;
ptr = &obj;

Accessing through pointer:
pointer_name->class_member;
cout << ptr->a;

Example:-
class Test{
public:
int a;
Test(){
a=10;
}
};
int main(){
Test obj;
Test *ptr;  // Pointer to class Test
ptr = &obj;
cout << obj.a;
cout << ptr->a;  // Accessing member with pointer
}

No comments:

Post a Comment