c++基础---C++之运算符重载

来源:互联网 发布:数据库字段注释 编辑:程序博客网 时间:2024/04/30 18:32

C++之运算符重载(1)

参考自:http://www.cnblogs.com/CaiNiaoZJ/archive/2011/08/12/2136598.html

  在“C++之多态性与虚函数”一节中曾提到过,C++中运行时的多态性主要是通过虚函数来实现的,而编译时的多态性是由函数重载和运算符重载来实现的。这一系列我将主要讲解C++中有关运算符重载方面的内容。在每一个系列讲解之前,都会有它的一些基础知识需要我们去理解。而运算符重载的基础就是运算符重载函数。所以今天主要讲的是运算符重载函数。

  1.运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用域不同类型的数据导致不同行为的发生。比如

复制代码
1 int i;2 int i1=10,i2=10;3 i=i1+i2;4 std::cout<<"i1+i2="<<i<<std::endl;5 6 double d;7 double d1=20,d2=20;8 d=d1+d2;9 std::cout<<"d1+d2="<<d<<std::endl;
复制代码

在这个程序里"+"既完成两个整形数的加法运算,又完成了双精度型的加法运算。为什么同一个运算符"+"可以用于完成不同类型的数据的加法运算?这是因为C++针对预定义基本数据类型已经对"+"运算符做了适当的重载。在编译程序编译不同类型数据的加法表达式时,会自动调用相应类型的加法运算符重载函数。但是C++中所提供的预定义的基本数据类型毕竟是有限的,在解决一些实际的问题时,往往需要用户自定义数据类型。比如高中数学里所提到的复数:

复制代码
 1 class Complex //复数类 2 { 3 public: 4 double real;//实数 5 double imag;//虚数 6         Complex(double real=0,double imag=0) 7         { 8 this->real=real; 9 this->imag=imag;10         }11 }
复制代码

假如我们建立两个复数,并用"+"运算符让它们直接相加:

1 Complex com1(10,10),com2(20,20),sum;2 sum=com1+com2;

那么会提示没有与这些操作数匹配的 "+" 运算符的错误。这是因为Complex类类型不是预定义类型,系统没用对该类型的数据进行加法运算符函数的重载。C++就为运算符重载提供了一种方法,即运算符重载函数。其函数名字规定为operator后紧跟重载运算符。比如:operator+(),operator*()等。现在我们给上述程序声明一个加法运算符的重载函数用于完成复数的加法运算:

View Code
复制代码
 1 #include "stdafx.h" 2 #include <iostream> 3  4 class Complex //复数类 5 { 6 public: 7 double real;//实数 8 double imag;//虚数 9         Complex(double real=0,double imag=0)10         {11 this->real=real;12 this->imag=imag;13         }14 };15 16 Complex operator+(Complex com1,Complex com2)//运算符重载函数17 {18 return Complex(com1.real+com2.real,com1.imag+com2.imag);19 }20 21 int main()22 {23     Complex com1(10,10),com2(20,20),sum;24     sum=com1+com2;//或sum=operator+(com1,com2)25 26     std::cout<<"sum的实数部分为"<<sum.real<<std::endl;27     std::cout<<"sum的虚数部分为"<<sum.imag<<"i"<<std::endl;28 29 return0;30 }
复制代码

结果:

在上述示例代码中,调用运算符重载函数时,也可以以operator+(com1,com2)的形式来调用,实际上com1+com2在程序解释时也是转化成前者一样的形式。但是直接用com1+com2的形式更加符合人的书写习惯。

  2.上述示例中的运算符重载函数是不属于任何的类,是全局的函数。因为在Complex类(复数类)中的数据成员是公有的性质,所以运算符重载函数可以访问。但如果定义为私有的呢,那该怎么办。其实,在实际的运算符重载函数声明当中,要不定义其为要操作类的成员函数或类的友元函数

  (1)运算符重载函数作为类的友元函数的形式:

  class 类名

  {

    friend 返回类型 operator运算符(形参表);

  }

  类外定义格式:

  返回类型 operator运算符(参数表)

  {

    函数体

  }

