一、运算符重载机制:

一元运算符:  @obj => operator @(obj)

二元运算符:  obj@obj2 => operator @(obj,obj2)

注意:前置++、--与一元运算符处理方式相同,而后置++、--这样处理:obj++ => operator ++(obj,0)

二、除了.、.*、::、?:、sizeof这5个运算符之外,其他一概都可以重载。

三、普通运算符重载:

1.重载为类的友元函数:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Complex
  5. {
  6. private:
  7. double real;
  8. double image;
  9. public:
  10. Complex(double real=,double image=){ this->real=real,this->image=image; }
  11. void display(){ cout<<"("<<real<<","<<image<<")"<<endl; }
  12. friend Complex operator + (Complex A,Complex B){ return Complex(A.real+B.real,A.image+B.image); }//加法
  13. friend Complex operator - (Complex A,Complex B);//减号
  14. friend Complex operator - (Complex A);//负号
  15. friend Complex operator ++ (Complex& A);//前置++
  16. friend Complex operator ++ (Complex& A,int);//后置++
  17. friend bool operator < (const Complex& A,const Complex& B);//
  18. friend bool operator == (const Complex& A,const Complex& B);//
  19. };
  20. Complex operator - (Complex A,Complex B) { return Complex(A.real-B.real,A.image-B.image); }
  21. Complex operator - (Complex A) { return Complex(-A.real,-A.image); }
  22. Complex operator ++(Complex& A) { return Complex(++A.real,A.image); }
  23. Complex operator ++(Complex& A,int) { return Complex(A.real++,A.image); }
  24. bool operator < (const Complex& A,const Complex& B) { return A.real*A.real+A.image*A.image<B.real*B.real+B.image*B.image; }
  25. bool operator == (const Complex& A,const Complex& B) { return A.real==B.real&&A.image==B.image; }
  26.  
  27. int main()
  28. {
  29. Complex A(100.0,200.0),B(-10.0,20.0),C;
  30. cout<<"A=";A.display();
  31. cout<<"B=";B.display();
  32. C=A+B;
  33. cout<<"C=A+B=";C.display();
  34. C=A-B;
  35. cout<<"C=A-B=";C.display();
  36. C=-A+B;
  37. cout<<"C=-A+B=";C.display();
  38. C=A++;
  39. cout<<"C=A++,C=";C.display();
  40. C=++A;
  41. cout<<"C=++A,C=";C.display();
  42. C=A+;
  43. cout<<"C=A+5=";C.display();
  44. return ;
  45. }

2.重载为类的成员函数:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Complex
  5. {
  6. private:
  7. double real;
  8. double image;
  9. public:
  10. Complex(double real=,double image=){ this->real=real,this->image=image; }
  11. void display(){ cout<<"("<<real<<","<<image<<")"<<endl; }
  12. Complex operator + (Complex B);//加法
  13. Complex operator - (Complex B);//减号
  14. Complex operator - ();//负号
  15. Complex operator ++ ();//前置++
  16. Complex operator ++ (int);//后置++
  17. bool operator < (const Complex &B)const;//
  18. bool operator ==(const Complex &B)const;//
  19. };
  20. Complex Complex::operator +(Complex B) { return Complex(real+B.real,image+B.image); }
  21. Complex Complex::operator - (Complex B) { return Complex(real-B.real,image-B.image); }
  22. Complex Complex::operator - () { return Complex(-real,-image); }
  23. Complex Complex::operator ++() { return Complex(++real,image); }
  24. Complex Complex::operator ++(int) { return Complex(real++,image); }
  25. bool Complex::operator < (const Complex &B)const { return real*real+image*image<B.real*B.real+B.image*B.image; }
  26. bool Complex::operator == (const Complex &B)const {return real==B.real&&image==B.image; }
  27.  
  28. int main()
  29. {
  30. Complex A(100.0,200.0),B(-10.0,20.0),C;
  31. cout<<"A=";A.display();
  32. cout<<"B=";B.display();
  33. C=A+B;
  34. cout<<"C=A+B=";C.display();
  35. C=A-B;
  36. cout<<"C=A-B=";C.display();
  37. C=-A+B;
  38. cout<<"C=-A+B=";C.display();
  39. C=A++;
  40. cout<<"C=A++,C=";C.display();
  41. C=++A;
  42. cout<<"C=++A,C=";C.display();
  43. C=A+;
  44. cout<<"C=A+5=";C.display();
  45. return ;
  46. }

