当前位置: 首页 > news >正文

中企动力 网站建设dw做网站乱码

中企动力 网站建设,dw做网站乱码,域名服务商有哪些,网站建设报价明细及方案一、什么是运算符重载 简单来讲就是对运算符赋予新的意义#xff0c;但是又不能改变原有的含义#xff0c;它本身也就是一个函数。运算符重载的本质是以函数的方式来体现。 二、运算符重载有几种 1、按照作用域来划分#xff0c;有全局操作符重载函数和成员函数操作符重载函…一、什么是运算符重载 简单来讲就是对运算符赋予新的意义但是又不能改变原有的含义它本身也就是一个函数。运算符重载的本质是以函数的方式来体现。 二、运算符重载有几种 1、按照作用域来划分有全局操作符重载函数和成员函数操作符重载函数。 2、按照操作数个数来划分有单目运算符重载和双目运算符重载。 通常来讲往往把输入流和输出流运算符改写为全局操作符重载函数普通的运算符改写为成员函数操作符重载函数。如果想要使用成员函数来重载输出流《 和 输入流》必须在C库中对应的类加入这个重载的成员函数不建议使用。 三、运算符重载的特性 1、不能新增运算符只能对已有的C运算符进行重载。 2、不能改变操作数的个数运算符重载后仍是原有操作数个数。 3、尽量不改变语义应当使重载运算符功能是类似相同。 4、并不是所有的运算符都可以重载以下5个运算符不能重载作用域运算符:成员访问运算符.类型长度运算符sizeof()成员指针运算符.*条件运算符?:。 四、怎么实现运算符重载 采用普通函数的调用方式来调用运算符重载函数基本语法如下 // 关键字operator不能省略 // 函数名operator 运算符 // 参数列表操作数的个数受原有运算符的操作数个数限定返回值 operator 运算符(参数列表) {// 重载函数体 } 五、案例 1、运算符重载的简单使用 #include iostreamusing namespace std;// 复数类 class Complex { private:int a; // 实部int b; // 虚部 public:Complex(int a 0, int b 0){this-a a;this-b b;}void show(){if(b 0){cout a b i endl;}else{cout a b i endl;}}// 重载运算符Complex operator (const Complex op){static Complex temp;temp.a this-a op.a;temp.b this-b op.b;return temp;}// 重载运算符-Complex operator - (const Complex op){static Complex temp;temp.a this-a - op.a;temp.b this-b - op.b;return temp;}// 重载运算符*Complex operator * (const Complex op){static Complex temp;temp.a this-a*op.a - this-b*op.b;temp.b this-a*op.b this-b*op.a;return temp;}// 重载运算符 / Complex operator / (const Complex op){static Complex temp;int x op.a*op.a op.b*op.b;temp.a (this-a*op.a this-b*op.b)/x;temp.b (this-b*op.a - this-a*op.b)/x;return temp;}bool operator (const Complex op){return (this-a op.a this-b op.b);}};int main() {Complex A(1, 2);Complex B(1, 2);if(A B){cout A B endl;}Complex C A B;cout C ;C.show();Complex D A - B;cout D ;D.show();Complex E A * B;cout E ;E.show();Complex F A / B;cout F ;F.show();return 0; } 2、全局操作符重载示例 #include iostreamusing namespace std;// 复数类 class Complex { private:int a; // 实部int b; // 虚部 public:Complex(int a 0, int b 0){this-a a;this-b b;}void show(){if(b 0){cout a b i endl;}else{cout a b i endl;}}// 全局操作符重载重载输入流和输出流为了方便通常声明friend友元函数friend ostream operator (ostream output, const Complex other);friend istream operator (istream input, Complex other);}; // 可以改为以下定义但是后面不能连续输入对象即cout A endl;是非法的只能cout A; // void operator (ostream output, const Complex other)ostream operator (ostream output, const Complex other) {if(other.b 0){output other.a other.b i;}else{cout other.a other.b i;}return output;}// 可以改为以下定义但是后面不能连续输入对象即cinAB;是非法的只能cinA; cinB; // void operator (istream output, const Complex other) istream operator (istream input, Complex other) {input other.a other.b;return input; }int main() {Complex A;Complex B;cin A B; // 可以连续输入类对象cout A ;A.show();cout B B endl;return 0; }3、[]运算符和()运算符 #include iostreamusing namespace std;class Test { private:int *a;int len; public:Test(int len 1){this-len len;if(len 0){cout len is less than 0 endl;return;}a new int[len];}~Test(){if(a ! nullptr){delete [] a;a nullptr;}}// 重载[]运算符函数int operator[](int index){return a[index];}// 重载()运算符函数变成所谓的仿函数可以用类对象来作为函数名void operator()(int len){for(int i 0; i len; i){cout a[i] ; }cout endl;}};int main() {Test temp(5);for(int i 0; i 5; i){temp[i] i1; // 像数组那样来操作}temp(5); // 像调用函数那样来操作return 0; } 六、总结 运算符重载通常是为了更加方便的操作类对象但是并不是所有的运算符都可以重载重载的运算符应该尽量跟原来运算符的含义相似同时不能创造新的不属于C的运算符。
http://www.w-s-a.com/news/595035/

相关文章:

  • 企业网站seo优帮云手机桌面布局设计软件
  • 无证做音频网站违法吗智能建站加盟电话
  • 鹿泉专业网站建设做网站为什么要建站点
  • 加强网站建设和维护工作新闻大全
  • 红鱼洞水库建设管理局网站左右左布局网站建设
  • 手机网站建设地址做网站公
  • 贵州建设厅网站首页网络公司除了做网站
  • 运动鞋建设网站前的市场分析wordpress 搜索框代码
  • app开发网站开发教程平台网站开发的税率
  • 百度网站优化排名加强服务保障满足群众急需i
  • 宁夏建设职业技术学院网站安徽网站优化建设
  • 四川关于工程建设网站硬盘做网站空间
  • 桂林网站制作培训学校外包seo公司
  • 莱州网站建设方案北京装修公司口碑
  • 大型网站建设济南兴田德润团队怎么样韩国女足出线了吗
  • 南通做网站找谁重庆网络推广网站推广
  • ps网站主页按钮怎么做怎样做网站的用户分析
  • 哪个网站做黑色星期五订酒店活动公司网络营销推广软件
  • 岳阳新网网站建设有限公司网页设计基础考试题目
  • 辽宁响应式网站费用海外平台有哪些
  • 杨凌规划建设局网站网站后台建设怎么进入
  • 有赞商城网站建设企业管理咨询是做什么的
  • 提供衡水网站建设中国石化工程建设有限公司邮政编码
  • 大芬地铁站附近做网站工业设计公司报价
  • 建设网站最强永年网站建设
  • 网站分站代理加盟wordpress国内工作室主题
  • 东营远见网站建设公司服装网站建设内容
  • 互助平台网站建设费用百度seo优化怎么做
  • lol英雄介绍网站模板工商局网上注册
  • 电商网站运营策划什么样的网站容易做seo