几个典型运算符重载C四

上传人:人*** 文档编号:570774126 上传时间:2024-08-06 格式:PPT 页数:56 大小:1.01MB
返回 下载 相关 举报
几个典型运算符重载C四_第1页
第1页 / 共56页
几个典型运算符重载C四_第2页
第2页 / 共56页
几个典型运算符重载C四_第3页
第3页 / 共56页
几个典型运算符重载C四_第4页
第4页 / 共56页
几个典型运算符重载C四_第5页
第5页 / 共56页
点击查看更多>>
资源描述

《几个典型运算符重载C四》由会员分享,可在线阅读,更多相关《几个典型运算符重载C四(56页珍藏版)》请在金锄头文库上搜索。

1、2024/8/617.3 7.3 几个典型运算符重载几个典型运算符重载 2024/8/627.3 7.3 7.3 7.3 几个典型运算符重载几个典型运算符重载几个典型运算符重载几个典型运算符重载 数学类中常用的几个运算重载的特点和应用 6.3 几个典型运算符重载2024/8/637.3.1 7.3.1 重载重载 + 与与 - 设A Aobject ;运算符 + + 和 - - 有两种方式:前置方式:前置方式: +Aobject -Aobject后置方式:后置方式: Aobject + Aobject - 成员函数成员函数 重载A : A operator+ () ; 解释为:Aobject .

2、 operator + + ( ) ; 友员函数友员函数 重载friend A operator+ (A &) ; 解释为: operator + + ( Aobject ) ; 成员函数成员函数 重载A : A operator+ (int) ; 解释为: Aobject . operator + + ( 0 ) ; 友员函数友员函数 重载:friend A operator+ (A &, int) ; 解释为: operator+(Aobject, 0) 伪参数6.3.1 重载+与-2024/8/64成员函数重载成员函数重载+#includeclass Increase public :

3、Increase ( ) value=0; void display( ) const coutvaluen; ; Increase operator + ( ) ; / 前置 Increase operator + ( int ) ; / 后置 private: unsigned value ;Increase Increase : operator + ( ) value + ; return *this ; Increase Increase : operator + ( int ) Increase temp; temp.value = value + ; return temp; v

4、oid main( ) Increase a , b , n ; for ( int i = 0 ; i 10 ; i + ) a = n + ; cout n= ; n.display( ) ; cout a= ; a.display( ) ; for ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; 6.3.1 重载+与-2024/8/65成员函数重载成员函数重载+#includeclass Increase public : Increase ( ) value=0; vo

5、id display( ) const coutvaluen; ; Increase operator + ( ) ; / 前置 Increase operator + ( int ) ; / 后置 private: unsigned value ;Increase Increase : operator + ( ) value + ; return *this ; Increase Increase : operator + ( int ) Increase temp; temp.value = value + ; return temp; void main( ) Increase a ,

6、 b , n ; for ( int i = 0 ; i 10 ; i + ) a = n + ; cout n= ; n.display( ) ; cout a= ; a.display( ) ; for ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; Increase operator + ( ) ;Increase Increase : operator + ( ) value + ; return * this ; + n ;预定义版本6.3.1 重载+与-2024/8

7、/66成员函数重载成员函数重载+#includeclass Increase public : Increase ( ) value=0; void display( ) const coutvaluen; ; Increase operator + ( ) ; / 前置 Increase operator + ( int ) ; / 后置 private: unsigned value ;Increase Increase : operator + ( ) value + ; return *this ; Increase Increase : operator + ( int ) Incr

8、ease temp; temp.value = value + ; return temp; void main( ) Increase a , b , n ; for ( int i = 0 ; i 10 ; i + ) a = n + ; cout n= ; n.display( ) ; cout a= ; a.display( ) ; for ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; Increase operator + ( ) ;Increase Increas

9、e : operator + ( ) value + ; return * this ; + n ;重载版本6.3.1 重载+与-2024/8/67成员函数重载成员函数重载+#includeclass Increase public : Increase ( ) value=0; void display( ) const coutvaluen; ; Increase operator + ( ) ; / 前置 Increase operator + ( int ) ; / 后置 private: unsigned value ;Increase Increase : operator + (