3.两种重载方式的比较:

1)一般情况下,单目运算符最好重载为类的成员函数;双目运算符则最好重载为类的友元函数。

2)一些双目运算符不能重载为类的友元函数:=、()、[]、->。

3)若一个运算符的操作需要改变对象的状态,选择重载为成员函数较好。

4)若运算符所需的操作数(尤其第一个操作数)希望有隐式类型转换,则只能选用友元函数。

5)当运算符函数是一个成员函数时,最左边的操作数必须是运算符类的一个类对象或其引用。若左边的操作数必须是一个不同类的对象,或者是一个基本数据类型的对象,该运算符函数必须作为一个友元函数来实现。

6)当需要重载运算符的运算具有可交换性时,选择重载为友元函数。

四、典型运算符重载:

1.重载=进行复数类数据赋值:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Complex
  5. {
  6. private:
  7. double real;
  8. double image;
  9. public:
  10. Complex(double real=,double image=) { this->real=real,this->image=image; }
  11. void display() { cout<<"("<<real<<","<<image<<")"<<endl; }
  12. Complex operator + (Complex B);
  13. Complex operator = (Complex B);
  14. };
  15. Complex Complex::operator +(Complex B) { return Complex(real+B.real,image+B.image); }
  16. Complex Complex::operator =(Complex B)
  17. {
  18. real=B.real,image=B.image;
  19. cout<<"operator = calling..."<<endl;
  20. return *this;
  21. }
  22. int main()
  23. {
  24. Complex A(100.0,200.0),B(-10.0,20.0),C;
  25. cout<<"A=",A.display();
  26. cout<<"B=",B.display();
  27. C=A+B;
  28. cout<<"C=A+B=",C.display();
  29. C=A;
  30. cout<<"C=A=",C.display();
  31. }

2.->:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Complex
  5. {
  6. private:
  7. double real;
  8. double image;
  9. public:
  10. Complex(double real=,double image=) { this->real=real,this->image=image; }
  11. void display() { cout<<"("<<real<<","<<image<<")"<<endl; }
  12. };
  13. class PComplex
  14. {
  15. private:
  16. Complex *PC;
  17. public:
  18. PComplex(Complex *PC=NULL) { this->PC=PC; }
  19. Complex * operator ->()
  20. {
  21. static Complex NullComplex(0.0);
  22. if(PC==NULL) return &NullComplex;
  23. return PC;
  24. }
  25. };
  26. int main()
  27. {
  28. PComplex P1;
  29. P1->display();
  30. Complex C1(,);
  31. P1=&C1;
  32. P1->display();
  33. return ;
  34. }

3.[]:

  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class String
  6. {
  7. private:
  8. char *str;
  9. int len;
  10. public:
  11. void showstr() { cout<<"string:"<<str<<",length:"<<len<<endl; }
  12. String(const char *p=NULL)
  13. {
  14. if(p)
  15. {
  16. len=strlen(p);
  17. str=new char[len+];
  18. strcpy(str,p);
  19. }
  20. else
  21. {
  22. len=;
  23. str=NULL;
  24. }
  25. }
  26. ~String()
  27. {
  28. if(str!=NULL) delete []str;
  29. }
  30. char &operator[](int n) { return *(str+n); }
  31. const char &operator[](int n)const { return *(str+n); }
  32. };
  33.  
  34. int main()
  35. {
  36. String S1("0123456789abcdef");
  37. S1.showstr();
  38. S1[]='A';
  39. cout<<"S1[10]=A"<<endl;
  40. S1.showstr();
  41. const String S2("ABCDEFGHIJKLMN");
  42. cout<<"S2[10]="<<S2[]<<endl;
  43. return ;
  44. }

