What actually facilitates the late binding in run time polymorphism?
Is it only the keyword VIRTUAL?
#include <iostrem.h>
#include <conio.h>
class A
{
public:
virtual void display ()
{
cout<<"A";
}
};
class B: public A
{
public:
void display ()
{ cout << "B"; }
};
class C: public A
{
public:
void display ()
{cout <<"C";}
};
void main()
{
A a;
B b;
C c;
A * ptr[3];
ptr[0] = &a;
ptr[1] = &b;
ptr[2] = &c;
ptr[1]-> display(); //What's so run time about this?
getch();
}
----------------------------------------------------------------------------------------------------------
चूंकि Objects भी Memory में Store होते हैं, इसलिए इन Objects को भी ठीक उसी प्रकार से Pointers द्वारा Point किया जा सकता है, जिस प्रकार से किसी Variable को। उदाहरण के लिए हम किसी Object को Define करके उसके Address को किसी Pointer में Store कर सकते हैं:
Student student1; <-- Define Object of a class Student
Student * ptrStudent1 = &student1; <-- Place Object’s Address in ptrStudent1
जैसाकि हमने पहले भी कहा कि Pointer Variable व Variable दोनों का Data Type समान होना चाहिए, ठीक इसी आधार पर हमने पहले Student Class का एक Object student1 Define किया है। फिर student1 के Address को एक Student प्रकार के Pointer Object ptrStudent1 को प्रदान किया है।
// pointers to class objects
#include <iostream.h>
#include <string.h> // for strcpy()
#include <conio.h>
class employee // employee class
{
private:
enum {LEN=30}; // length of name
char name[LEN]; // employee name
unsigned long number; // employee number
public: // 2-arg constructor
employee(char* na, unsigned long nu) : number(nu)
{
strcpy(name, na);
}
};
void main()
{ // employee objects
employee emp1(“Kuldeep”, 123123L);
employee emp2(“Mishra”, 234234L);
cout << “Address values”;
cout << endl << &emp1 // print addresses of objects
<< endl << &emp2;
employee* ptr; // pointer to employees
cout << “\nPointer values”;
ptr = &emp1; // pointer points to emp1
cout << endl << ptr; // print pointer value
ptr = &emp2; // pointer points to emp2
cout << endl << ptr; // print pointer value
}
Output
Address values
0x0012ff68
0x0012ff44
Pointer values
0x0012ff68
0x0012ff44
हम Output से देख सकते हैं कि employee Object किसी int या float प्रकार के Variable से कितनी ज्यादा Memory Reserve करता है। ये Memory Class के सभी Data Members की कुल Memory के जोड के बराबर होता है।
अभी तक हमने जितने भी Programs बनाए हैं, उन सभी में हमने Variable या Objects के नाम से उनको Access किया है। मानलो कि हमें किसी Variable का नाम पता नहीं है लेकिन हम उस Variable का Address जानते हैं। इस स्थिति में भी हम Variables को Access कर सकते हैं। जब किसी Variable के मान को उसके नाम के बजाय उसके Address से Access करना होता है, तब हमें एक Special Syntax को Follow करना पडता है। इसे निम्न Program में दर्शाया गया है-
// accessing the variable pointed to
#include <iostream.h>
#include <conio.h>
void main()
{
int var1 = 101; // two integer variables
int var2 = 202;
int* ptr; // pointer to integers
ptr = &var1; // pointer points to var1
cout << endl << *ptr; // print contents of pointer (11)
ptr = &var2; // pointer points to var2
cout << endl << *ptr; // print contents of pointer (22)
getch();
}
ये Program var1 व var2 के मान को Print करता है लेकिन हम देख सकते हैं कि हमने ये मान इन Variables के नाम var1 या var2 का प्रयोग करके Print नहीं किया है, बल्कि हमने एक Special Syntax का प्रयोग किया है। हमने var1 व var2 के मान को Print करने के लिए *ptr Expression का प्रयोग किया है। इस Expression को Indirection Operator ( * ) कहा जाता है। Indirection Operator ( * ) के Right में जो Pointer Variable होता है, वह जिस Variable को Point करता है, उस Variable का मान Return होता है।
हम हमारे उदाहरण में देखें तो Indirection Operator के Right Side में एक Pointer Variable (*ptr) है, और ये ptr पहले var1 को Point करता है और बाद में var2 को। इसलिए पहले var1 का मान Print होता है और बाद में var2 का। Indirection Operator को कई बार “Content of” Operator भी कहा जाता है। जैसे *ptr को हम “Content of ptr” भी कह सकते हैं।
हम Pointer Variable का प्रयोग ना केवल Variable में Value Store करने या Variable से Values प्राप्त करने के लिए कर सकते हैं बल्कि हम जो कुछ भी प्रक्रिया एक Variable के नाम को उपयोग में लेकर करते है, वे सभी काम Variable के Pointer Variable से उस Variable पर Directly कर सकते हैं। इसे समझने के लिए निम्न Program देखिए-
// other access using pointers
#include <iostream.h>
#include <conio.h>
void main()
{
int var1, var2; // two integer variables
int* ptr; // pointer to integers
ptr = &var1; // set pointer to address of var1
*ptr = 37; // same as var1=37
var2 = *ptr; // same as var2=var1
cout << endl << var2; // verify var2 is 37
getch();
}
एक बात हमेंशा ध्यान रखें कि एक * का प्रयोग Pointer Variable Define करते समय भी किया जाता है और Pointer Variables में Stored Address वाले Variable के मान को Access करने के लिए भी किया जाता है। जैसे
int* ptr // Declaration
*ptr = 21 // Indirection
Indirection Operator का प्रयोग करके किसी Address पर Stored मान को Access करना, Pointer को Indirect Addressing या Dereferencing करना कहलाता है। सारांश में हम अभी तक के अपने Discussion को निम्नानुसार लिख सकते हैं-
int v; // defines variable v of type int
int* p; // defines p as a pointer to int
p = &v; // assigns address of variable v to pointer p
v = 3; // assigns 3 to v
*p = 3; // also assigns 3 to v
यहां अन्तिम दो Statements Normal Addressing व Direct Addressing में अन्तर दर्शा रहे हैं जहां हम पहले Statement में एक Variable v को उसके नाम से Refer कर रहे हैं और दूसरे Statement में उसी Variable को उसके Address *p एक Pointer या Indirect Addressing से Refer कर रहे हैं।
अभी तक हमने केवल Pointers के Basic Idea व उसे Use करने के तरीके को समझने की कोशिश की है। वास्तव में जिस प्रकार से हमने अभी तक Pointers को Use किया है, उस तरह से नहीं करते हैं। क्योंकि जब हमारे पास किसी Variable का नाम होता है, तब हम उस Variable को उसके नाम से ही Directly Access कर सकते हैं Pointers का प्रयोग सामान्यतया तब किया जाता है जब हमें उस Variable को Access करना होता है, जिसका कोई नाम ही नहीं होता है।
No comments:
Post a Comment