- Introduction to C++ type conversion in Hindi
- Implicit type conversion in Hindi
- Explicit type conversion in Hindi
Introduction to C++ Type Conversion
Type conversion एक ऐसी process होती है जिसमें एक तरह के data type की value/variable को दूसरे तरह के data type के variable को assign किया जाता है। ऐसा सिर्फ किसी एक particular operation के लिए किया जाता है, हर बार ऐसा नहीं होता है।
उदाहरण के लिए आप program में एक int type का और दूसरा float type का variable create करते है। Integer type के variable को आपने initialize नहीं किया है। Float type के variable में आपने एक floating point value डाली है। जैसा की नीचे दिए गए उदाहरण में show किया गया है।
यदि इस situation में आप float type का variable int type के variable को assign करते है तो type conversion occur होगा। Float type के variable की value integer में convert हो जायेगी और integer variable को assign कर दी जायेगी।
इस operation के बाद integer variable में एक whole number होगा। ऊपर दिए गए उदाहरण में integer variable में 10 value होगी।
Type conversion को type casting भी कहा जाता है। Type conversion implicit और explicit 2 प्रकार का होता है। इनके बारे में आगे के sections में detail से बताया जा रहा है।
Implicit Type Conversion
Implicit type conversion को automatic type conversion भी कहा जाता है। ऊपर दिया गया उदाहरण implicit type conversion का ही उदाहरण था। Implicit type conversion में एक type के variable को दूसरे type की value/variable बिना किसी problem के assign हो जाती है। यह automatically होता है।
Implicit type conversion सिर्फ compatible type में ही होता है। C++ में सभी built-in types (int, short int, long int, double, float,char, Boolean) एक दूसरे के compatible है। दूसरे user defined types (classes) के लिए आप खुद conversion perform कर सकते है।
Explicit Type Conversion
User defined data types को आप खुद के purpose के लिए खुद define करते है। इसलिए compiler ऐसे types के लिए automatic conversion provide नहीं करता है। ऐसी situation में आप खुद ही conversion handle करते है। 3 situations में आपको explicit conversion की जरुरत हो सकती है। इनके बारे में नीचे दिया जा रहा है।
- Basic to class type - जब आप किसी basic type variable को class type variable को assign कर रहे हो।
- Class to basic type - जब आप class type के variable को basic type variable को assign कर रहे हो।
- Class to class type - जब आप एक class type object को दूसरे class type object को assign कर रहे हो।
आइये अब इनके बारे में detail से जानने का प्रयास करते है।
Basic to Class Type Conversion
Basic type को class type में आसानी से convert किया जा सकता है। इस तरह का conversion ऐसे statements के दौरान होता है जब assignment operator के दाँयी तरफ built in type हो और बाँयी तरफ class type होता है।
Basic type को class type में convert करने के लिए आप constructor को यूज़ करते है। जैसा की आपको पता है की constructors object construct करने के लिए यूज़ किये जाते है।
ऐसी situation में आप constructor को overload load कर सकते है और इस प्रकार program कर सकते है की यदि कोई भी उस class type के object को basic type assign करे तो वह constructor आपके अनुसार execute हो जाये।
आइये इसे एक उदाहरण से समझने का प्रयास करते है। मान लीजिये आपने bigsmall नाम से एक class create की है जो की इस प्रकार है।
जैसा की आप देख सकते है bigsmall class में दो data members declare किये गए है और object create करते समय जैसे ही normal constructor execute होता है दोनों को 1 value assign हो जाती है।
इस class के लिए एक constructor और declare किया जा सकता है ताकि जब भी कोई इस class के object को basic type assign करे तो वह constructor call हो जाये और वह value दोनों variables को calculation के बाद assign हो जाये। ऐसा constructor आप इस प्रकार declare कर सकते है।
जब कोई भी bigsmall class के object को integer value assign करेगा तो ये constructor call होगा और assign की गयी value इसी के according assign की जायेगी। आइये अब देखते है की इसे main function में किस प्रकार यूज़ किया जा सकता है।
इसे एक complete program के रूप में निचे दिया जा रहा है।
ऊपर दिया गया program निचे दिया गया output generate करता है।
इसी प्रकार आप दूसरे basic types के लिए भी constructor overload कर सकते है।
इसके लिए C++ आपको एक conversion function define करने की capability provide करती है। इसे casting operator भी कहते है। इस function को operator keyword के द्वारा define किया जाता है। इसका general syntax नीचे दिया जा रहा है।
ऊपर दिए गए syntax में operator एक keyword है और data-type-name उस data type का name है जिसे आप class type को assign करने वाले है। इस function को आप class के बाहर भी scope resolution operator के माध्यम से define कर सकते है। आइये इसे एक उदाहरण से समझने का प्रयास करते है।
मान लीजिये आप पहले ऊपर create की गयी bigsmall class के object को किसी integer variable को assign करना चाहते है तो इसके लिए आप casting operator इस प्रकार define करेंगे।
जैसा की आप ऊपर दिए गए conversion function में देख रहे है जब भी bigsmall class के object को किसी integer variable को assign किया जायेगा तो class के data members bigNum और smallNum की values add होकर उस variable को assign होगी। आइये अब देखते है की इसे main() function में किस प्रकार यूज़ किया जा सकता है।
इसे एक complete उदाहरण के रूप में निचे दिया जा रहा है।
ऊपर दिया गया program निचे दिया गया output generate करता है।
Basic type को class type में convert करने के लिए आप constructor को यूज़ करते है। जैसा की आपको पता है की constructors object construct करने के लिए यूज़ किये जाते है।
ऐसी situation में आप constructor को overload load कर सकते है और इस प्रकार program कर सकते है की यदि कोई भी उस class type के object को basic type assign करे तो वह constructor आपके अनुसार execute हो जाये।
आइये इसे एक उदाहरण से समझने का प्रयास करते है। मान लीजिये आपने bigsmall नाम से एक class create की है जो की इस प्रकार है।
जैसा की आप देख सकते है bigsmall class में दो data members declare किये गए है और object create करते समय जैसे ही normal constructor execute होता है दोनों को 1 value assign हो जाती है।
इस class के लिए एक constructor और declare किया जा सकता है ताकि जब भी कोई इस class के object को basic type assign करे तो वह constructor call हो जाये और वह value दोनों variables को calculation के बाद assign हो जाये। ऐसा constructor आप इस प्रकार declare कर सकते है।
जब कोई भी bigsmall class के object को integer value assign करेगा तो ये constructor call होगा और assign की गयी value इसी के according assign की जायेगी। आइये अब देखते है की इसे main function में किस प्रकार यूज़ किया जा सकता है।
इसे एक complete program के रूप में निचे दिया जा रहा है।
#include <iostream>
|
ऊपर दिया गया program निचे दिया गया output generate करता है।
इसी प्रकार आप दूसरे basic types के लिए भी constructor overload कर सकते है।
Class to Basic Type Conversion
Basic type को class type में convert करने के लिए constructor को यूज़ किया गया था। लेकिन class type को basic type में आप constructor के माध्यम से convert नहीं कर सकते है।इसके लिए C++ आपको एक conversion function define करने की capability provide करती है। इसे casting operator भी कहते है। इस function को operator keyword के द्वारा define किया जाता है। इसका general syntax नीचे दिया जा रहा है।
// Statements to be executed
|
ऊपर दिए गए syntax में operator एक keyword है और data-type-name उस data type का name है जिसे आप class type को assign करने वाले है। इस function को आप class के बाहर भी scope resolution operator के माध्यम से define कर सकते है। आइये इसे एक उदाहरण से समझने का प्रयास करते है।
मान लीजिये आप पहले ऊपर create की गयी bigsmall class के object को किसी integer variable को assign करना चाहते है तो इसके लिए आप casting operator इस प्रकार define करेंगे।
जैसा की आप ऊपर दिए गए conversion function में देख रहे है जब भी bigsmall class के object को किसी integer variable को assign किया जायेगा तो class के data members bigNum और smallNum की values add होकर उस variable को assign होगी। आइये अब देखते है की इसे main() function में किस प्रकार यूज़ किया जा सकता है।
int num;
obj.display();
num = obj;
cout<<"Num variable contains"<<num;
return 0;
}
|
इसे एक complete उदाहरण के रूप में निचे दिया जा रहा है।
#include <iostream>
int num;
cout<<"Before conversion inside object ";
// Will display 1 and 1 as output
obj.display();
num = obj;
cout<<"After converting object into num variable "; // Will display 2 as output
cout<<"num variable contains "<<num;
return 0;
}
|
ऊपर दिया गया program निचे दिया गया output generate करता है।
One Class to Another Class Type Conversion
एक class से दूसरी class में type conversion आप constructor द्वारा भी कर सकते है और operator function द्वारा भी कर सकते है। इसके लिए आप दोनों classes में एक दूसरे के लिए operator function define कर सकते है या फिर constructors define कर सकते है।
जिस प्रकार आपने class to basic type conversion में int type के लिए conversion function define किया था उसी प्रकार आप एक class type के लिए conversion function define कर सकते है।
जिस प्रकार आपने class to basic type conversion में int type के लिए conversion function define किया था उसी प्रकार आप एक class type के लिए conversion function define कर सकते है।
मान लीजिये आप दो अलग अलग classes के objects को इस प्रकार यूज़ कर रहे है।
ऊपर दिए गए expression में x class के object को y class का object assign किया जा रहा है। इस situation में x class एक conversion function define करेगी जो y class के object को modify करके x class के object को assign करने में मदद करेगा।
0 Comments