几个典型运算符重载(C++)(四)[宣讲].ppt
文本预览下载声明
2.重载函数调用符 () // 用重载()算符实现数学函数的抽象 #include iostream.h 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 ; } memFun memFun f.memFun (5.2,2.5) 比较 定义普通成员函数 6.3.3 重载运算符[]和() * 精品PPT | 借鉴参考 istream 和 ostream 是 C++ 的预定义流类 cin 是 istream 的对象,cout 是 ostream 的对象 运算符 由ostream 重载为插入操作,用于输出基本类型数据 运算符 由 istream 重载为提取操作,用于输入基本类型数据 程序员重载 和 ,用于输出和输入用户自定义的数据类型 7.3.4 重载流插入和流提取运算符 6.3.4 重载流插入和流提取运算符 * 精品PPT | 借鉴参考 成员函数重载++ #includeiostream.h class Increase { public : Increase ( ) { value=0; } void display( ) const { coutvalue\n; } ; 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 重载++与-- * 精品PPT | 借鉴参考 成员函数重载++ #includeiostream.h class Increase { public : Increase ( ) { value=0; } void display( ) const { coutvalue\n; } ; 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 ++
显示全部