10、 ) value + ; return *this ; Increase Increase : operator + ( int ) Increase temp; temp.value = value + ; return temp; void main( ) Increase a , b , n ; for ( int i = 0 ; i 10 ; i + ) a = n + ; cout n= ; n.display( ) ; cout a= ; a.display( ) ; for ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display(

11、 ) ; cout b= ; b.display( ) ; Increase operator + ( int ) ;Increase Increase : operator + ( int ) Increase temp; temp.value = value + ; return temp; n + ;预定义版本6.3.1 重载+与-2024/8/68成员函数重载成员函数重载+#includeclass Increase public : Increase ( ) value=0; void display( ) const coutvaluen; ; Increase operator

12、+ ( ) ; / 前置 Increase operator + ( int ) ; / 后置 private: unsigned value ;Increase Increase : operator + ( ) value + ; return *this ; Increase Increase : operator + ( int ) Increase temp; temp.value = value + ; return temp; void main( ) Increase a , b , n ; for ( int i = 0 ; i 10 ; i + ) a = n + ; co

13、ut n= ; n.display( ) ; cout a= ; a.display( ) ; for ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; Increase operator + ( int ) ;Increase Increase : operator + ( int ) Increase temp; temp.value = value + ; return temp; n + ;重载版本6.3.1 重载+与-2024/8/69成员函数重载成员函数重载+#inc

14、ludeclass Increase public : Increase ( ) value=0; void display( ) const coutvaluen; ; Increase operator + ( ) ; / 前置 Increase operator + ( int ) ; / 后置 private: unsigned value ;Increase Increase : operator + ( ) value + ; return *this ; Increase Increase : operator + ( int ) Increase temp; temp.valu

15、e = value + ; return temp; void main( ) Increase a , b , n ; for ( int i = 0 ; i 10 ; i + ) a = n + ; cout n= ; n.display( ) ; cout a= ; a.display( ) ; for ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; 6.3.1 重载+与-2024/8/610友员函数重载友员函数重载+#includeclass Increase publ

16、ic : Increase ( ) value=0; void display( ) const coutvaluen; ; friend Increase operator + ( Increase & ) ; / 前置 friend Increase operator + ( Increase &, int ) ; / 后置 private: unsigned value ;Increase operator + ( Increase & a ) a.value + ; return a ; Increase operator + ( Increase & a, int ) Increas

17、e temp(a); a.value + ; return temp; void main( ) Increase a , b , n ; for ( int i = 0 ; i 10 ; i + ) a = n + ; cout n= ; n.display( ) ; cout a= ; a.display( ) ; for ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; 6.3.1 重载+与-2024/8/611友员函数重载友员函数重载+#includeclass Incr

18、ease public : Increase ( ) value=0; void display( ) const coutvaluen; ; friend Increase operator + ( Increase & ) ; / 前置 friend Increase operator + ( Increase &, int ) ; / 后置 private: unsigned value ;Increase operator + ( Increase & a ) a.value + ; return a ; Increase operator + ( Increase & a, int

19、) Increase temp(a); a.value + ; return temp; void main( ) Increase a , b , n ; for ( int i = 0 ; i 10 ; i + ) a = n + ; cout n= ; n.display( ) ; cout a= ; a.display( ) ; for ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; friend Increase operator + ( Increase & ) ;

20、Increase operator + ( Increase & a ) a.value + ; return a ; + n ;通过引用参数操作对象6.3.1 重载+与-2024/8/612友员函数重载友员函数重载+#includeclass Increase public : Increase ( ) value=0; void display( ) const coutvaluen; ; friend Increase operator + ( Increase & ) ; / 前置 friend Increase operator + ( Increase &, int ) ; / 后

21、置 private: unsigned value ;Increase operator + ( Increase & a ) a.value + ; return a ; Increase operator + ( Increase & a, int ) Increase temp(a); a.value + ; return temp; void main( ) Increase a , b , n ; for ( int i = 0 ; i 10 ; i + ) a = n + ; cout n= ; n.display( ) ; cout a= ; a.display( ) ; for

22、 ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; friend Increase operator + ( Increase &, int ) ;Increase operator + ( Increase & a, int ) Increase temp(a); a.value + ; return temp; n + ;复制构造局部对象6.3.1 重载+与-2024/8/613友员函数重载友员函数重载+#includeclass Increase public : Incr

