欢迎来到天天文库
浏览记录
ID:14169041
大小:115.00 KB
页数:40页
时间:2018-07-26
《c++运算符重载经典举例》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、c++运算符重载经典举例算法运算符重载//AclassComplex,anditsarithmeticoperations.#include#include#includeclassComplex{public: Complex(floatr=0,floati=0):_r(r),_i(i) { } voidprint()const; friendconstComplexoperator+(constComplex&x,constComplex&y); friendconstComplexoper
2、ator-(constComplex&x,constComplex&y); friendconstComplexoperator*(constComplex&x,constComplex&y); friendconstComplexoperator/(constComplex&x,constComplex&y);private: float_r; float_i;};voidComplex::print()const{ cout<<"_r="<<_r<3、tor+(constComplex&x,constComplex&y){ returnComplex(x._r+y._r,x._i+y._i);}inlineconstComplexoperator-(constComplex&x,constComplex&y){ returnComplex(x._r-y._r,x._i-y._i);}inlineconstComplexoperator*(constComplex&x,constComplex&y){ returnComplex(x._r*y._r-x._i*y._i,x._r*y._i+x._i*y._r4、);}constComplexoperator/(constComplex&x,constComplex&y){ if(y._r==0&&y._i==0) { exit(1); } floatden=y._r*y._r+y._i*y._i; ComplexaComplex((x._r*y._r+x._i*y._i)/den, (x._i*y._r-x._r*y._i)/den); //returnComplex((x._r*y._r+x._i*y._i)/den, // (x._i*y._r-x._r*y._i)/den5、); returnaComplex;}intmain(){ Complexx(2,3),y(-1,3); cout<<"xis";x.print(); cout<<"yis";y.print(); cout<<"x+yis"; (x+y).print(); cout<<"x-yis"; (x-y).print(); cout<<"x*yis"; (x*y).print(); cout<<"x/yis"; (x/y).print(); return0;} 引用 报告回复sdb新手上路UID1050精华0积分0帖子24阅读权限10注册6、2007-4-2状态离线#2使用道具 发表于2007-7-1114:52 资料 个人空间 短消息 加为好友 重载为成员函数//AclassComplex,anditsarithmeticoperations.#include#include#includeclassComplex{public: Complex(floatr=0,floati=0):_r(r),_i(i) { } voidprint()const; constComplexoperator+(constComplex&y); 7、constComplexoperator-(constComplex&y); constComplexoperator*(constComplex&y); constComplexoperator/(constComplex&y);private: float_r; float_i;};voidComplex::print()const{ cout<<"_r="<<_r<
3、tor+(constComplex&x,constComplex&y){ returnComplex(x._r+y._r,x._i+y._i);}inlineconstComplexoperator-(constComplex&x,constComplex&y){ returnComplex(x._r-y._r,x._i-y._i);}inlineconstComplexoperator*(constComplex&x,constComplex&y){ returnComplex(x._r*y._r-x._i*y._i,x._r*y._i+x._i*y._r
4、);}constComplexoperator/(constComplex&x,constComplex&y){ if(y._r==0&&y._i==0) { exit(1); } floatden=y._r*y._r+y._i*y._i; ComplexaComplex((x._r*y._r+x._i*y._i)/den, (x._i*y._r-x._r*y._i)/den); //returnComplex((x._r*y._r+x._i*y._i)/den, // (x._i*y._r-x._r*y._i)/den
5、); returnaComplex;}intmain(){ Complexx(2,3),y(-1,3); cout<<"xis";x.print(); cout<<"yis";y.print(); cout<<"x+yis"; (x+y).print(); cout<<"x-yis"; (x-y).print(); cout<<"x*yis"; (x*y).print(); cout<<"x/yis"; (x/y).print(); return0;} 引用 报告回复sdb新手上路UID1050精华0积分0帖子24阅读权限10注册
6、2007-4-2状态离线#2使用道具 发表于2007-7-1114:52 资料 个人空间 短消息 加为好友 重载为成员函数//AclassComplex,anditsarithmeticoperations.#include#include#includeclassComplex{public: Complex(floatr=0,floati=0):_r(r),_i(i) { } voidprint()const; constComplexoperator+(constComplex&y);
7、constComplexoperator-(constComplex&y); constComplexoperator*(constComplex&y); constComplexoperator/(constComplex&y);private: float_r; float_i;};voidComplex::print()const{ cout<<"_r="<<_r<
此文档下载收益归作者所有