您好,欢迎来电子发烧友网! ,新用户?[免费注册]

当前位置:电子发烧友网 > 图书频道 > 电子 > 《C++程序设计实践例题》 > 第4章 第四章

第1节 第一部分

例10.1 通过函数来实现复数相加。

#include <iostream>

usingnamespacestd;

classComplex                                     //定义Complex类

{public:

Complex( ){real=0;imag=0;}                      //定义构造函数

Complex(doubler,doublei){real=r;imag=i;}     //构造函数重载

Complex complex_add(Complex &c2);              //声明复数相加函数

voiddisplay( );                                //声明输出函数

 private:

doublereal;                                   //实部

doubleimag;                                   //虚部

};

 

Complex Complex∷complex_add(Complex &c2)

{Complex c;

c.real=real+c2.real;

c.imag=imag+c2.imag;

returnc;}  

 

voidComplex∷display( )                            //定义输出函数

{cout<<"("<<real<<","<<imag<<"i)"<<endl;}

 

intmain( )

{Complex c1(3,4),c2(5,-10),c3;                     //定义3个复数对象

c3=c1.complex_add(c2);                            //调用复数相加函数

cout<<"c1="; c1.display( );                        //输出c1的值

cout<<"c2="; c2.display( );                        //输出c2的值

cout<<"c1+c2="; c3.display( );                     //输出c3的值

return0;

}

 

例10.10 包含转换构造函数、运算符重载函数和类型转换函数的程序。

先阅读以下程序,在这个程序中只包含转换构造函数和运算符重载函数。

#include <iostream>

usingnamespacestd;

classComplex

{public:

Complex( ){real=0;imag=0;}                             //默认构造函数

Complex(doubler){real=r;imag=0;}                     //转换构造函数

Complex(doubler,doublei){real=r;imag=i;}            //实现初始化的构造函数

friendComplex operator+ (Complex c1,Complex c2);    //重载运算符“+”的友元函数

voiddisplay( );

private:

doublereal;

doubleimag;

};

 

Complex operator+ (Complex c1,Complex c2)               //定义运算符“+”重载函数

{returnComplex(c1.real+c2.real, c1.imag+c2.imag);}

 

voidComplex∷display( )

{cout<<"("<<real<<","<<imag<<"i)"<<endl;}

 

intmain( )

{Complex c1(3,4),c2(5,-10),c3;

c3=c1+2.5;                                              //复数与double数据相加

c3.display( );

return0;

}

 

例10.2 改写例10.1,重载运算符“+”,使之能用于两个复数相加。

#include <iostream>

usingnamespacestd;

classComplex

{public:

Complex( ){real=0;imag=0;}

Complex(doubler,doublei){real=r;imag=i;}

Complex operator+(Complex &c2);               //声明重载运算符的函数

voiddisplay( );

 private:

doublereal;

doubleimag;

};

Complex Complex∷operator+(Complex &c2)          //定义重载运算符的函数

{ Complex c;

c.real=real+c2.real;

c.imag=imag+c2.imag;

returnc;}

 

voidComplex∷display( )

{ cout<<"("<<real<<","<<imag<<"i)"<<endl;}

 

intmain( )

{ Complex c1(3,4),c2(5,-10),c3;

c3=c1+c2;                                     //运算符+用于复数运算

cout<<"c1=";c1.display( );

cout<<"c2=";c2.display( );

cout<<"c1+c2=";c3.display( );

return0;

}

 

例10.3 将运算符“+”重载为适用于复数加法,重载函数不作为成员函数,而放在类外,作为Complex类的友元函数。

#include <iostream>

usingnamespacestd;

classComplex

{public:

Complex( ){real=0;imag=0;}

Complex(doubler,doublei){real=r;imag=i;}

friendComplex operator+ (Complex &c1,Complex &c2);//重载函数作为友元函数

voiddisplay( );

 private:

doublereal;

doubleimag;

};

 

Complex operator+ (Complex &c1,Complex &c2)          //定义作为友元函数的重载函数

{returnComplex(c1.real+c2.real, c1.imag+c2.imag);}

 

voidComplex∷display( )

{cout<<"("<<real<<","<<imag<<"i)"<<endl;}

intmain( )