23、ease ( ) value=0; void display( ) const coutvaluen; ; friend Increase operator + ( Increase & ) ; / 前置 friend Increase operator + ( Increase &, int ) ; / 后置 private: unsigned value ;Increase operator + ( Increase & a ) a.value + ; return a ; Increase operator + ( Increase & a, int ) Increase temp(a)

24、; a.value + ; return temp; void main( ) Increase a , b , n ; for ( int i = 0 ; i 10 ; i + ) a = n + ; cout n= ; n.display( ) ; cout a= ; a.display( ) ; for ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; friend Increase operator + ( Increase &, int ) ;Increase oper

25、ator + ( Increase & a, int ) Increase temp(a); a.value + ; return temp; n + ;伪参数6.3.1 重载+与-2024/8/614友员函数重载友员函数重载+#includeclass Increase public : Increase ( ) value=0; void display( ) const coutvaluen; ; friend Increase operator + ( Increase & ) ; / 前置 friend Increase operator + ( Increase &, int )

26、; / 后置 private: unsigned value ;Increase operator + ( Increase & a ) a.value + ; return a ; Increase operator + ( Increase & a, int ) Increase temp(a); a.value + ; return temp; void main( ) Increase a , b , n ; for ( int i = 0 ; i 10 ; i + ) a = n + ; cout n= ; n.display( ) ; cout a= ; a.display( )

27、; for ( i = 0 ; i 10 ; i + ) b = + n ; cout n= ; n.display( ) ; cout b= ; b.display( ) ; 6.3.1 重载+与-2024/8/6157.3.2 7.3.2 重载赋值运算符重载赋值运算符 赋值运算符重载用于对象数据的复制 operator= 必须重载为成员函数 重载函数原型为:类型 & 类名 : operator= ( const 类名 & ) ; 6.3.2 重载赋值运算符2024/8/616#include#includeclass Name public : Name ( char *pN ) ; Na

28、me( const Name & ) ; /复制构造函数 Name& operator=( const Name& ) ; / 重载赋值算符 Name() ; protected : char *pName ; int size ; ;void main() Name Obj1( ZhangSan ) ; Name Obj2 = Obj1 ;/ 调用复制构造函数 Name Obj3( NoName ) ; Obj3 = Obj2 = Obj1 ;/ 调用重载赋值算符函数 定义定义Name类的重载赋值函数类的重载赋值函数 6.3.2 重载赋值运算符2024/8/617#include#inclu

29、declass Name public : Name ( char *pN ) ; Name( const Name & ) ; /复制构造函数 Name& operator=( const Name& ) ; / 重载赋值算符 Name() ; protected : char *pName ; int size ; ;void main() Name Obj1( ZhangSan ) ; Name Obj2 = Obj1 ;/ 调用复制构造函数 Name Obj3( NoName ) ; Obj3 = Obj2 = Obj1 ;/ 调用重载赋值算符函数 定义定义Name类的重载赋值函数类的

30、重载赋值函数 初始化对象时调用复制构造函数6.3.2 重载赋值运算符2024/8/618#include#includeclass Name public : Name ( char *pN ) ; Name( const Name & ) ; /复制构造函数 Name& operator=( const Name& ) ; / 重载赋值算符 Name() ; protected : char *pName ; int size ; ;void main() Name Obj1( ZhangSan ) ; Name Obj2 = Obj1 ;/ 调用复制构造函数 Name Obj3( NoNa

