[C++] Conversion Function

Keywords: Programming C

1. For the predefined basic data types of the system, C++ provides two types of type conversion: implicit type conversion and display type conversion.

int a=5,sum;
double b=5.55;
sum=a+b;    //Implicit Type Conversion: The compiler first converts the value of a from int to double, and then adds it to b to get 10.55. When assigning a value to an integer sum, it converts 10.55 to an integer 10 and assigns it to a variable sum.
cout<<"Implicit Conversion: a+b="<<sum<<endl;

//Display type conversion
sum=(int)(a+b);    //Form of C Language
sun=int(a+b);      //The form of C++
cout<<"Display conversion: a+b="<<sum<<endl;

2. C++ provides the following two methods for converting user-defined class types to other data types:

(1) non-ecplicit-one-argument ctor

(2) conversion function (type conversion function)

Take the complex class as an example to describe the above two functions:

//Definition of Complex Class

#include <iostream>
using namespace std;

class Complex
{
private:
	double real;
	double imag;
public:

	Complex(double real, double imag)
	{
		this->real = real;
		this->imag = imag;
	}
	
	Complex(double real=0)    //Conversion constructor non-explicit-one-argument ctor
	{
		real = real;
		imag = 0;
	}
	
	Complex operator+(Complex com1);
	void showComplex();
};

Complex Complex::operator+(Complex com1)
{
	return Complex(real + com1.real, imag + com1.imag);
}

void Complex::showComplex()
{
	cout << real;
	if (imag>0)
		cout << "+";
	if (imag != 0)
		cout << imag << "i" << endl;
}

int main()
{
	Complex com(10, 10), sum;
	sum = com + 5.5;    //5.5 calls Complex(5.5) to generate temporary objects, and then adds them to com
	sum.showComplex();	//The output is 15.5+10i
	while (1);
	return 0;
}

If the explicit keyword is added before the transformation constructor, 5.5 will not be implicitly converted to the Complex class, it must be Complex(5.5), the code is as follows

//Definition of Complex Class

#include <iostream>
using namespace std;

class Complex
{
private:
	double real;
	double imag;
public:

	Complex(double real, double imag)
	{
		this->real = real;
		this->imag = imag;
	}
	
	explicit Complex(double r=0.0)    //Add explicit keyword
	{
		real = r;
		imag = 0;
	}
	
	Complex operator+(Complex com1);
	void showComplex();
};

Complex Complex::operator+(Complex com1)
{
	return Complex(real + com1.real, imag + com1.imag);
}

void Complex::showComplex()
{
	cout << real;
	if (imag>0)
		cout << "+";
	if (imag != 0)
		cout << imag << "i" << endl;
}

int main()
{
	Complex com(10, 10), sum;
	sum = com + Complex(5.5);    //Correspondingly, the transformation constructor must be explicitly invoked with Complex
	sum.showComplex();	//The output is 15.5+10i

	while (1);
	return 0;
}

As can be seen from the above, the form of the transformation constructor is as follows:

Class name (type to be converted)

  {

Function body;

  }

Conversion constructors can transform not only predefined data types into objects of custom types, but also objects of another class into objects of the class where the conversion constructor is located. But the conversion constructor can not convert the cumulative object into basic data type, such as the object of Complex class into double type data, so type conversion function is introduced in C++ to solve this problem. The general form of the type conversion function is as follows:

operator target type ()

    {

         ...

return target type data;

    }

The target type is the name of the type to be converted. It can be either predefined or basic type, or custom type. It should be noted that:

a. The return type cannot be specified before the function name of the type conversion function (operator target type)

b. No parameters

c. The last statement in the body of a function is generally a return statement, which returns data of the target type.

See the relevant examples below:

#include <iostream>  

class Complex //Complex class  
{
private://private  
	double real;//real number  
	double imag;//Imaginary number  
public:
	Complex(double real, double imag)
	{
		this->real = real;
		this->imag = imag;
	}
	Complex(double d = 0.0)//Conversion constructor  
	{
		real = d;//Real number takes the value of double type  
		imag = 0;//Imaginary number 0  
	}
	Complex operator+(Complex com1);//Or friend Complex operator+(Complex com1,Complex com2);  
	void showComplex();
	operator double();
};

Complex Complex::operator+(Complex com1)
{
	return Complex(real + com1.real, imag + com1.imag);
}

void Complex::showComplex()
{
	std::cout << real;
	if (imag>0)
		std::cout << "+";
	if (imag != 0)
		std::cout << imag << "i" << std::endl;
}

Complex::operator double()
{
	return real;
}

int main()
{
	Complex com(10, 10);
	double total1, total2;
	total1 = double(com) + 5.5;//double(com) converts complex number (10+10i) to double precision number 10.0  
	total2 = 5.5 + com; // Writing total2 = com + 5.5 is wrong   
	std::cout << "hold Complex Class explicit object transformation double Type and 5.5 Add up to:" << total1 << std::endl;
	std::cout << "hold Complex Implicit transformation of class objects double Type and 5.5 Add up to:" << total2 << std::endl;
	while (1);
	return 0;
}

Note that total2=com+5.5; errors will be reported because of ambiguity, whether com uses conversion function to convert to double type to add double definition, or 5.5 uses conversion constructor to convert to temporary variable of Complex to add the definition of Complex.

Note: Reference Blog https://www.cnblogs.com/forcheryl/p/3955407.html

 

Posted by audiodef on Sat, 26 Jan 2019 18:54:14 -0800