友元函数重载双目运算符(有两个操作数,通常在运算符的左右两则),参数表中的个数为两个。若是重载单目运算符(只有一个操作数),则参数表中只有一参数。

i.友元函数重载双目运算符(+):

View Code
复制代码
 1 #include "stdafx.h" 2 #include <iostream> 3  4 class Complex //复数类 5 { 6 private://私有 7 double real;//实数 8 double imag;//虚数 9 public:10         Complex(double real=0,double imag=0)11         {12 this->real=real;13 this->imag=imag;14         }15         friend Complex operator+(Complex com1,Complex com2);//友元函数重载双目运算符+16 void showSum();17 };18 19 20 Complex operator+(Complex com1,Complex com2)//友元运算符重载函数21 {22 return Complex(com1.real+com2.real,com1.imag+com2.imag);23 }24 25 void Complex::showSum()26 {27     std::cout<<real;28 if(imag>0)29         std::cout<<"+";30 if(imag!=0)31         std::cout<<imag<<"i"<<std::endl;32 }33 34 int main()35 {36     Complex com1(10,10),com2(20,-20),sum;37     sum=com1+com2;//或sum=operator+(com1,com2)【实际上编译器就翻译成这样的】38     sum.showSum();//输出复数相加结果39 40 return0;41 }
复制代码

结果:

ii.友元函数重载单目运算符(++):

View Code
复制代码
 1 #include "stdafx.h" 2 #include <iostream> 3  4 class Point//坐标类 5 { 6 private: 7 int x; 8 int y; 9 public:10     Point(int x,int y)11     {12 this->x=x;13 this->y=y;14     }15     friend voidoperator++(Point& point);//友元函数重载单目运算符++16 void showPoint();17 };18 19 voidoperator++(Point& point)//友元运算符重载函数20 {21 ++point.x;22 ++point.y;23 }24 25 void Point::showPoint()26 {27     std::cout<<"("<<x<<","<<y<<")"<<std::endl;28 }29 30 int main()31 {32     Point point(10,10);33 ++point;//或operator++(point)【总之,重载为友元函数就看成是重载的是全局的函数或全局的运算符】34     point.showPoint();//输出坐标值35     36 return0;37 }
复制代码

结果:

运算符重载函数可以返回任何类型,甚至是void,但通常返回类型都与它所操作的类类型一样,这样可以使运算符使用在复杂的表达式中。比如把上述双目运算符重载函数示例代码中main()主函数里的com1+com2改为com1+com2+com2,那么结果又会不一样了。像赋值运算符=、下标运算符[]、函数调用运算符()等是不能被定义为友元运算符重载函数。同一个运算符可以定义多个运算符重载函数来进行不同的操作。

  (2)运算符重载函数作为类的成员函数的形式:

  class 类名

  {

    返回类型 operator 运算符(形参表);

  }

   类外定义格式:

  返回类型 类名:: operator 运算符(形参表)

  {

    函数体;

  }

对于成员函数重载运算符而言,双目运算符的参数表中仅有一个参数,而单目则无参数。同样的是重载,为什么和友元函数在参数的个数上会有所区别的。原因在于友元函数,没有this指针

i.成员函数重载双目运算符(+):

View Code
复制代码
 1 #include "stdafx.h" 2 #include <iostream> 3  4 class Complex //复数类 5 { 6 private://私有 7 double real;//实数 8 double imag;//虚数 9 public:10         Complex(double real=0,double imag=0)11         {12 this->real=real;13 this->imag=imag;14         }15         Complex operator+(Complex com1);//成员函数重载双目运算符+16 void showSum();17 };18 19 20 Complex Complex::operator+(Complex com1)21 {22 return Complex(real+com1.real,imag+com1.imag);23 }24 25 void Complex::showSum()26 {27     std::cout<<real;28 if(imag>0)29         std::cout<<"+";30 if(imag!=0)31         std::cout<<imag<<"i"<<std::endl;32 }33 34 35 int main()36 {37     Complex com1(10,10),com2(20,-20),sum;38     sum=com1+com2;//或sum=com1.operator+(com2)【书写形式上和友元一样,但编译器翻译成不一样的东西】39     sum.showSum();//输出复数相加结果40 return0;41 }