31、me ) ; Obj3 = Obj2 = Obj1 ;/ 调用重载赋值算符函数 定义定义Name类的重载赋值函数类的重载赋值函数 修改对象时调用重载赋值运算符函数6.3.2 重载赋值运算符2024/8/619定义定义Name类的重载赋值函数类的重载赋值函数 Name:Name ( char *pN ) cout Constructing pN endl ; pName = new char strlen( pN ) + 1 ; if( pName != 0 ) strcpy( pName,pN ) ; size = strlen( pN ) ; Name:Name( const Name &

32、Obj ) /复制构造函数 cout Copying Obj.pName into its own blockn; pName = new charstrlen( Obj.pName ) + 1 ; if ( pName != 0 ) strcpy( pName, Obj.pName ) ; size = Obj.size;Name & Name:operator= ( const Name & Obj ) / 重载赋值算符 delete pName ; pName = new char strlen( Obj.pName ) + 1 ; if ( pName != 0 ) strcpy( p

33、Name , Obj.pName ) ; size = Obj.size ; return *this ;Name: Name() cout Destructing pName sizeLiHaiObj6Obj.size6.3.2 重载赋值运算符2024/8/620Name:Name ( char *pN ) cout Constructing pN endl ; pName = new char strlen( pN ) + 1 ; if( pName != 0 ) strcpy( pName,pN ) ; size = strlen( pN ) ; Name:Name( const Nam

34、e & Obj ) /复制构造函数 cout Copying Obj.pName into its own blockn; pName = new charstrlen( Obj.pName ) + 1 ; if ( pName != 0 ) strcpy( pName, Obj.pName ) ; size = Obj.size;Name & Name:operator= ( const Name & Obj ) / 重载赋值算符 delete pName ; pName = new char strlen( Obj.pName ) + 1 ; if ( pName != 0 ) strcp

35、y( pName , Obj.pName ) ; size = Obj.size ; return *this ;Name: Name() cout Destructing pName size6Obj.size6.3.2 重载赋值运算符2024/8/621Name:Name ( char *pN ) cout Constructing pN endl ; pName = new char strlen( pN ) + 1 ; if( pName != 0 ) strcpy( pName,pN ) ; size = strlen( pN ) ; Name:Name( const Name &

36、Obj ) /复制构造函数 cout Copying Obj.pName into its own blockn; pName = new charstrlen( Obj.pName ) + 1 ; if ( pName != 0 ) strcpy( pName, Obj.pName ) ; size = Obj.size;Name & Name:operator= ( const Name & Obj ) / 重载赋值算符 delete pName ; pName = new char strlen( Obj.pName ) + 1 ; if ( pName != 0 ) strcpy( p

37、Name , Obj.pName ) ; size = Obj.size ; return *this ;Name: Name() cout Destructing pName size6Obj.size6.3.2 重载赋值运算符2024/8/622Name:Name ( char *pN ) cout Constructing pN endl ; pName = new char strlen( pN ) + 1 ; if( pName != 0 ) strcpy( pName,pN ) ; size = strlen( pN ) ; Name:Name( const Name & Obj

38、) /复制构造函数 cout Copying Obj.pName into its own blockn; pName = new charstrlen( Obj.pName ) + 1 ; if ( pName != 0 ) strcpy( pName, Obj.pName ) ; size = Obj.size;Name & Name:operator= ( const Name & Obj ) / 重载赋值算符 delete pName ; pName = new char strlen( Obj.pName ) + 1 ; if ( pName != 0 ) strcpy( pName

39、 , Obj.pName ) ; size = Obj.size ; return *this ;Name: Name() cout Destructing pName size6Obj.size6.3.2 重载赋值运算符2024/8/623Name:Name ( char *pN ) cout Constructing pN endl ; pName = new char strlen( pN ) + 1 ; if( pName != 0 ) strcpy( pName,pN ) ; size = strlen( pN ) ; Name:Name( const Name & Obj ) /复

40、制构造函数 cout Copying Obj.pName into its own blockn; pName = new charstrlen( Obj.pName ) + 1 ; if ( pName != 0 ) strcpy( pName, Obj.pName ) ; size = Obj.size;Name & Name:operator= ( const Name & Obj ) / 重载赋值算符 delete pName ; pName = new char strlen( Obj.pName ) + 1 ; if ( pName != 0 ) strcpy( pName , O

41、bj.pName ) ; size = Obj.size ; return *this ;Name: Name() cout Destructing pName size6Obj.size66.3.2 重载赋值运算符2024/8/624Name:Name ( char *pN ) cout Constructing pN endl ; pName = new char strlen( pN ) + 1 ; if( pName != 0 ) strcpy( pName,pN ) ; size = strlen( pN ) ; Name:Name( const Name & Obj ) /复制构造

