nullnull第10章 运算符重载第10章 运算符重载6.3 几个典型运算符重载6.3 几个典型运算符重载10.5 重载双目运算符 最常用的运算符——双目运算符;
有两个操作数,在运算符的左右两侧: 3+5 , a=b , i<10
重载双目运算符时,函数中应该有两个参数(友元函数)。6.3 几个典型运算符重载6.3 几个典型运算符重载例10.4 定义一个字符串类String,用来存放不定长的字符串,重载运算符“==”,“<”和“>”,用于两个字符串的等于、小于和大于的比较运算。
下面分4步来介绍编程过程:
建立一个String类(含构造函数,display函数)
重载一个运算符“>”
扩展至重载3个运算符“>” “<” “==”
函数compare整合3个重载运算符。重载双目运算符的应用null(1) 建立一个String类:
#include
using namespace std;
class String
{public:
String( ){p=NULL;} //默认构造函数
String(char *str); //构造函数
void display( );
private:
char *p; //字符型指针,用于指向字符串
};
6.3 几个典型运算符重载6.3 几个典型运算符重载String∷String(char *str) //定义构造函数
{ p=str; } //使p指向实参字符串
void String∷display( ) //输出p所指向的字符串
{ cout<”。
#include
#include
using namespace std;
class String
{public:
String( ){p=NULL;}
String(char *str);
friend bool operator>(String &string1,String &string2); //声明运算符函数为友元函数
void display( );
private:
char *p; //字符型指针,用于指向字符串
};VC++6.0环境下应改为
#include
#include
见323页“说明”。nullString∷String(char *str)
{ p=str; }
void String∷display( ) //输出p所指向的字符串
{ cout<(String &string1,String &string2) //定义运算符重载函数
{ if(strcmp(string1.p,string2.p)>0)
return true;
else return false;
}
int main( )
{ String string1(″Hello″),string2(″Book″);
cout<< (string1>string2) < (String &string1, String &string2);
friend bool operator< (String &string1, String &string2);
friend bool operator== (String &string1, String& string2);
在类外分别定义3个运算符重载函数:
bool operator> (String &string1,String &string2) //对运算符“>”重载
{ if(strcmp(string1.p,string2.p)>0)
return true;
else
return false;
}
bool operator< (String &string1,String &string2) //对运算符“<”重载
{ if(strcmp(string1.p,string2.p)<0)
return true;
else
return false;
}nullbool operator== (String &string1,String &string2) //对运算符“==”重载
{ if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}
再修改主函数:
int main( )
{ String string1(″Hello″),string2(″Book″),string3(″Computer″);
cout<<(string1>string2)<
using namespace std;
class String
{public:
String( ){p=NULL;}
String(char *str) {p=str;}
friend bool operator>(String &string1,String &string2);
friend bool operator<(String &string1,String &string2);
friend bool operator==(String &string1,String &string2);
void display( ) {cout<
#include
见323页“说明”。nullbool operator> (String &string1,String &string2)
{if(strcmp(string1.p,string2.p)>0)
return true;
else
return false;
}
bool operator< (String &string1,String &string2)
{if(strcmp(string1.p,string2.p)<0)
return true;
else
return false;
}
bool operator== (String &string1,String &string2)
{if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}调用此函数需加头文件
#include nullvoid compare(String &string1,String &string2)
{if(operator>(string1,string2)==1)
{string1.display( );cout<<″>″;string2.display( );}
else
if(operator<(string1,string2)==1)
{string1.display( );cout<<″<″;string2.display( );}
else
if(operator==(string1,string2)==1)
{string1.display( );cout<<″=″;string2.display( );}
cout<方法
与重载双目运算符类似null例10.5 有一个Time类,包含数据成员minute(分)和sec(秒),模拟秒
,每次走一秒,满60秒进一分钟,此时秒又从0开始算。要求输出分和秒的值。
#include
using namespace std;
class Time
{public:
Time( ){minute=0;sec=0;} //默认构造函数
Time(int m,int s):minute(m),sec(s){ } //构造函数重载
Time operator++( ); //声明运算符重载函数
void display( ){cout<=60)
{sec-=60; //满60秒进1分钟
++minute;}
return *this; //返回当前对象值
}
int main( )
{ Time time1(34,0);
for (int i=0;i<61;i++)
{ ++time1;
time1.display( );}
return 0;
}调用预定义版本null“++”和“--”运算符有两种使用方式:
前置、后置
重载时怎样区别呢?
C++约定:
在自增(自减)运算符重载函数中,增加一 个int型形参,就是后置自增(自减)运算符 函数。1 重载 ++ 与 -- 下面介绍类中常用的几个运算符重载的特点和应用:
++ 与 -- 、 = 、 []与()6.3.1 重载++与--6.3.1 重载++与--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 重载++与--6.3.1 重载++与--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 重载++与--6.3.1 重载++与--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) 引用&,是为了减少函数返回时对象复制的开销null例10.6 在例10.5程序的基础上增加对后置自增运算符的重载。修改后的程序如下:
#include
using namespace std;
class Time
{public:
Time( ){minute=0;sec=0;}
Time(int m,int s):minute(m),sec(s){}
Time operator++( ); //声明前置自增运算符“++”重载函数
Time operator++(int); //声明后置自增运算符“++”重载函数
void display( ){cout<=60)
{sec-=60;
++minute;}
return *this; //返回自加后的当前对象
}
Time Time∷operator++(int) //定义后置自增运算符“++”重载函数
{ Time temp(*this);
sec++;
if(sec>=60)
{sec-=60;
++minute;}
return temp; //返回的是自加前的对象
}nullint main( )
{ Time time1(34,59),time2;
cout<<″ time1 : ″;
time1.display( );
++time1;
cout<<″++time1: ″;
time1.display( );
time2= time1++ ; //将自加前的对象的值赋给time2
cout<<″time1++: ″;
time1.display( );
cout<<″ time2 :″;
time2.display( ); //输出time2对象的值
}前置 “++”和后置 “++” 的区别:
前置 “++”是先自加,返回的是修改后的对象本身;
后置 “++”返回的是自加前的对象,然后对象自加。6.3.1 重载++与--6.3.1 重载++与--例 成员函数重载++#include
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<
#include
class 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 重载赋值运算符6.3.2 重载赋值运算符#include
#include
class 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 重载赋值运算符6.3.2 重载赋值运算符#include
#include
class 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 重载赋值运算符6.3.2 重载赋值运算符#include
#include
class 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 重载赋值运算符6.3.2 重载赋值运算符#include
#include
class 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 重载赋值运算符6.3.2 重载赋值运算符#include
#include
class 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 重载赋值运算符6.3.2 重载赋值运算符#include
#include
class 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 重载赋值运算符6.3.2 重载赋值运算符#include
#include
class Name
{ public :
Name ( char *pN ) ;
Name( const Name & ) ; //复