复制代码

对于双目运算符而言,运算符重载函数的形参中仅为一个参数,它作为运算符的右操作数(如com2对象),而当前对象作为左操作数(如:上述中的com1对象),它是通过this指针隐含传递给成员运算符重载函数的

ii.成员函数重载单目运算符(++):

View Code
复制代码
 1 #include "stdafx.h" 2 #include <iostream> 3  4  5 class Point//坐标类 6 { 7 private: 8 int x; 9 int y;10 public:11     Point(int x,int y)12     {13 this->x=x;14 this->y=y;15     }16 voidoperator++();//成员函数重载双目运算符++17 void showPoint();18 };19 20 21 void Point::operator++()22 {23 ++x;24 ++y;25 }26 27 28 void Point::showPoint()29 {30     std::cout<<"("<<x<<","<<y<<")"<<std::endl;31 }32 33 int main()34 {35     Point point(10,10);36 ++point;//或point.operator++()【总之,分析这类问题,就转化成编译器翻译成的语句去分析就行了】37     point.showPoint();//输出坐标值38     39 return0;40 }
复制代码

对于单目运算符而言,当前对象作为运算符的操作数

  在运算符重载运用时应该注意以下几个问题:(1)C++中只能对已有的C++运算符进行重载,不允许用户自己定义新的运算符;(2)C++中绝大部分的运算符可重载,除了成员访问运算符.,成员指针访问运算符.*,作用域运算符::,长度运算符sizeof以及条件运算符?:;(3)重载后不能改变运算符的操作对象(操作数)的个数。如:"+"是实现两个操作数的运算符,重载后仍然为双目运算符;(4)重载不能改变运算符原有的优先级;(5)重载不能改变运算符原有结合的特性。比如:z=x/y*a,执行时是先做左结合的运算x/y,重载后也是如此,不会变成先做右结合y*a;(6)运算符重载不能全部是C++中预定义的基本数据,这样做的目的是为了防止用户修改用于基本类型数据的运算符性质;(7)从上述的示例中可以看到双目运算符可以被重载为友元函数也可以重载为成员函数,但有一种情况,只能使用友元函数,是什么情况呢?我举个例子: 

复制代码
 1 class Complex //复数类 2 { 3 private://私有 4 double real;//实数 5 double imag;//虚数 6 public: 7         Complex(double real=0,double imag=0) 8         { 9 this->real=real;10 this->imag=imag;11         }12         Complex operator+(int x);13 };14 15 Complex Complex::operator+(int x)16 {17 return Complex(real+x,imag);18 }19 20 int main()21 {22     Complex com1(5,10),total;23     total=com1+5;24 25 return0;26 }
复制代码

如果我们把上述main()主函数实现部分里的total=com1+5改为total=5+com1;那么程序就会报错(没有与这些操作数匹配的 "+" 运算符),因为左操作数5不是该复数类的对象,不能调用相应的成员函数Complex operator+(int x),所以编译错误。但如果我们定义一下两个友元函数就能解决上述的问题:

  friend Complex operator+(Complex com1,int x);

  friend Complex operator+(int x,Complex com1);

【总之,翻译成编译器最终生成的那个语句来分析问题就容易解决了】

  3.最后还是一样,我将用一个示例来总结一下今天所讲的内容(开发工具:vs2010): 