42、函数 cout Copying Obj.pName into its own blockn; pName = new charstrlen( Obj.pName ) + 1 ; if ( pName != 0 ) strcpy( pName, Obj.pName ) ; size = Obj.size;Name & Name:operator= ( const Name & Obj ) / 重载赋值算符 delete pName ; pName = new char strlen( Obj.pName ) + 1 ; if ( pName != 0 ) strcpy( pName , Obj.

43、pName ) ; size = Obj.size ; return *this ;Name: Name() cout Destructing pName size6Obj.size6.3.2 重载赋值运算符2024/8/625#include#includeclass Name public : Name ( char *pN ) ; Name( const Name & ) ; /复制构造函数 Name& operator=( const Name& ) ; / 重载赋值算符 Name() ; protected : char *pName ; int size ; ;void main(

44、) Name Obj1( ZhangSan ) ; Name Obj2 = Obj1 ;/ 调用复制构造函数 Name Obj3( NoName ) ; Obj3 = Obj2 = Obj1 ;/ 调用重载赋值算符函数 定义定义Name类的重载赋值函数类的重载赋值函数 6.3.2 重载赋值运算符2024/8/626#include#includeclass Name public : Name ( char *pN ) ; Name( const Name & ) ; /复制构造函数 Name& operator=( const Name& ) ; / 重载赋值算符 Name() ; prot

45、ected : char *pName ; int size ; ;void main() Name Obj1( ZhangSan ) ; Name Obj2 = Obj1 ;/ 调用复制构造函数 Name Obj3( NoName ) ; Obj3 = Obj2 = Obj1 ;/ 调用重载赋值算符函数 定义定义Name类的重载赋值函数类的重载赋值函数 构造对象6.3.2 重载赋值运算符2024/8/627#include#includeclass Name public : Name ( char *pN ) ; Name( const Name & ) ; /复制构造函数 Name& o

46、perator=( const Name& ) ; / 重载赋值算符 Name() ; protected : char *pName ; int size ; ;void main() Name Obj1( ZhangSan ) ; Name Obj2 = Obj1 ;/ 调用复制构造函数 Name Obj3( NoName ) ; Obj3 = Obj2 = Obj1 ;/ 调用重载赋值算符函数 定义定义Name类的重载赋值函数类的重载赋值函数 复制构造对象6.3.2 重载赋值运算符2024/8/628#include#includeclass Name public : Name ( c

47、har *pN ) ; Name( const Name & ) ; /复制构造函数 Name& operator=( const Name& ) ; / 重载赋值算符 Name() ; protected : char *pName ; int size ; ;void main() Name Obj1( ZhangSan ) ; Name Obj2 = Obj1 ;/ 调用复制构造函数 Name Obj3( NoName ) ; Obj3 = Obj2 = Obj1 ;/ 调用重载赋值算符函数 定义定义Name类的重载赋值函数类的重载赋值函数 构造对象6.3.2 重载赋值运算符2024/8

48、/629#include#includeclass Name public : Name ( char *pN ) ; Name( const Name & ) ; /复制构造函数 Name& operator=( const Name& ) ; / 重载赋值算符 Name() ; protected : char *pName ; int size ; ;void main() Name Obj1( ZhangSan ) ; Name Obj2 = Obj1 ;/ 调用复制构造函数 Name Obj3( NoName ) ; Obj3 = Obj2 = Obj1 ;/ 调用重载赋值算符函数

49、定义定义Name类的重载赋值函数类的重载赋值函数 析构赋值操作后的对象6.3.2 重载赋值运算符2024/8/6307.3.3 7.3.3 重载运算符重载运算符和和()() 运算符 和 () 是二元运算符 和 () 只能用成员函数重载,不能用友元函数重载 6.3.3 重载运算符和()2024/8/6311 1重载下标算符重载下标算符 重载格式类 : 类型 operator ( 类型 ) ; 运算符用于访问数据对象的元素6.3.3 重载运算符和()2024/8/6321 1重载下标算符重载下标算符 重载格式类 : 类型 operator ( 类型 ) ; 运算符用于访问数据对象的元素定义重载函数

50、的类名6.3.3 重载运算符和()2024/8/6331 1重载下标算符重载下标算符 重载格式类 : 类型 operator 类型 ; 运算符用于访问数据对象的元素函数返回类型6.3.3 重载运算符和()2024/8/6341 1重载下标算符重载下标算符 重载格式类 : 类型 operator ( 类型 ) ; 运算符用于访问数据对象的元素函数名6.3.3 重载运算符和()2024/8/6351 1重载下标算符重载下标算符 重载格式类 : 类型 operator ( 类型 ) ; 运算符用于访问数据对象的元素右操作数为符合原语义,用 int6.3.3 重载运算符和()2024/8/6361 1

