CSC166 ยท 2079 ยท 5 marks
What are possible data conversion types? Write a program to show basis to user defined conversion.
What are possible data conversion types? Write a program to show basis to user defined conversion. [5]
Quick answer
The following program demonstrates converting a basic (built-in) data type to a user-defined data type (class) using a constructor, and converting a user-defined type back to a basic type using a conversion function (operator overloading).
Data Conversion Types and User-Defined Conversion
Possible Data Conversion Types
In C++, there are mainly two types of data conversion:
1. Implicit Conversion (Automatic Type Conversion)
- Done automatically by the compiler without any external trigger from the user.
- Takes place when there is more than one data type in an expression.
- All data types are upgraded to the largest data type present in the expression to avoid loss of data.
Example:
int a = 5;
float b = 2.5;
float result = a + b; // int 'a' is automatically converted to float
2. Explicit Conversion (User-Defined / Type Casting)
- Also called type casting.
- The user manually converts the data type of a value to a desired type.
- In C++, it can be done in two ways:
- Using C-style cast:
(type) expression - Using C++ cast operator:
type(expression)
- Using C-style cast:
Program to Show Basic to User-Defined Conversion
#include<iostream>
using namespace std;
// User-defined class
class Celsius {
float temperature;
public:
// Default constructor
Celsius() {
temperature = 0;
}
// Constructor for basic to user-defined conversion
// Converts float (basic type) to Celsius object (user-defined type)
Celsius(float t) {
temperature = t;
cout << "Basic to User-Defined Conversion: float -> Celsius object" << endl;
}
// Conversion function: user-defined to basic type
// Converts Celsius object to float (basic type)
operator float() {
cout << "User-Defined to Basic Conversion: Celsius object -> float" << endl;
return temperature;
}
void display() {
cout << "Temperature = " << temperature << " C" << endl;
}
};
int main() {
float temp = 36.6;
// Basic to User-Defined Conversion
// float is implicitly converted to Celsius object using constructor
Celsius c1 = temp;
c1.display();
// User-Defined to Basic Conversion
// Celsius object is converted to float using conversion function
float t2 = c1;
cout << "Converted back to float: " << t2 << endl;
// Explicit (user-defined) type casting
Celsius c2(98.4);
float t3 = (float) c2; // C-style explicit cast
cout << "Explicit cast result: " << t3 << endl;
return 0;
}
Output
Basic to User-Defined Conversion: float -> Celsius object
Temperature = 36.6 C
User-Defined to Basic Conversion: Celsius object -> float
Converted back to float: 36.6
Basic to User-Defined Conversion: float -> Celsius object
User-Defined to Basic Conversion: Celsius object -> float
Explicit cast result: 98.4
Summary Table
| Conversion Type | Direction | Mechanism Used |
|---|---|---|
| Basic to User-Defined | float -> class | Constructor with parameter |
| User-Defined to Basic | class -> float | Conversion function (operator float()) |
| Explicit (Type Casting) | Any -> Any | (type) or type() |
Note: The constructor enables basic-to-user-defined conversion, while the conversion operator (operator overloading) enables user-defined-to-basic conversion. Both together form the complete picture of user-defined type conversion in C++.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.