View Code
复制代码
  1 #include "stdafx.h"  2 #include <iostream>  3   4 class Complex //复数类  5 {  6 private://私有  7 double real;//实数  8 double imag;//虚数  9 public: 10         Complex(double real=0,double imag=0) 11         { 12 this->real=real; 13 this->imag=imag; 14         } 15         Complex operator+(Complex com1);//成员函数重载双目运算符+ 16 //或friend Complex operator+(Complex com1,Complex com2);//友元函数重载双目运算符+ 17         friend Complex operator+(Complex com1,int x);//友元函数重载双目运算符+ 18 //或Complex operator+(int x); 19         friend Complex operator+(int x,Complex com1);//友元函数重载双目运算符+ 20 void showSum(); 21 }; 22  23  24 Complex Complex::operator+(Complex com1) 25 { 26 return Complex(real+com1.real,imag+com1.imag); 27 } 28  29 Complex operator+(Complex com1,int x)//左操作数类型为复数,右操作数的类型为整数 30 { 31 return Complex(com1.real+x,com1.imag); 32 } 33  34 Complex operator+(int x,Complex com1)//左操作数类型为整数,右操作数的类型为复数 35 { 36 return Complex(x+com1.real,com1.imag); 37 } 38  39 void Complex::showSum() 40 { 41     std::cout<<real; 42 if(imag>0) 43         std::cout<<"+"; 44 if(imag!=0) 45         std::cout<<imag<<"i"<<std::endl; 46 } 47  48 class Point//坐标类 49 { 50 private: 51 int x; 52 int y; 53 public: 54     Point(int x,int y) 55     { 56 this->x=x; 57 this->y=y; 58     } 59     friend voidoperator++(Point& point);//友元函数重载单目运算符++ 60     Point operator++();//成员函数重载双目运算符++ 61 void showPoint(); 62 }; 63  64 voidoperator++(Point& point)//友元运算符重载函数 65 { 66 ++point.x; 67 ++point.y; 68 } 69  70 Point Point::operator++() 71 { 72 ++x; 73 ++y; 74 return*this;//返回当前对象 75 } 76  77  78 void Point::showPoint() 79 { 80     std::cout<<"("<<x<<","<<y<<")"<<std::endl; 81 } 82  83 int main() 84 { 85 //两个复数相加 86     std::cout<<"两个复数相加:"<<std::endl; 87  88     Complex com1(10,10),com2(20,-20),sum; 89     sum=com1+com2;//或sum=com1.operator+(com2) 90     std::cout<<"(10+10i)+(20-20i)="; 91     sum.showSum();//输出复数相加结果 92  93 //三个复数相加 94     std::cout<<"三个复数相加:"<<std::endl; 95  96     sum=com1+com2+com2; 97     std::cout<<"(10+10i)+(20-20i)+(20-20i)="; 98     sum.showSum(); 99 100 //整数和复数相加101     std::cout<<"整数和复数相加:"<<std::endl;102     103     Complex com3(5,10),total;104     total=com3+5;//或total=operator+(com1,5);105     std::cout<<"(5+10i)+5=";106     total.showSum();107 108     total=5+com3;//或total=operator+(5,com1);109 //只能用友元函数来重载运算符110     std::cout<<"5+(5+10i)=";111     total.showSum();112 113 //单目运算符++重载114     std::cout<<"单目运算符++重载:"<<std::endl;115 116 //注意:下述实现部分不能只用一个++point会造成二义性117     Point point(10,10);118 //调用友元函数119 operator++(point);//或++point120     std::cout<<"调用友元函数:++(10,10)=";121     point.showPoint();//输出坐标值122 123 //调用成员函数124     point=point.operator++();//或++point;125     std::cout<<"调用成员函数:++(10,10)=";126     point.showPoint();127 128 return0;129 }
复制代码

结果:

 

 

C++之运算符重载(2)(接1)

