C + + language Basics (22)-conversion constructors and type conversion functions

Source: Internet
Author: User

I. Conversion constructors

Converting other types to the current class type requires the use of a transform constructor (Conversion constructor). The transformation constructor is also a constructor that follows the general rules of the constructor. The conversion constructor has only one parameter.

#include <iostream>using namespacestd;//Plural classclasscomplex{ Public: Complex (): M_real (0.0), M_imag (0.0) {} Complex (DoubleRealDoubleimag): M_real (Real), M_imag (imag) {} Complex (DoubleReal): M_real (Real), M_imag (0.0){ }//Conversion Constructors Public: Friend Ostream&operator<< (Ostream & out, Complex &c);//friend functionPrivate:    DoubleM_real;//Real Department    DoubleM_imag;//Imaginary Part};//Overloading >> OperatorsOstream &operator<< (Ostream & out, Complex &c) {     out<< C.m_real <<" + "<< C.m_imag <<"I";; return  out;}intMain () {Complex A (10.0,20.0); cout<<a<<Endl; A=25.5;//Call conversion constructorcout<<a<<Endl; return 0;}

Operation Result:

Ten + 20i
25.5 + 0i

Two. type conversion function
#include <iostream>using namespacestd;//Plural classclasscomplex{ Public: Complex (): M_real (0.0), M_imag (0.0) {} Complex (DoubleRealDoubleimag): M_real (Real), M_imag (imag) {} Public: Friend Ostream&operator<< (Ostream & out, Complex &c); Friend Complexoperator+(ConstComplex &c1,ConstComplex &C2); operator Double()Const{returnM_real; }//type conversion functionPrivate:    DoubleM_real;//Real Department    DoubleM_imag;//Imaginary Part};//Overloading >> OperatorsOstream &operator<< (Ostream & out, Complex &c) {     out<< C.m_real <<" + "<< C.m_imag <<"I";; return  out;}//Overloaded + operatorsComplexoperator+(ConstComplex &c1,ConstComplex &C2)    {Complex C; C.m_real= C1.m_real +C2.m_real; C.m_imag= C1.m_imag +C2.m_imag; returnC;}intMain () {Complex C1 (24.6, -); Doublef = C1;//equivalent to double f = complex::operator double (&c1);cout<<"f ="<<f<<Endl; F=12.5+ C1 +6;//equivalent to F = 12.5 + Complex::operator double (&C1) + 6;cout<<"f ="<<f<<Endl; intn = Complex (43.2,9.3);//convert first to double, then to intcout<<"n ="<<n<<Endl; return 0;}

Operation Result:

f = 24.6
F = 43.1
n = 43

Note: It is best not to use both the transform constructor and the type conversion function, as this results in semantic semantics of two.

C + + language Basics (22)-conversion constructors and type conversion functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.