C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In this tutorial, we will discuss conversion constructors in C++ and how they can be used to convert other types to the current type class.
Introduction to Conversion Constructors: A conversion constructor is a single-argument constructor that converts an object of a different type to an object of the current class type. By defining a conversion constructor, you enable implicit conversions of the specified type to your class type.
Creating a Conversion Constructor: To create a conversion constructor, you need to define a constructor that takes a single argument of the type you want to convert to the current class type. In the constructor's body, you can specify how the object of the specified type should be converted.
Example:
Let's create an example that demonstrates how to define a conversion constructor that converts an int
to a Fraction
class.
#include <iostream> class Fraction { private: int numerator; int denominator; public: // Default constructor Fraction() : numerator(0), denominator(1) {} // Conversion constructor: Convert an int to a Fraction Fraction(int num) : numerator(num), denominator(1) {} // Function to print the fraction void print() const { std::cout << numerator << "/" << denominator << std::endl; } }; int main() { // Create a Fraction object using the default constructor Fraction frac1; frac1.print(); // Output: 0/1 // Create a Fraction object using the conversion constructor Fraction frac2 = 5; // Convert int 5 to a Fraction frac2.print(); // Output: 5/1 // Implicit conversion using the conversion constructor Fraction frac3 = 7; frac3.print(); // Output: 7/1 return 0; }
In the example above, we have defined a Fraction
class with a conversion constructor that takes an int
as its argument. The conversion constructor initializes the numerator
with the value of the int
and the denominator
with 1. We then demonstrated the conversion constructor's usage to create Fraction
objects from int
values.
int
to a Fraction
class.How to convert other types to current type class in C++:
class MyType { public: int value; // Conversion constructor MyType(int v) : value(v) { // Constructor body } }; int main() { MyType obj = 42; // Conversion from int to MyType using the conversion constructor return 0; }
Explicit type conversion using conversion constructors in C++:
class MyType { public: int value; // Conversion constructor MyType(int v) : value(v) { // Constructor body } }; int main() { MyType obj = static_cast<MyType>(42); // Explicit conversion using static_cast return 0; }
Conversion constructor for string to custom class in C++:
#include <iostream> #include <string> class MyType { public: int value; // Conversion constructor from string MyType(const std::string& str) : value(std::stoi(str)) { // Constructor body } }; int main() { MyType obj = "42"; // Conversion from string to MyType using the conversion constructor std::cout << obj.value << std::endl; return 0; }