Type Casting in C++

    C++ Type Conversion

    C++ एक Strong-Typed language है, जिसका अर्थ है कि प्रोग्राम में उपयोग किए जाने से पहले Data Type को एक variable के साथ associate जाता है। किसी दिए गए Expression के data Type को दूसरे प्रकार data Type में बदलने को type casting (टाइप कास्टिंग )कहा जाता है। type casting (टाइप कास्टिंग )  

    Type of casting

    Type Casting दो प्रकार की होती हैं;-

    1. Implicit type casting
    2. Explicit type casting

    Implicit Type Conversion 

    यह type Conversion स्वतः होने वाली   type casting हैं, जब दो समान Data type(डेटा टाइप) वाली value(वैल्यूज) पर Operation(ऑपरेशन) किया जाता है तो उसका result(परिणाम) समान Data Type में आता है। उदाहरण के लिए यदि दो Integer Value(इंटीजर वैल्यूज) को जोड़ा जाएगा तो परिणामस्वरूप प्राप्त होने वाली संख्या भी Integer (इंटीजर) ही होगी।

    Example:-


        #include <iostream>
        #include <string>
        using namespace std;

        int main()
        {
           int a = 30; //  Integer
           int b = 20; //Integer

            cout<< "the value of a + b is = "<< a+b; // return Integer  value (50)
            return 0;
        }

    किन्तु यदि Expression(एक्सप्रेशन) का एक Iperands Integer हो तथा दूसरा float तो परिणाम किस Data Type(डेटा टाइप) में आएगा? ऐसी स्थिति में परिणाम Float(फ्लोट) में आएगा। सामान्य शब्दों में यह कहा जा सकता है कि यदि किसी एक्सप्रेशन में एक से अधिक प्रकार के डेटा टाइप प्रयुक्त हुए हैं तो सामान्यतः परिणाम उस Data type(डेटा टाइप) में होगा, जो मैमोरी में अधिक स्थान घेरता है।

    अधिकांशतः इसके लिए Compiler(कंपाइलर) पहले अन्य Data Type(डेटा टाइप) को उस Data Type(डेटा टाइप) में परिवर्तित कर देता है जो मैमोरी में अधिक स्थान घेरता है। इसके पश्चात् उस Expression(एक्सप्रेशन)  के परिणाम की गणना की जाती है। इस प्रकार यह कहा जा सकता है कि आवश्यकता पड़ने पर Compiler(कंपाइलर) स्वत Data Type(डेटा टाइप)  में परिवर्तन कर लेता है।

    Example:- 


        #include <iostream>
        #include <string>
        using namespace std;

        int main()
        {
            int a = 30;      //  Integer  value
            float b = 56.12; //float value
            cout<<"the value of a = "<<a<<endl;
            cout<<"the value of b = "<<b<<endl;
            cout << "result of a + b is = " << a + b; // return float value
            return 0;
        }

    Output:-

    the value of a = 30
    the value of b = 56.12
    result of a + b is = 86.12

    Example:-

        int x = 20;
        short int y = 5;
        int z = x + y;

    Explain Example:-

    उपरोक्त उदाहरण में, दो अलग-अलग Data Type के Variable, x, और y लिए हैं, जहां x एक int प्रकार का variable है, और y एक short int प्रकार का है। और variable z भी एक Integer type का variable है जो x और y के Result(x+y) को store करता है। लेकिन C++ compiler automatically, lower rank data type (short int) के मान को दो संख्याओं के योग से पहले higher type (int) में परिवर्तित कर देता है। इस प्रकार, C++ के प्रकार के conversion में data loss, overflow, या sign loss से बचा जाता है।

    Order of the typecast in implicit conversion

    lower rank से higher rank तक data types का सही क्रम निम्नलिखित है:

    1. bool  -> char ->  short   int  ->  int  -> unsigned int  ->  long   int  -> unsigned long   int  ->  long   long   int ->  float ->  double -> long  double  

    Example 1 C++ :: Implicit type conversion का उपयोग करके int को Float Type में बदलने का Program

    implicit type conversion का उपयोग करके Smaller rank data types को higher types में बदलने का  Program(प्रोग्राम) बनाएं।


        #include <iostream>
        using namespace std;
        int main()
        {
            // assign the integer value
            int num1 = 25;
            // declare a float variable
            float num2;
            // convert int value into float variable using implicit conversion
            num2 = num1;
            cout << " The value of num1 is: " << num1 << endl;
            cout << " The value of num2 is: " << num2 << endl;
            return 0;
        }
       

    Output:-

     The value of num1 is: 25
     The value of num2 is: 25

    Example 1 C++ :: Implicit type Conversion का उपयोग करके double से  int data type में convert करने का Program 

    implicit type conversion का उपयोग करके higher types को   Smaller rank data types में बदलने का  Program(प्रोग्राम) बनाएं।


        #include <iostream>
        using namespace std;
        int main()
        {
            int num;             // declare int type variable
            double num2 = 15.25; // declare and assign the double variable

            // use implicit type conversion to assign a double value to int variable
            num = num2;
            cout << " The value of the int variable is: " << num << endl;
            cout << " The value of the double variable is: " << num2 << endl;
            return 0;
        }
       

    Output:-

     The value of the int variable is: 15
     The value of the double variable is: 15.25

    Explain Example:-

    उपरोक्त Program में, हमने num को एक integer type और num2 को double data type variable के रूप में declared किया है और फिर num2 को 15.25 के रूप में assign किया है। इसके बाद, हम assignment operator(असाइनमेंट ऑपरेटर) का उपयोग करके num variable को num2 का मान assign करते हैं। इसलिए, एक सी ++ double data type मान को int type में compiler automatically converts कर देता है, इसे num वेरिएबल में assign करने से पहले और truncate valueको 15 के रूप में प्रिंट करता है।

    2.) Explicit type conversion

     Explicit type conversion यूजर द्वारा करवाया जाता हैं,  एक variable के Data type को दूसरे variable में बदलने के लिए user intervention आवश्यकता वाले conversion को Explicit type conversion कहा जाता है । दूसरे शब्दों में, एक explicit conversion programmer (प्रोग्रामर) को variable के data type को एक से दूसरे data type में manually बदलने या typecasts(टाइपकास्ट) करने की अनुमति देता है। इसलिए, इसे typecasting(टाइपकास्टिंग) के रूप में भी जाना जाता है। आम तौर पर, हम explicit type conversion को data को एक data type से दूसरे data type में परिवर्तित करने के लिए use करते हैं क्योंकि यह implicit conversion ruls का पालन नहीं करता है।

    अर्थात: Implicit conversion(स्वतः डेटा परिवर्तन) के अलावा यूजर (प्रोग्रामर) भी यह बता सकता है कि डेटा टाइप में परिवर्तन कब किया जाना है। इसे यूजर डिफाइड डेटा परिवर्तन भी कहा जाता है जिसे तकनीकी शब्दों में एक्सप्लिसिट टाइप कास्टिंग (explicit type casting) कहा जाता है। 

    इसका प्रारूप निम्न प्रकार है:

    (DataTypeToCast) Operand  // C style
    or
    DataTypeToCast (Operand)  // C++ style

    Example:-


        #include <iostream>  
        using namespace std;  
        int main ()  
        {  
        // declare a float variable  
        float a =  81.5;  
       
        cout << " The value of int a/2 is: " <<a/2 << endl;  

        // convert data type from float to int  and print value
        cout << " The value of float num2 is: " << (int)a/2 << endl;  
        return 0;  
        }  

    Output:-

     The value of int a/2 is: 40.75
     The value of float num2 is: 40

    Explain Example:-

    उपरोक्त उदाहरण में a/2 की स्थिति में पहले 2 फ्लोट में परिवर्तित होगा फिर गणना होगी। वहीं (int)a/2 की स्थिति में पहले a इंटीजर में परिवर्तित किया जाएगा फिर उसे इंटीजर 2 से विभाजित किया जाएगा। जब इंटीजर में परिवर्तित होगा तो उसका डेसीमल वाला हिस्सा (5) गायब हो जाएगा और उसकी वैल्यू सिर्फ 80 ही रह जाएगी। तत्पश्चात् 80 को दो से विभाजित करने पर परिणाम 40 प्राप्त होगा।

    Explicit type conversion को दो तरीकों से विभाजित किया गया है:

    1. Cast operator का उपयोग करके Explicit conversion  करना 
    2. Assignment operator (असाइनमेंट ऑपरेटर) का उपयोग करके  Explicit conversion  करना 

    Cast Operator 

     C++ Language में, एक Cast operator(कास्ट ऑपरेटर) एक unary operator(यूनरी ऑपरेटर) होता है जो बलपूर्वक एक type को दूसरे type में परिवर्तित करता है।

    Example:- 

    Cast operator(कास्ट ऑपरेटर) का उपयोग करके float value(फ्लोट वैल्यू) को int type(इंट टाइप) में बदलने का Program:-


        #include <iostream>
        using namespace std;
        int main()
        {
            float f2 = 6.7;
            // use cast operator to convert data from one type to another
            int x = static_cast<int >(f2);
            cout << " The value of x is: " << x;
            return 0;
        }
       

    Output:-

     The value of x is: 6

    C++ Example:-

    Assignment operator (असाइनमेंट ऑपरेटर) का उपयोग करके एक data type को दूसरे data type में बदलने का program


        #include <iostream>  
        using namespace std;  
        int main ()  
        {  
        // declare a float variable  
        float num2;  
        // initialize an int variable  
        int num1 = 25;  
       
        // convert data type from int to float  
        num2 = (float) num1;  
        cout << " The value of int num1 is: " << num1 << endl;  
        cout << " The value of float num2 is: " << num2 << endl;  
        return 0;  
        }  

    Output:-

     The value of int num1 is: 25  
     The value of float num2 is: 25

    Post a Comment

    0 Comments