Type Casting or Type Conversion in C++

To Convert an expression of a given type into another type is known as type-casting or type conversion. C++ language allows combination of constants and variables of different types in an expression. It automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance. 
एक एक्सप्रेशन में दिए गए डाटा टाइप को किसी अन्य डाटा टाइप में परिवर्तित करना टाइप कास्टिंग या टाइप कन्वर्शन कहलाता है। C++ लैंग्वेज में एक एक्सप्रेशन कई प्रकार के वेरिएबल या कांस्टेंट रख सकती है जिन्हें अंत में किसी एक डाटा टाइप में परिवर्तित किया जाता है जिससे सही परिणाम प्राप्त होता है। 

There are two types of Type Casting-
टाइप कास्टिंग  दो प्रकार की होती है- 

1.) Implicit type-casting or Implicit Type Conversion or Automatic Type Conversion:-

Implicit conversions do not require any operator. They are automatically performed by compiler when a value is copied to a compatible type.
इस कन्वर्शन में किसी ऑपरेटर की आवश्यकता नहीं होती है यह टाइप कास्टिंग कम्पाइलर द्वारा स्वत: की जाती है एवं हल को सही टाइप में परिवर्तित किया जाता है।
The following rules are applied during evaluating expressions using implicit type casting -
यहाँ निम्न नियमो का पालन किया जाता है -
All short and char are automatically converted to int data type then
1. If one operand is long double, then all other operand will be converted to long double and result will be long double. otherwise
2. If one operand is double, then all other operand will be converted to double and result will be double. otherwise
3. If one operand is float, then all other operand will be converted to float and result will be float.
4. If one operand is unsigned long int, then all other will be converted into unsigned long int and result will be unsigned long int. otherwise...

सभी शोर्ट एवं करैक्टर टाइप को इन्टिजर टाइप में परिवर्तित किया जायेगा इसके पश्चात् -
1. यदि एक भी ऑपरेंड लॉन्ग डबल टाइप का है तब अन्य सभी को लॉन्ग डबल टाइप में परिवर्तित किया जायेगा एवं हल लॉन्ग डबल टाइप का होगा अन्यथा 
2. यदि एक भी ऑपरेंड डबल टाइप का है तब अन्य सभी को डबल टाइप में परिवर्तित किया जायेगा एवं हल डबल टाइप का होगा अन्यथा 
3. यदि एक भी ऑपरेंड फ्लोट टाइप का है तब अन्य सभी को फ्लोट टाइप में परिवर्तित किया जायेगा एवं हल फ्लोट टाइप का होगा अन्यथा
4. यदि एक भी ऑपरेंड लॉन्ग इंट टाइप का है तब अन्य सभी कोलॉन्ग इंट टाइप में परिवर्तित किया जायेगा एवं हल लॉन्ग इंट टाइप का होगा।
For example:
1.)
int a=20;
float  b;
b=a; // b=20.00 float

2.)
float ans;
ans=9/4; 
//ans=2.0 float 

2.)Explicit Type Casting or Explicit Type Conversion:-

C++ is a strong-typed language. Many times there may arise a situation where we perform manual type conversion in place of automatic type conversion. In C++, we have two notations for explicit type conversion: functional and c-like casting: 
C++ एक शक्तिशाली टाइप्ड लैंग्वेज है इसमें कई बार ऐसी स्थिति उत्पन्न हो जाती है जब आटोमेटिक टाइप कास्टिंग के स्थान पर मैन्युअल टाइप कास्टिंग की जाती है इसे एक्सप्लिसिट टाइप कास्टिंग कहा जाता है। C++ में हम निम्न दो नोटेशन का प्रयोग कर एक्सप्लिसित टाइप कास्टिंग कर सकते है - 
Syntax:-
(data_type) expression;
data_type (expression);

example:-
1.)
int  a=2000;
float b;
b = (float) a;    // c-like cast notation
b = float (a);    // functional notation

2.)
float ans;
ans=(float)9/4; //ans=2.25

Program:-
C++ program to find division of a student when subject marks are given.

#include<iostream.h>
#include<conio.h>
void main(){
int p,c,m,e,h,om,tm;
float per;
clrscr();
cout<<"Enter numbers of following subjects- "<<endl;
cout<<"Physics = ";
cin>>p;
cout<<"Chemistry = ";
cin>>c;
cout<<"Maths = ";
cin>>m;
cout<<"English = ";
cin>>e;
cout<<"Hindi = ";
cin>>h;
om=p+c+m+e+h;
cout<<"Total obtained marks= "<<om<<endl;
cout<<"Enter Total Marks Of Examination = ";
cin>>tm;
per=((float)om/tm)*100; //explicit type-casting
if(per>=60) cout<<per<<"% Congrats !! You are passed in First Division"<<endl;
else
{
if(per>=45) cout<<per<<"% Congrats!! You are passed in Second Division"<<endl;
else
{
if(per>=33) cout<<per<<"% You are passed in Third Division"<<endl;
else cout<<per<<"% You are Fail\n Try Again"<<endl;
}
}
getch();
}

In order to control these types of conversions between classes, we have four specific casting operators:- 
क्लासेस के मध्य, इन टाइप कन्वर्शन को नियंत्रित करने के लिए हम निम्न चार प्रकार के कास्टिंग ऑपरेटर  प्रयुक्त कर सकते है :-

dynamic_cast <new_type> (expression)
reinterpret_cast <new_type> (expression)
static_cast <new_type> (expression)
const_cast <new_type> (expression)

No comments:

Post a Comment