{Complex c1(3,4),c2(5,-10),c3;

c3=c1+c2;

cout<<"c1="; c1.display( );

cout<<"c2="; c2.display( );

cout<<"c1+c2 ="; c3.display( );

}

 

例10.4 定义一个字符串类String,用来存放不定长的字符串,重载运算符“==”,“<”和“>”,用于两个字符串的等于、小于和大于的比较运算。

为了使读者便于理解程序,同时也使读者了解建立程序的步骤,下面分几步来介绍编程过程。

(1) 先建立一个String类:

#include <iostream>

usingnamespacestd;

classString

{public:

String( ){p=NULL;}                             //默认构造函数

String(char*str);                            //构造函数

voiddisplay( );

private:

char*p;                                       //字符型指针,用于指向字符串

};

String∷String(char*str)                          //定义构造函数

{p=str;}                                           //使p指向实参字符串

 

voidString∷display( )                             //输出p所指向的字符串

{cout<<p;}

 

intmain( )

{String string1("Hello"),string2("Book");

string1.display( );

cout<<endl;

string2.display( );

return0;

}

 

(2) 有了这个基础后,再增加其他必要的内容。现在增加对运算符重载的部分。为便于编写和调试,先重载一个运算符“>”。程序如下:

#include <iostream>

#include <string>

usingnamespacestd;

classString

{public:

String( ){p=NULL;}

String(char*str);

friendbooloperator>(String &string1,String &string2);//声明运算符函数为友元函数

voiddisplay( );

private:

char*p;                                       //字符型指针,用于指向字符串

};

String∷String(char*str)

{p=str;}

 

voidString∷display( )                             //输出p所指向的字符串

{cout<<p;}

booloperator>(String &string1,String &string2)      //定义运算符重载函数

{if(strcmp(string1.p,string2.p)>0)

returntrue;

elsereturnfalse;

}

 

intmain( )

{String string1("Hello"),string2("Book");

cout<<(string1>string2)<<endl;

}

 

(3) 扩展到对3个运算符重载。

在String类体中声明3个成员函数:

friendbooloperator> (String &string1, String &string2);

friendbooloperator< (String &string1, String &string2);

friendbooloperator==(String &string1, String& string2);

在类外分别定义3个运算符重载函数:

booloperator>(String &string1,String &string2)          //对运算符“>”重载

{if(strcmp(string1.p,string2.p)>0)

returntrue;

else

returnfalse;

}

 

booloperator<(String &string1,String &string2)          //对运算符“<”重载

{if(strcmp(string1.p,string2.p)<0)

returntrue;

else

returnfalse;

}

 

booloperator==(String &string1,String &string2)       //对运算符“==”重载

{if(strcmp(string1.p,string2.p)==0)

returntrue;

else

returnfalse;

}

再修改主函数:

intmain( )

{String string1("Hello"),string2("Book"),string3("Computer");

cout<<(string1>string2)<<endl;            //比较结果应该为true

cout<<(string1<string3)<<endl;            //比较结果应该为false

cout<<(string1==string2)<<endl;           //比较结果应该为false

return0;

}

 

(4) 再进一步修饰完善,使输出结果更直观。下面给出最后的程序。

#include <iostream>

usingnamespacestd;

classString

{public:

String( ){p=NULL;}

String(char*str);

friendbooloperator>(String &string1,String &string2);

friendbooloperator<(String &string1,String &string2);

friendbooloperator==(String &string1,String &string2);

voiddisplay( );

private:

char*p;

};

String∷String(char*str)

{p=str;}

 

voidString∷display( )                             //输出p所指向的字符串

{cout<<p;}

 

booloperator>(String &string1,String &string2)

{if(strcmp(string1.p,string2.p)>0)

returntrue;

else

returnfalse;

}

 

booloperator<(String &string1,String &string2)

{if(strcmp(string1.p,string2.p)<0)

returntrue;

else

returnfalse;

}

 

booloperator==(String &string1,String &string2)

{if(strcmp(string1.p,string2.p)==0)

returntrue;

else

returnfalse;

}

 

voidcompare(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<<endl;

}

intmain( )

{String string1("Hello"),string2("Book"),string3("Computer"),string4("Hello");

compare(string1,string2);

compare(string2,string3);

compare(string1,string4);

return0;

}

 

例10.5 有一个Time类,包含数据成员minute(分)和sec(秒),模拟秒表,每次走一秒,满60秒进一分钟,此时秒又从0开始算。要求输出分和秒的值。

