Type Casting in C++ (सी++ में टाइप कास्टिंग)

To convert a value of one data type into another data type is known as type casting or type conversion. C++ allows constants and variables of different data types to be used in an expression. During evaluation, the compiler may automatically convert values to appropriate types according to the conversion rules.

एक डेटा टाइप के मान को किसी दूसरे डेटा टाइप में परिवर्तित करना Type Casting या Type Conversion कहलाता है। C++ में एक एक्सप्रेशन में विभिन्न डेटा टाइप के वेरिएबल और कॉन्स्टेंट का प्रयोग किया जा सकता है। एक्सप्रेशन का मूल्यांकन करते समय कम्पाइलर आवश्यकतानुसार डेटा टाइप को स्वतः परिवर्तित कर सकता है।

There are two main types of type conversion:

टाइप कन्वर्शन मुख्यतः दो प्रकार की होती है—

  1. Implicit Type Conversion (Automatic Type Conversion)

  2. Explicit Type Conversion (Manual Type Conversion)

1. Implicit Type Casting or Automatic Type Conversion (स्वतः टाइप कन्वर्शन)

Implicit type conversion is performed automatically by the compiler when a value is converted to another compatible data type. It does not require a type-casting operator.

Implicit Type Conversion कम्पाइलर द्वारा स्वतः की जाती है। इसमें प्रोग्रामर को किसी विशेष टाइप-कास्टिंग ऑपरेटर का प्रयोग नहीं करना पड़ता।

Example 1:

int a = 20;
float b;
b = a;
cout << b;

Output:

20

Here, the integer value 20 is automatically converted into a floating-point value and stored in b.

यहाँ int टाइप के वेरिएबल a की वैल्यू 20 को स्वतः float टाइप में परिवर्तित करके b में स्टोर किया जाता है।

Important Rules of Implicit Type Conversion

During arithmetic expressions, C++ performs usual arithmetic conversions. In simple terms, smaller integer types such as char and short are generally promoted to int (or unsigned int) before arithmetic operations.

अरिथमेटिक एक्सप्रेशन में C++ कुछ नियमों के अनुसार डेटा टाइप को परिवर्तित करता है। सामान्यतः char तथा short जैसे छोटे integer types को arithmetic operation से पहले int में promote किया जाता है।

For example:

int a = 10;
float b = 2.5;
float result;
result = a + b;

Here, a is converted to float, and the result is 12.5.

यहाँ a की वैल्यू 10 को float में परिवर्तित किया जाता है। अतः परिणाम 12.5 प्राप्त होता है।

Example 2: Integer Division

float ans;
ans = 9 / 4;
cout << ans;

Output:

2

Although ans is a float, the expression 9 / 4 is evaluated using integer division because both operands are integers. Therefore, the result is 2, which is then converted to 2.0 when assigned to ans.

यहाँ ans का डेटा टाइप float है, लेकिन 9 / 4 में दोनों operands integer हैं। इसलिए integer division के कारण परिणाम 2 प्राप्त होता है। बाद में यह 2.0 के रूप में ans में स्टोर होता है।

2. Explicit Type Casting or Explicit Type Conversion (मैन्युअल टाइप कन्वर्शन)

Explicit type conversion is performed manually by the programmer when a value needs to be converted into a specific data type.

जब प्रोग्रामर स्वयं किसी वैल्यू को एक डेटा टाइप से दूसरे डेटा टाइप में परिवर्तित करता है, तो इसे Explicit Type Conversion कहते हैं।

C++ provides different ways to perform explicit type conversion.

C++ में Explicit Type Conversion के लिए निम्नलिखित notation का प्रयोग किया जा सकता है।

Syntax:

(data_type) expression;

Example:

int a = 2000;
float b;
b = (float)a;

Here, the C-style cast (float)a converts the integer value of a into a floating-point value.

यहाँ (float)a के द्वारा a की integer value को float में परिवर्तित किया जाता है।

Another notation is:

float(a);

This is called functional notation.

Example 3: Correct Floating-Point Division

float ans;
ans = (float)9 / 4;
cout << ans;

Output:

2.25

Here, 9 is converted into float before division. Therefore, floating-point division is performed and the result is 2.25.

यहाँ division से पहले 9 को float में परिवर्तित किया जाता है। इसलिए floating-point division होता है और परिणाम 2.25 प्राप्त होता है।

Program: Calculate Student Percentage and Division

Write a C++ program to calculate the percentage of a student and display the division based on the percentage obtained.

एक C++ प्रोग्राम लिखिए जो विद्यार्थी के प्राप्त अंकों के आधार पर प्रतिशत की गणना करे तथा उसकी division प्रदर्शित करे।

Program:

#include <iostream>
using namespace std;
int main()
{
int p, c, m, e, h, om, tm;
float per;
cout << "Enter marks 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;
// Explicit type casting
per = (static_cast<float>(om) / tm) * 100;
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 failed. Try Again."
<< endl;
return 0;
}

Sample Output:

Enter marks of following subjects:
Physics = 75
Chemistry = 70
Maths = 80
English = 65
Hindi = 70
Total obtained marks = 360
Enter total marks of examination = 500
72% Congrats! You are passed in First Division

Explanation (व्याख्या)

The statement:

per = (static_cast<float>(om) / tm) * 100;

converts om into a floating-point value before division. This prevents integer division and allows the percentage to be calculated accurately.

यहाँ static_cast<float>(om) के द्वारा प्राप्तांक om को float में परिवर्तित किया जाता है। इससे integer division नहीं होता और प्रतिशत की गणना सही तरीके से होती है।

Note: This example assumes that the entered marks and total marks are valid and that tm is greater than zero.

Type Casting Operators in C++ (C++ के टाइप कास्टिंग ऑपरेटर्स)

C++ provides four named cast operators to perform specific types of conversions.

C++ में विभिन्न प्रकार के conversions को नियंत्रित करने के लिए चार named cast operators उपलब्ध हैं।

  1. static_cast

    static_cast<new_type>(expression)

    Used for well-defined conversions, such as converting an integer to a float.

  2. dynamic_cast

    dynamic_cast<new_type*>(expression)

    Used mainly for safe runtime conversions within polymorphic class hierarchies.

  3. reinterpret_cast

    reinterpret_cast<new_type>(expression)

    Used for low-level conversions that reinterpret a value's representation. It must be used carefully.

  4. const_cast

    const_cast<new_type>(expression)

    Used to add or remove const or volatile qualifiers. Modifying an object that was originally declared const is not allowed.

Type casting is an important feature of C++ that allows values of different data types to be converted when required. Implicit type conversion is performed automatically by the compiler, whereas explicit type conversion is performed by the programmer.

C++ में Type Casting एक महत्वपूर्ण concept है, जिसकी सहायता से विभिन्न डेटा टाइप के मानों को आवश्यकतानुसार परिवर्तित किया जा सकता है। Implicit Type Conversion कम्पाइलर द्वारा स्वतः की जाती है, जबकि Explicit Type Conversion प्रोग्रामर द्वारा मैन्युअल रूप से की जाती है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Programming Methodologies and Data structures using C++ Language with OOP's Concepts

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)