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

Conversion Constructor: Convert Other Types To Current Type Class in C++

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.

  • Summary: In this tutorial, we discussed conversion constructors in C++ and their usage to convert objects of other types to the current class type. We provided an example demonstrating the creation and usage of a conversion constructor for converting an int to a Fraction class.
  1. How to convert other types to current type class in C++:

    • Description: Introduces the concept of converting other types to the current type class in C++ using conversion constructors.
    • Example Code:
      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;
      }
      
  2. Explicit type conversion using conversion constructors in C++:

    • Description: Illustrates explicit type conversion using conversion constructors, requiring the use of static_cast or other casting mechanisms.
    • Example Code:
      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;
      }
      
  3. Conversion constructor for string to custom class in C++:

    • Description: Illustrates how to implement a conversion constructor to convert a string to a custom class in C++.
    • Example Code:
      #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;
      }