转自:http://www.cnblogs.com/CaiNiaoZJ/archive/2011/08/13/2137033.html

  上一节主要讲解了C++里运算符重载函数,在看了单目运算符(++)重载的示例后,也许有些朋友会问这样的问题。++自增运算符在C或C++中既可以放在操作数之前,也可以放在操作数之后,但是前置和后置的作用又是完全不同的(q前置运算符:先加1,再赋值;后置运算符:先赋值,再加1)。那么要怎么重载它们,才可以有效的区分开来呢?今天我就来说说C++中是怎么处理前置运算符和后置运算符的重载的。以及介绍一下插入运算符(>>)和提取运算符(<<)的重载。

  1.在C++里编译器是根据运算符重载函数参数表里是否插入关键字int来区分前置还是后置运算。比如:

复制代码
 1 #include "stdafx.h" 2 #include <iostream> 3  4 class TDPoint//三维坐标 5 { 6 private: 7 int x; 8 int y; 9 int z;10 public:11         TDPoint(int x=0,int y=0,int z=0)12         {13 this->x=x;14 this->y=y;15 this->z=z;16         }17         TDPoint operator++();//成员函数重载前置运算符++18         TDPoint operator++(int);//成员函数重载后置运算符++19         friend TDPoint operator++(TDPoint& point);//友元函数重载前置运算符++20         friend TDPoint operator++(TDPoint& point,int);//友元函数重载后置运算符++21 void showPoint();22 };23 24 TDPoint TDPoint::operator++()25 {26 ++this->x;27 ++this->y;28 ++this->z;29 return*this;//返回自增后的对象30 }31 32 TDPoint TDPoint::operator++(int)33 {34     TDPoint point(*this);35 this->x++;36 this->y++;37 this->z++;38 return point;//返回自增前的对象39 }40 41 TDPoint operator++(TDPoint& point)42 {43 ++point.x;44 ++point.y;45 ++point.z;46 return point;//返回自增后的对象47 }48 49 TDPoint operator++(TDPoint& point,int)50 {51     TDPoint point1(point);52     point.x++;53     point.y++;54     point.z++;55 return point1;//返回自增前的对象56 }57 58 void TDPoint::showPoint()59 {60     std::cout<<"("<<x<<","<<y<<","<<z<<")"<<std::endl;61 }62 63 int main()64 {65     TDPoint point(1,1,1);66     point.operator++();//或++point67     point.showPoint();//前置++运算结果68 69     point=point.operator++(0);//或point=point++70     point.showPoint();//后置++运算结果71 72 operator++(point);//或++point;73     point.showPoint();//前置++运算结果74 75     point=operator++(point,0);//或point=point++;76     point.showPoint();//后置++运算结果77 78 return0;79 }
复制代码

结果:

从示例代码里可以清楚的看出,后置运算符重载函数比前置运算符重载函数多了一个int类型的参数,这个参数只是为了区别前置和后置运算符,此外没有任何作用。所以在调用后置运算符重载函数时,int类型的实参可以取任意值

  2.在C++中,操作符"<<"和">>"被定义为左位移运算符和右位移运算符。由于在iostream头文件中对它们进行了重载,使得它们可以用基本数据的输出和输入。

复制代码
#include "stdafx.h"#include <iostream>int main(){    int a=10;    std::cout<<"a="<<a<<std::endl;//运算符"<<"重载后用于输出    a=a>>2;//右移运算符    std::cout<<"右移2位:a="<<a<<std::endl;    std::cout<<"请输入一个整数a:";    std::cin>>a;//运算符">>"重载后用于输入    a=a<<2;//左移运算符    std::cout<<"左移2位:a="<<a<<std::endl;    return0;}
复制代码

结果:

插入运算符"<<"是双目运算符,左操作数为输出流类ostream的对象,右操作数为系统预定义的基本类型数据。头文件iostrem对其重载的函数原型为ostream& operator<<(ostream& ,类型名);类型名就是指基本类型数据。但如果要输出用户自定义的类型数据的话,就需要重载操作符"<<",因为该操作符的左操作数一定为ostream类的对象,所以插入运算符"<<"只能是类的友元函数或普通函数,不能是其他类的成员函数。一般定义格式:

  ostream& operator<<(ostream& ,自定义类名&);