51、重载下标算符重载下标算符 重载格式类 : 类型 operator() 类型 ; 运算符用于访问数据对象的元素例例 设 x 是类 X 的一个对象,则表达式x y 可被解释为x . operator ( y )显式声明一个参数6.3.3 重载运算符和()2024/8/6372 2重载函数调用符重载函数调用符 ()() / / 用重载用重载()()算符实现数学函数的抽象算符实现数学函数的抽象#include class F public : double operator ( ) ( double x , double y ) ; ;double F : operator ( ) ( double

52、x , double y ) return x * x + y * y ; void main ( ) F f ; cout f ( 5.2 , 2.5 ) endl ;6.3.3 重载运算符和()2024/8/6382 2重载函数调用符重载函数调用符 ()() / / 用重载用重载()()算符实现数学函数的抽象算符实现数学函数的抽象#include class F public : double operator ( ) ( double x , double y ) ; ;double F : operator ( ) ( double x , double y ) return x *

53、x + y * y ; void main ( ) F f ; cout f ( 5.2 , 2.5 ) endl ;f . operator() (5.2, 2.5)6.3.3 重载运算符和()2024/8/6392 2重载函数调用符重载函数调用符 ()() / 用重载()算符实现数学函数的抽象#include class F public : double operator ( ) ( double x , double y ) ; ;double F : operator ( ) ( double x , double y ) return x * x + y * y ; void ma

54、in ( ) F f ; cout f ( 5.2 , 2.5 ) endl ;memFunmemFunf.memFun (5.2,2.5) 6.3.3 重载运算符和()2024/8/640istream 和 ostream 是 C+ 的预定义流类cin 是 istream 的对象,cout 是 ostream 的对象运算符 由 istream 重载为提取操作,用于输入基本类型数据程序员重载 ,用于输出和输入用户自定义的数据类型 7.3.4 7.3.4 重载流插入和流提取运算符重载流插入和流提取运算符 6.3.4 重载流插入和流提取运算符2024/8/641#include#includecl

55、ass vector public : vector( int size =1 ) ; vector() ; int & operator ( int i ) ; friend ostream & operator ( istream & input, vector & ) ; private : int * v ; int len ;void main() int k ; cout k ; vector A( k ) ; cout A ; cout Output the elements of vector A :n ; cout A ;为为vector类重载流插入运算符和提取运算符类重载流

56、插入运算符和提取运算符 6.3.4 重载流插入和流提取运算符2024/8/642#include#includeclass vector public : vector( int size =1 ) ; vector() ; int & operator ( int i ) ; friend ostream & operator ( istream & input, vector & ) ; private : int * v ; int len ;void main() int k ; cout k ; vector A( k ) ; cout A ; cout Output the ele

57、ments of vector A :n ; cout A ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 重载几个运算符6.3.4 重载流插入和流提取运算符2024/8/643#include#includeclass vector public : vector( int size =1 ) ; vector() ; int & operator ( int i ) ; friend ostream & operator ( istream & input, vector & ) ; private : int * v ; int len ;void mai

58、n() int k ; cout k ; vector A( k ) ; cout A ; cout Output the elements of vector A :n ; cout A ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 标准流类6.3.4 重载流插入和流提取运算符2024/8/644#include#includeclass vector public : vector( int size =1 ) ; vector() ; int & operator ( int i ) ; friend ostream & operator ( istre

59、am & input, vector & ) ; private : int * v ; int len ;void main() int k ; cout k ; vector A( k ) ; cout A ; cout Output the elements of vector A :n ; cout A ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 使用预定义版本6.3.4 重载流插入和流提取运算符2024/8/645#include#includeclass vector public : vector( int size =1 ) ; vector

60、() ; int & operator ( int i ) ; friend ostream & operator ( istream & input, vector & ) ; private : int * v ; int len ;void main() int k ; cout k ; vector A( k ) ; cout A ; cout Output the elements of vector A :n ; cout (A)6.3.4 重载流插入和流提取运算符2024/8/646#include#includeclass vector public : vector( int

