c++运算符重载经典举例.doc_第1页
c++运算符重载经典举例.doc_第2页
c++运算符重载经典举例.doc_第3页
c++运算符重载经典举例.doc_第4页
c++运算符重载经典举例.doc_第5页
已阅读5页,还剩35页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

c+运算符重载经典举例算法运算符重载/ A class Complex, and its arithmetic operations.#include #include #include class Complexpublic: Complex( float r = 0, float i = 0 ) : _r( r ), _i( i ) void print( ) const; friend const Complex operator+(const Complex& x, const Complex& y); friend const Complex operator-(const Complex& x, const Complex& y); friend const Complex operator*(const Complex& x, const Complex& y); friend const Complex operator/(const Complex& x, const Complex& y);private: float _r; float _i;void Complex:print( ) const cout_r = _rendl; cout_I = _iendl;inline const Complex operator+(const Complex& x, const Complex& y) return Complex( x._r + y._r, x._i + y._i);inline const Complex operator-(const Complex& x, const Complex& y) return Complex(x._r - y._r, x._i - y._i);inline const Complex operator*(const Complex& x, const Complex& y) return Complex( x._r * y._r - x._i * y._i, x._r * y._i + x._i * y._r);const Complex operator/(const Complex& x, const Complex& y) if (y._r = 0 & y._i = 0) exit(1); float den = y._r * y._r + y._i * y._i; Complex aComplex(x._r * y._r + x._i * y._i) / den, (x._i * y._r - x._r * y._i) / den); /return Complex(x._r * y._r + x._i * y._i) / den, / (x._i * y._r - x._r * y._i) / den); return aComplex;int main( ) Complex x(2, 3), y(-1, 3); cout x is ; x.print( ); cout y is ; y.print( ); cout x + y is ; (x + y).print( ); cout x - y is ; (x - y).print( ); cout x * y is ; (x * y).print( ); cout x / y is ; (x / y).print( ); return 0;引用 报告 回复 sdb 新手上路UID 1050精华 0积分 0帖子 24阅读权限 10注册 2007-4-2状态 离线 #2使用道具发表于 2007-7-11 14:52 资料 个人空间 短消息 加为好友 重载为成员函数/ A class Complex, and its arithmetic operations.#include #include #include class Complexpublic: Complex( float r = 0, float i = 0 ) : _r( r ), _i( i ) void print( ) const; const Complex operator+(const Complex& y); const Complex operator-(const Complex& y); const Complex operator*(const Complex& y); const Complex operator/(const Complex& y);private: float _r; float _i;void Complex:print( ) const cout_r = _rendl; cout_I = _iendl;inline const Complex Complex:opearator+(const Complex& y) return Complex( _r + y._r, _i + y._i);inline const Complex Complex:operator-(const Complex& y) return Complex(_r - y._r, _i - y._i);inline const Complex Complex:operator*(const Complex& y) return Complex( _r * y._r - _i * y._i, _r * y._i + _i * y._r);const Complex Complex:operator/(const Complex& y) if (y._r = 0 & y._i = 0) exit(1); float den = y._r * y._r + y._i * y._i; Complex aComplex(_r * y._r + _i * y._i) / den, (_i * y._r - _r * y._i) / den); /return Complex(x._r * y._r + x._i * y._i) / den, / (x._i * y._r - x._r * y._i) / den); return aComplex;int main( ) Complex x(2, 3), y(-1, 3); cout x is ; x.print( ); cout y is ; y.print( ); cout x + y is ; (x + y).print( ); cout x - y is ; (x - y).print( ); cout x * y is ; (x * y).print( ); cout x / y is ; (x / y).print( ); return 0; 本帖最后由 sdb 于 2007-7-11 14:58 编辑 引用 报告 回复 sdb 新手上路UID 1050精华 0积分 0帖子 24阅读权限 10注册 2007-4-2状态 离线 #3使用道具发表于 2007-7-11 14:53 资料 个人空间 短消息 加为好友 重载下标操作符/ Class Vector is to demonstrator overloading the subscript operator#include #define size 5class Vectorprivate: int repsize;public: const int& operator (int index) const return repindex; int& operator (int index) return repindex; ;/ Using the overloaded operator , this function does not access / the private member of class Vectorostream& operator(ostream& out, const Vector& a) for (int i = 0; i size; +i) out a endl; /outa.rep; / the const version of operator is used return out;int main( ) Vector x; cout Enter size integers.n; for (int i = 0; i x; / the non-const version of operator is used const Vector y = x; cout y = ; cout y; x0 = 1; / Non-const version again cout y = ; cout y; cout x = ; coutx; return 0;/ This example defines the second version of the class Vector, which has overloaded operators =, / and . #include class Vectorpublic: Vector(int s, int an_array ); / a constructor Vector( ) delete rep; / a destructor int get_size( ) const return size; / an accessor const Vector& operator=(const Vector& x); int& operator (int index) return repindex; const int& operator (int index) const return repindex; private: int *rep; int size;/ A constructor initializing the members of rep by the parameter an_arrayVector: Vector(int s, int an_array ) : size(s), rep( new ints ) for (int i = 0; i size; +i) rep = an_array; / Note that the initializer uses new ints to initialize rep, but not new intsize/ because the initializers may not be evaluated in the specified order.const Vector& Vector:operator=(const Vector& x) if( this != &x) size = x.size; delete rep; / clean up the old one. rep = new intsize; for (int i = 0; i size; +i) rep = x.rep; return *this;ostream& operator(ostream& out, const Vector& x) int s = x.get_size( ); for (int i = 0; i s; +i) out xendl; return out;int main() int array5 = 1, 2, 3, 4, 5 ; Vector v1( 5, array ); coutv1= v1endl; Vector v2 = v1; coutv2= v2endl; v14 = 100; coutv2= v2endl; return 0; 本帖最后由 sdb 于 2007-7-11 14:58 编辑 引用 报告 回复 sdb 新手上路UID 1050精华 0积分 0帖子 24阅读权限 10注册 2007-4-2状态 离线 #4使用道具发表于 2007-7-11 14:54 资料 个人空间 短消息 加为好友 输出符重载/ To illustrate the dangers of the default assignment with dynamic memory allocation.#include #include const int MAX_CHAR = 10; class Account friend ostream& operator strlen(title) ) delete title; title = NULL; title = new charstrlen(newname)+1; strcpy( title, newname ); else strcpy(title, newname); void changename(char* newname) strcpy( owner, newname ); Account () delete title; private: char* title; char ownerMAX_CHAR; float balance; ; ostream& operator (ostream& os, Account &b) os who: b.title b.owner how much=b.balancen; os endl; return os; void main() Account acc(Lou,Mr, 100); Account acc1; cout acc; cout acc1; acc1=acc; cout acc1; acc1.changename(jean); cout acc1; coutacc; acc1.changetitle(Dr.); cout acc1; coutacc;引用 报告 回复 sdb 新手上路UID 1050精华 0积分 0帖子 24阅读权限 10注册 2007-4-2状态 离线 #5使用道具发表于 2007-7-11 14:59 资料 个人空间 短消息 加为好友 容器的构造/ The following is another version of the class Vector with three constructors, a destructor (which / is the same as in the previous example), an overloaded assignment operator, an overloaded/ output operator, and an overloaded equality operator.#include class Vectorprivate: int *rep; int size; void clone(const Vector& a); void dispose( );public: Vector(int s=0); / a default constructor initializing all members of rep to 0 if s is not 0. Vector(int *, int); / a constructor creates a Vector object from an ordinary array Vector(const Vector& v); / a copy constructor Vector( ) dispose( ); / a destructor int get_size( ) const return size; / an accessor const Vector& operator=(const Vector& x); int& operator (int index) return repindex; const int& operator (int index) const return repindex;void Vector:clone(const Vector& a) this-size = a.size; rep = new intsize; for (int count = 0; count size; +count) repcount = acount;void Vector:dispose( ) delete rep; Vector:Vector(int s) : size(s) if (size = 0) rep = NULL; else rep = new intsize; for (int count = 0; count size; +count) repcount = 0; Vector:Vector(int * a, int s) : size(s), rep(new ints) for (int count = 0; count size = a.size; rep = new intsize; for (int count = 0; count size; +count) repcount = acount; return *this;/ overloading operator , not a friend functionostream& operator(ostream& out, const Vector& x) int s = x.get_size( ); for (int i = 0; i s; +i) out xendl; out endl; return out;bool operator=(const Vector& a, const Vector& b) bool yes = true; if (a.get_size( ) != b.get_size( ) yes = false; else int s, index = 0; s = a.get_size( ); while (index s & aindex = bindex) +index; if (index s) yes = false; return yes;引用 报告 回复 sdb 新手上路UID 1050精华 0积分 0帖子 24阅读权限 10注册 2007-4-2状态 离线 #6使用道具发表于 2007-7-11 15:01 资料 个人空间 短消息 加为好友 类型转换/ The following example demonstrates when a constructor or a destructor is called:#include class Demoprivate: int i;public: / default constructor Demo(int n = 0) : i(n) cout default constructor calledn; / copy constructor: for example Demo b(a); Demo(const Demo& a) i = a.i;cout copy constructor calledn; / destructor Demo( ) cout i = a.i; cout assignment operator usedn; return *this; Demo& set_value(int n) i = n; return *this; / returning an object to facilitate function chain int get_value( ) return i;Demo foo(Demo x)/ pass by value to invoke copy constructor Demo d; return d; / returning a local object by value int main( ) Demo a(2); Demo b; b = foo(a); Demo c = a; return 0;引用 报告 回复 sdb 新手上路UID 1050精华 0积分 0帖子 24阅读权限 10注册 2007-4-2状态 离线 #7使用道具发表于 2007-7-11 15:02 资料 个人空间 短消息 加为好友 字符串的构造/ Constructor type conversion#include #include class String int len; char * rep;public: String( ) : len(0) / default constructor rep = new char1; strcpy(rep, ); String(char * s) / another constructor if (!s) len = 0; s = new char1; strcpy(rep, ); else len = strlen(s); rep = new charlen + 1; strcpy(rep, s); String(const String& str) : len(str.len), rep(new charstr.len + 1) / copy constructor strcpy(rep, str.rep); String( ) / destructor delete rep; friend void print(const String& s);void print(const String& s) cout s.rep ; return;int main( ) char * a = first string; print(a); print(second string); String b = bad initialization; print(b); return 0;引用 报告 回复 sdb 新手上路UID 1050精华 0积分 0帖子 24阅读权限 10注册 2007-4-2状态 离线 #8使用道具发表于 2007-7-11 15:04 资料 个人空间 短消息 加为好友 / The following program defines four arrays of objects in class Item.The first array initializes the / objects by calling the constructor explicitly.The second

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论