#include <iostream>

usingnamespacestd;

classTime

{public:

Time( ){minute=0;sec=0;}                                //默认构造函数

Time(intm,ints):minute(m),sec(s){ }                   //构造函数重载

Time operator++( );                                     //声明运算符重载函数

voiddisplay( ){cout<<minute<<":"<<sec<<endl;}          //定义输出时间函数

private:

intminute;

intsec;

};

Time Time∷operator++( )                       //定义运算符重载函数

{if(++sec>=60)

{sec-=60;                                  //满60秒进1分钟

++minute;}

return*this;                             //返回当前对象值

}  

intmain( )

{Time time1(34,0);

for(inti=0;i<61;i++)

{++time1;

time1.display( );}

return0;

}

 

例10.6 在例10.5程序的基础上增加对后置自增运算符的重载。修改后的程序如下:

#include <iostream>

usingnamespacestd;

classTime

{public:

Time( ){minute=0;sec=0;}

Time(intm,ints):minute(m),sec(s){}

Time operator++( );                              //声明前置自增运算符“++”重载函数

Time operator++(int);                           //声明后置自增运算符“++”重载函数

voiddisplay( ){cout<<minute<<":"<<sec<<endl;}

private:

intminute;

intsec;

};

 

Time Time∷operator++( )                     //定义前置自增运算符“++”重载函数

{if(++sec>=60)

{sec-=60;

++minute;}

return*this;                              //返回自加后的当前对象

}

 

Time Time∷operator++(int)                  //定义后置自增运算符“++”重载函数

{Time temp(*this);

sec++;

if(sec>=60)

{sec-=60;

++minute;}

returntemp;                                //返回的是自加前的对象

}

 

intmain( )

{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对象的值

}

 

例10.7 在例10.2的基础上,用重载的“<<”输出复数。

#include <iostream>

usingnamespacestd;

classComplex

{public:

Complex( ){real=0;imag=0;}

Complex(doubler,doublei){real=r;imag=i;}

Complex operator+ (Complex &c2);                    //运算符“+”重载为成员函数

friendostream& operator<< (ostream&,Complex&);     //运算符“<<”重载为友元函数

private:

doublereal;

doubleimag;

};

 

Complex Complex∷operator+ (Complex &c2)               //定义运算符“+”重载函数

{returnComplex(real+c2.real,imag+c2.imag);}

ostream& operator<< (ostream& output,Complex& c)       //定义运算符“<<”重载函数

{output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;

returnoutput;

}

 

intmain( )

{Complex c1(2,4),c2(6,10),c3;

c3=c1+c2;

cout<<c3;

return0;

}

(在Visual C++ 6.0环境下运行时,需将第一行改为#include <iostream.h>,并删去第2行。)

 

例10.8 在例10.7的基础上,增加重载流提取运算符“>>”,用“cin>>”输入复数,用“cout<<”输出复数。

#include <iostream>

usingnamespacestd;

classComplex

{public:

friendostream& operator<< (ostream&,Complex&);    //声明重载运算符“<<”

friendistream& operator>> (istream&,Complex&);    //声明重载运算符“>>”

private:

doublereal;

doubleimag;

};

ostream& operator<< (ostream& output,Complex& c)       //定义重载运算符“<<”

{output<<"("<<c.real<<"+"<<c.imag<<"i)";

returnoutput;

}

istream& operator>> (istream& input,Complex& c)     //定义重载运算符“>>”

{cout<<"input real part and imaginary part of complex number:";

input>>c.real>>c.imag;

returninput;

}

intmain( )

{Complex c1,c2;

cin>>c1>>c2;

cout<<"c1="<<c1<<endl;

cout<<"c2="<<c2<<endl;

return0;

}

 

例10.9 使用类型转换函数的简单例子。

#include <iostream>

usingnamespacestd;

classComplex

{public:

Complex( ){real=0;imag=0;}

Complex(doubler,doublei){real=r;imag=i;}

operatordouble( ) {returnreal;}                //类型转换函数

private:

doublereal;

doubleimag;

};

 

intmain( )

{Complex c1(3,4),c2(5,-10),c3;

doubled;

d=2.5+c1;                           //要求将一个double数据与Complex类数据相加

cout<<d<<endl;

return0;

}