61、 size =1 ) ; vector() ; int & operator ( int i ) ; friend ostream & operator ( istream & input, vector & ) ; private : int * v ; int len ;void main() int k ; cout k ; vector A( k ) ; cout A ; cout Output the elements of vector A :n ; cout A ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 使用重载版本cout . operat

62、or (A)6.3.4 重载流插入和流提取运算符2024/8/647vector:vector( int size ) if (size 100 ) cout The size of size =0 & i len ) return v i ; cout The subscript i is outside !n ; abort() ;ostream & operator ( ostream & output, vector & ary ) for(int i = 0 ; i ary.len ; i + ) output ary i ; output ( istream & input, ve

63、ctor & ary ) for( int i = 0 ; i ary i ; return input ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 6.3.4 重载流插入和流提取运算符2024/8/648vector:vector( int size ) if (size 100 ) cout The size of size =0 & i len ) return v i ; cout The subscript i is outside !n ; abort() ;ostream & operator ( ostream & output, vecto

64、r & ary ) for(int i = 0 ; i ary.len ; i + ) output ary i ; output ( istream & input, vector & ary ) for( int i = 0 ; i ary i ; return input ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 使用重载版本访问对象元素6.3.4 重载流插入和流提取运算符2024/8/649vector:vector( int size ) if (size 100 ) cout The size of size =0 & i len ) retu

65、rn v i ; cout The subscript i is outside !n ; abort() ;ostream & operator ( ostream & output, vector & ary ) for(int i = 0 ; i ary.len ; i + ) output ary i ; output ( istream & input, vector & ary ) for( int i = 0 ; i ary i ; return input ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 cout 的别名6.3.4 重载流插入和流

66、提取运算符2024/8/650vector:vector( int size ) if (size 100 ) cout The size of size =0 & i len ) return v i ; cout The subscript i is outside !n ; abort() ;ostream & operator ( ostream & output, vector & ary ) for(int i = 0 ; i ary.len ; i + ) output ary i ; output ( istream & input, vector & ary ) for( i

67、nt i = 0 ; i ary i ; return input ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 cin 的别名6.3.4 重载流插入和流提取运算符2024/8/651vector:vector( int size ) if (size 100 ) cout The size of size =0 & i len ) return v i ; cout The subscript i is outside !n ; abort() ;ostream & operator ( ostream & output, vector & ary ) fo

68、r(int i = 0 ; i ary.len ; i + ) output ary i ; output ( istream & input, vector & ary ) for( int i = 0 ; i ary i ; return input ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 返回流类引用以符合原语义6.3.4 重载流插入和流提取运算符2024/8/652vector:vector( int size ) if (size 100 ) cout The size of size =0 & i len ) return v i ; cou

69、t The subscript i is outside !n ; abort() ;ostream & operator ( ostream & output, vector & ary ) for(int i = 0 ; i ary.len ; i + ) output ary i ; output ( istream & input, vector & ary ) for( int i = 0 ; i ary i ; return input ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 6.3.4 重载流插入和流提取运算符2024/8/653运算符重载

70、可以像基本数据类型一样,用简洁明确的运算符操作自定义的类对象。重载运算符函数可以对运算符作出新的解释,但重原有的基本语义不变。运算符函数既可以重载为成员函数,也可以重载义为友员函数或普通函数。当一元运算符的操作数,或者二元运算符的左操作数是该类的一个对象时,以成员函数重载;当一个运算符的操作需要修改类对象状态时,应该以成员函数重载。如果以成友员函数重载,可以使用引用参数修改对象。当运算符的操作数(尤其是第一个操作数)希望有隐式转换,则重载算符时必须用友员函数。构造函数和类型转换函数可以实现基本类型与类类型,以及类类型之间的类型转换。小结小结小结小结2024/8/654 7.4 类类型转换类类型转换 此节不介绍按统一规定进行。此节不介绍按统一规定进行。 有时间可以进行实验讲述。有时间可以进行实验讲述。2024/8/655作业:作业:7.1 选择题选择题P282 57.2 阅读程序,写出结果阅读程序,写出结果P283 27.3 思考题思考题P284 17.4 编程题编程题P284 456以上有不当之处,请大家给与批评指正,以上有不当之处,请大家给与批评指正,谢谢大家!谢谢大家!

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 资格认证/考试 > 自考

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号