提取运算符">>"也是如此,左操作数为istream类的对象,右操作数为基本类型数据。头文件iostrem对其重载的函数原型为istream& operator>>(istream& ,类型名);提取运算符也不能作为其他类的成员函数,可以是友元函数或普通函数。它的一般定义格式为:

  istream& operator>>(istream& ,自定义类名&);

我还是用上一节用的Complex类(复数类)来举例:

复制代码
#include "stdafx.h"#include <iostream>class Complex //复数类{    private://私有double real;//实数double imag;//虚数public:        Complex(double real=0,double imag=0)        {            this->real=real;            this->imag=imag;        }        friend std::ostream&operator<<(std::ostream& o,Complex& com);//友元函数重载提取运算符"<<"        friend std::istream&operator>>(std::istream& i,Complex& com);//友元函数重载插入运算符">>"};std::ostream&operator<<(std::ostream& o,Complex& com){    std::cout<<"输入的复数:";    o<<com.real;    if(com.imag>0)        o<<"+";    if(com.imag!=0)        o<<com.imag<<"i"<<std::endl;    return o;}std::istream&operator>>(std::istream& i,Complex& com){    std::cout<<"请输入一个复数:"<<std::endl;    std::cout<<"real(实数):";    i>>com.real;    std::cout<<"imag(虚数):";    i>>com.imag;    return i;}int main(){    Complex com;    std::cin>>com;    std::cout<<com;    return0;}
复制代码

结果:

 
 
 

C++之运算符重载(3)--(类型转换运算符重载)不同数据间的类型转换

转自:http://www.cnblogs.com/CaiNiaoZJ/archive/2011/08/14/2137790.html

  我们在使用重载的运算符时,往往需要在自定义数据类型和系统预定义的数据类型之间进行转换,或者需要在不同的自定义数据类型之间进行转换。今天就来讲讲C++中数据类型的转换。

  1.对于系统的预定义基本类型数据,C++提供了两种类型转换方式:隐式类型转换和显式类型转换。

复制代码
1 int a=5,sum;2 double b=5.55;3     sum=a+b;//-------(1)4     std::cout<<"隐式转换:a+b="<<sum<<std::endl;5 6     sum=(int)(a+b);//-------(2)7     sum=int(a+b);//-------(3)8     std::cout<<"显式转换:a+b="<<sum<<std::endl;
复制代码

上述代码中的(1)就是含有隐式类型转换的表达式,在进行"a+b"时,编译系统先将a的值5转换为双精度double,然后和b相加得到10.55,在向整形变量sum赋值时,将10.55转换为整形数10,赋值为变量sum。这种转换是C++编译系统自动完成,不需要用户去干预。而上例中的(2)和(3)中则涉及到了显式类型转换,它们都是把a+b所得结果的值,强制转化为整形数。只是(2)式是C语言中用到的形式:(类型名)表达式,而(3)式是C++中的采用的形式:类型名(表达式);

  2.那么对于用户自定义的类类型而言,有该如何去实现它们和其他数据类型之间的转换呢,C++中提供了来那个中方法:(1)通过转换构造函数进行类型转换;(2)通过类型转换函数进行类型转换;

  毫无疑问转换构造函数就是构造函数的一种,只不过它拥有类型转换的作用罢了。是否记得在C++之运算重载符(1)中两个复数(sum=com1+com2)相加的实例,现在如果我想要实现sum=com1+5.5,那该怎么办,也许你首先会想到再定义一个关于复数加双精度的运算符重载函数。这样做的确可以。另外我们还可以定义一个转换构造函数来解决上述的问题。我们对Comlex类(复数类)进行这样改造:

