资源预览内容
第1页 / 共56页
第2页 / 共56页
第3页 / 共56页
第4页 / 共56页
第5页 / 共56页
第6页 / 共56页
第7页 / 共56页
第8页 / 共56页
第9页 / 共56页
第10页 / 共56页
亲,该文档总共56页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 . 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 : 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; 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/65成员函数重载成员函数重载+#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 ) 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( ) ; cout b= ; b.display( ) ; Increase operator + ( ) ;Increase Increase : operator + ( ) value + ; return * this ; + n ;预定义版本6.3.1 重载+与-2024/8/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 ) 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( ) ; cout b= ; b.display( ) ; Increase operator + ( ) ;Increase Increase : 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 + ( ) 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( ) ; 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 + ( ) ; / 前置 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 + ; 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 + ( int ) ;Increase Increase : operator + ( int ) Increase temp; temp.value = value + ; return temp; n + ;重载版本6.3.1 重载+与-2024/8/69成员函数重载成员函数重载+#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 ) 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( ) ; cout b= ; b.display( ) ; 6.3.1 重载+与-2024/8/610友员函数重载友员函数重载+#includeclass Increase 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 ) 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( ) ; 6.3.1 重载+与-2024/8/611友员函数重载友员函数重载+#includeclass Increase 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 ) 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 & ) ;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 ) ; / 后置 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 ( 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 : 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 ) 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 &, int ) ;Increase operator + ( 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 ) ; / 后置 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 ( 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 ) ; 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/617#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 ;/ 调用重载赋值算符函数 定义定义Name类的重载赋值函数类的重载赋值函数 初始化对象时调用复制构造函数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( NoName ) ; 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 & 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( pName , 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 Name & 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( 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 & 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( pName , 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 ) /复制构造函数 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.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 ) /复制构造函数 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.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 ) /复制构造函数 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.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() 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() ; 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/627#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 ;/ 调用重载赋值算符函数 定义定义Name类的重载赋值函数类的重载赋值函数 复制构造对象6.3.2 重载赋值运算符2024/8/628#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 ;/ 调用重载赋值算符函数 定义定义Name类的重载赋值函数类的重载赋值函数 构造对象6.3.2 重载赋值运算符2024/8/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 ;/ 调用重载赋值算符函数 定义定义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 ( 类型 ) ; 运算符用于访问数据对象的元素定义重载函数的类名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重载下标算符重载下标算符 重载格式类 : 类型 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 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 * 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 main ( ) 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#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 elements of vector A :n ; cout A ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 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 elements 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 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/644#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 elements of vector A :n ; cout A ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 使用预定义版本6.3.4 重载流插入和流提取运算符2024/8/645#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 elements of vector A :n ; cout (A)6.3.4 重载流插入和流提取运算符2024/8/646#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 elements of vector A :n ; cout A ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 使用重载版本cout . operator (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, vector & 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, 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/649vector: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( int i = 0 ; i ary i ; return input ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 cout 的别名6.3.4 重载流插入和流提取运算符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( int 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 ) 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/652vector: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( int i = 0 ; i ary i ; return input ;为为vector类重载流插入运算符和提取运算符类重载流插入运算符和提取运算符 6.3.4 重载流插入和流提取运算符2024/8/653运算符重载可以像基本数据类型一样,用简洁明确的运算符操作自定义的类对象。重载运算符函数可以对运算符作出新的解释,但重原有的基本语义不变。运算符函数既可以重载为成员函数,也可以重载义为友员函数或普通函数。当一元运算符的操作数,或者二元运算符的左操作数是该类的一个对象时,以成员函数重载;当一个运算符的操作需要修改类对象状态时,应该以成员函数重载。如果以成友员函数重载,可以使用引用参数修改对象。当运算符的操作数(尤其是第一个操作数)希望有隐式转换,则重载算符时必须用友员函数。构造函数和类型转换函数可以实现基本类型与类类型,以及类类型之间的类型转换。小结小结小结小结2024/8/654 7.4 类类型转换类类型转换 此节不介绍按统一规定进行。此节不介绍按统一规定进行。 有时间可以进行实验讲述。有时间可以进行实验讲述。2024/8/655作业:作业:7.1 选择题选择题P282 57.2 阅读程序,写出结果阅读程序,写出结果P283 27.3 思考题思考题P284 17.4 编程题编程题P284 456以上有不当之处,请大家给与批评指正,以上有不当之处,请大家给与批评指正,谢谢大家!谢谢大家!
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号