运算符重载(C++)的更多相关文章

  1. C++ 运算符重载时,将运算符两边对象交换问题.

    在C++进行运算符重载时, 一般来讲,运算符两边的对象的顺序是不能交换的. 比如下面的例子: #include <iostream> using namespace std; class ...

  2. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

  3. C++运算符重载

    C++运算符重载 基本知识 重载的运算符是具有特殊名字的函数,他们的名字由关键字operator和其后要定义的运算符号共同组成. 运算符可以重载为成员函数和非成员函数.当一个重载的运算符是成员函数时, ...

  4. 标准C++之运算符重载和虚表指针

    1 -> *运算符重载 //autoptr.cpp     #include<iostream> #include<string> using namespace std ...

  5. python运算符重载

    python运算符重载就是在解释器使用对象内置操作前,拦截该操作,使用自己写的重载方法. 重载方法:__init__为构造函数,__sub__为减法表达式 class Number: def __in ...

  6. PoEduo - C++阶段班【Po学校】-Lesson03-5_运算符重载- 第7天

    PoEduo - Lesson03-5_运算符重载- 第7天 复习前面的知识点 空类会自动生成哪些默认函数 6个默认函数    1  构造  2  析构   3  赋值  4 拷贝构造  5 oper ...

  7. 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换

    [源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自 ...

  8. 我的c++学习(8)运算符重载和友元

    运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能.这个函数叫做运算符重载函数(常为类的成员函数). 方法与解释 ◆ 1.定义运 ...

  9. c/c++面试题(6)运算符重载详解

    1.操作符函数: 在特定条件下,编译器有能力把一个由操作数和操作符共同组成的表达式,解释为对 一个全局或成员函数的调用,该全局或成员函数被称为操作符函数.该全局或成员函数 被称为操作符函数.通过定义操 ...

  10. 实验12:Problem H: 整型数组运算符重载

    Home Web Board ProblemSet Standing Status Statistics   Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...

随机推荐

  1. java操作Excel之POI(6)使用POI实现使用模板批量添加数据

    action是用struts2写的:前端界面easyUI写的, 前端: <!DOCTYPE html> <html> <head> <meta charset ...

  2. unity3d之Editor的Assembly-CSharp.dll文件路径

    在Editor中与自己project中使用的Mono与Managed文件夹路径区别: Editor中:在unity安装路径[AppDir]下: 自己project中:在project的路径下,由bui ...

  3. java的缓存框架

    1.java里面有一些开源的缓存框架,比如ecache,memcache,redis等缓存框架. 2.使用缓存框架的原理就是减少数据库端的压力,将缓存数据放在内存里面,存储成键值对的格式,这样可以不去 ...

  4. Linux常用命令的命名来源

    很多人在学习Linux的时候会疑惑:这么多的Linux名,他们都是怎么被定义的?林纳斯是怎么制定如此花样繁多且数量庞大的命令?今天这篇文章可能会帮你解开疑惑. ## 1. 目录缩写 缩写 | 全称 | ...

  5. jenkins API

    1.curl http://199.168.299.99:8080/job/send_message/lastBuild/api/json --user administrator:1234  获取j ...

  6. QQ去除聊天框广告详解——2016.9 版

    QQ聊天框广告很烦人,百度出来的一些方法早已过时,下面是博主整理出来的方法,供各位同学参考. 1.按键盘上的 Win+R 快捷键打开运行框,然后复制并粘贴 Application Data\Tence ...

  7. extends和implements区别

    extends与implements的不同 1.在类的声明中,通过关键字extends来创建一个类的子类. 一个类通过关键字implements声明自己使用一个或者多个接口. extends 是继承某 ...

  8. UVA-673-栈-水题

    题意: 检测括号是否匹配,注意有空格 #include<stdio.h> #include<iostream> #include <strstream> #incl ...

  9. loganalyzer

  10. 好久没玩laravel了,5.6玩下(三)

    好了,基础的测试通了,咱们开始增删改了 思路整理 先创建项目功能控制器 然后设置路由访问规则 然后开发项目的增删改功能 1 先创建项目的控制器 php artisan make:controller ...