复制代码
 1 #include "stdafx.h" 2 #include <iostream> 3  4 class Complex //复数类 5 { 6 private://私有 7 double real;//实数 8 double imag;//虚数 9 public:10         Complex(double real,double imag)11         {12 this->real=real;13 this->imag=imag;14         }15         Complex(double d=0.0)//转换构造函数16         {17             real=d;//实数取double类型的值18             imag=0;//虚数取019         }20         Complex operator+(Complex com1);//或friend Complex operator+(Complex com1,Complex com2);21 void showComplex();22 };23 24 Complex Complex::operator+(Complex com1)25 {26 return Complex(real+com1.real,imag+com1.imag);27 }28 29 void Complex::showComplex()30 {31     std::cout<<real;32 if(imag>0)33         std::cout<<"+";34 if(imag!=0)35         std::cout<<imag<<"i"<<std::endl;36 }37 38 int main()39 {40 41     Complex com(10,10),sum;42     sum=com+Complex(5.5);//Complex(5.5)把双精度数5.5转换为复数5.5+0i43     sum.showComplex();//输出运算结果44 45 return0;46 }
复制代码

结果:

上述代码在执行Complex(5.5)时,调用了转换构造函数,将double类型的5.5转换为无名的Complex类的临时对象(5.5+0i),然后执行两个Complex类(复数类)对象相加的运算符重载函数。所以说一般的转换构造函数的定义形式:

  类名(待转换类型)

  {

    函数体;

  }

转换构造函数不仅可以将预定义的数据类型转换为自定义类的对象,也可以将另一个类的对象转换成转换构造函数所在的类的对象。

  转换构造函数可以把预定义类型转化为自定义类的对象,但是却不能把类的对象转换为基本数据类型。比如:不能将Complex类(复数类)的对象转换成double类型数据。于是在C++中就用类型转换函数来解决这个问题。定义类型转换函数一般形式:

  operator 目标类型()

  {

    ...

    return 目标类型的数据;

  }

目标类型是所要转化成的类型名,既可以是预定义及基本类型也可以是自定义类型。类型转换函数的函数名(operator 目标类型)前不能指定返回类型,且没有参数。但在函数体最后一条语句一般为return语句,返回的是目标类型的数据。现在我们对Complex类做类似改造:

复制代码
 1 #include "stdafx.h" 2 #include <iostream> 3  4 class Complex //复数类 5 { 6 private://私有 7 double real;//实数 8 double imag;//虚数 9 public:10         Complex(double real,double imag)11         {12 this->real=real;13 this->imag=imag;14         }15         Complex(double d=0.0)//转换构造函数16         {17             real=d;//实数取double类型的值18             imag=0;//虚数取019         }20         21         Complex operator+(Complex com1);//或friend Complex operator+(Complex com1,Complex com2);22 operatordouble();//声明类型转换函数23 void showComplex();24 };25 26 Complex Complex::operator+(Complex com1)27 {28 return Complex(real+com1.real,imag+com1.imag);29 }30 31 Complex::operatordouble()//定义类型转换函数32 {33 return real;//返回实数部分34 }35 36 void Complex::showComplex()37 {38     std::cout<<real;39 if(imag>0)40         std::cout<<"+";41 if(imag!=0)42         std::cout<<imag<<"i"<<std::endl;43 }44 45 46 47 int main()48 {49 50     Complex com(10,10),sum;51     sum=com+Complex(5.5);//Complex(5.5)把双精度数5.5转换为复数5.5+0i52     sum.showComplex();//输出运算结果53 54 double total;55     total=double(com)+5.5;//double(com)把复数(10+10i)转换为双精度数10.056     std::cout<<"把Complex类对象转化为double类型与5.5相加为:"<<total;57 58 return0;59 }
复制代码

结果:

  3.最后对类型转换函数做几点补充:(1)类型转换函数只能作为类的成员函数,不能定义为类的友元函数;(2)类型转换函数中必须有return语句,即必须送回目标类型的数据作为函数返回值;(3)一个类可以定义多个类型转换函数,C++编译器会根据函数名来自动调用相应的类型转换函数函数;

 

原创粉丝点击