一、运算符重载机制:

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

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

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

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

三、普通运算符重载:

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

 #include <iostream>
using namespace std; class Complex
{
private:
double real;
double image;
public:
Complex(double real=,double image=){ this->real=real,this->image=image; }
void display(){ cout<<"("<<real<<","<<image<<")"<<endl; }
friend Complex operator + (Complex A,Complex B){ return Complex(A.real+B.real,A.image+B.image); }//加法
friend Complex operator - (Complex A,Complex B);//减号
friend Complex operator - (Complex A);//负号
friend Complex operator ++ (Complex& A);//前置++
friend Complex operator ++ (Complex& A,int);//后置++
friend bool operator < (const Complex& A,const Complex& B);//
friend bool operator == (const Complex& A,const Complex& B);//
};
Complex operator - (Complex A,Complex B) { return Complex(A.real-B.real,A.image-B.image); }
Complex operator - (Complex A) { return Complex(-A.real,-A.image); }
Complex operator ++(Complex& A) { return Complex(++A.real,A.image); }
Complex operator ++(Complex& A,int) { return Complex(A.real++,A.image); }
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; }
bool operator == (const Complex& A,const Complex& B) { return A.real==B.real&&A.image==B.image; } int main()
{
Complex A(100.0,200.0),B(-10.0,20.0),C;
cout<<"A=";A.display();
cout<<"B=";B.display();
C=A+B;
cout<<"C=A+B=";C.display();
C=A-B;
cout<<"C=A-B=";C.display();
C=-A+B;
cout<<"C=-A+B=";C.display();
C=A++;
cout<<"C=A++,C=";C.display();
C=++A;
cout<<"C=++A,C=";C.display();
C=A+;
cout<<"C=A+5=";C.display();
return ;
}

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

 #include <iostream>
using namespace std; class Complex
{
private:
double real;
double image;
public:
Complex(double real=,double image=){ this->real=real,this->image=image; }
void display(){ cout<<"("<<real<<","<<image<<")"<<endl; }
Complex operator + (Complex B);//加法
Complex operator - (Complex B);//减号
Complex operator - ();//负号
Complex operator ++ ();//前置++
Complex operator ++ (int);//后置++
bool operator < (const Complex &B)const;//
bool operator ==(const Complex &B)const;//
};
Complex Complex::operator +(Complex B) { return Complex(real+B.real,image+B.image); }
Complex Complex::operator - (Complex B) { return Complex(real-B.real,image-B.image); }
Complex Complex::operator - () { return Complex(-real,-image); }
Complex Complex::operator ++() { return Complex(++real,image); }
Complex Complex::operator ++(int) { return Complex(real++,image); }
bool Complex::operator < (const Complex &B)const { return real*real+image*image<B.real*B.real+B.image*B.image; }
bool Complex::operator == (const Complex &B)const {return real==B.real&&image==B.image; } int main()
{
Complex A(100.0,200.0),B(-10.0,20.0),C;
cout<<"A=";A.display();
cout<<"B=";B.display();
C=A+B;
cout<<"C=A+B=";C.display();
C=A-B;
cout<<"C=A-B=";C.display();
C=-A+B;
cout<<"C=-A+B=";C.display();
C=A++;
cout<<"C=A++,C=";C.display();
C=++A;
cout<<"C=++A,C=";C.display();
C=A+;
cout<<"C=A+5=";C.display();
return ;
}

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

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

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

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

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

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

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

四、典型运算符重载:

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

 #include <iostream>
using namespace std; class Complex
{
private:
double real;
double image;
public:
Complex(double real=,double image=) { this->real=real,this->image=image; }
void display() { cout<<"("<<real<<","<<image<<")"<<endl; }
Complex operator + (Complex B);
Complex operator = (Complex B);
};
Complex Complex::operator +(Complex B) { return Complex(real+B.real,image+B.image); }
Complex Complex::operator =(Complex B)
{
real=B.real,image=B.image;
cout<<"operator = calling..."<<endl;
return *this;
}
int main()
{
Complex A(100.0,200.0),B(-10.0,20.0),C;
cout<<"A=",A.display();
cout<<"B=",B.display();
C=A+B;
cout<<"C=A+B=",C.display();
C=A;
cout<<"C=A=",C.display();
}

2.->:

 #include <iostream>
using namespace std; class Complex
{
private:
double real;
double image;
public:
Complex(double real=,double image=) { this->real=real,this->image=image; }
void display() { cout<<"("<<real<<","<<image<<")"<<endl; }
};
class PComplex
{
private:
Complex *PC;
public:
PComplex(Complex *PC=NULL) { this->PC=PC; }
Complex * operator ->()
{
static Complex NullComplex(0.0);
if(PC==NULL) return &NullComplex;
return PC;
}
};
int main()
{
PComplex P1;
P1->display();
Complex C1(,);
P1=&C1;
P1->display();
return ;
}

3.[]:

 #include <iostream>
#include <cstring>
using namespace std; class String
{
private:
char *str;
int len;
public:
void showstr() { cout<<"string:"<<str<<",length:"<<len<<endl; }
String(const char *p=NULL)
{
if(p)
{
len=strlen(p);
str=new char[len+];
strcpy(str,p);
}
else
{
len=;
str=NULL;
}
}
~String()
{
if(str!=NULL) delete []str;
}
char &operator[](int n) { return *(str+n); }
const char &operator[](int n)const { return *(str+n); }
}; int main()
{
String S1("0123456789abcdef");
S1.showstr();
S1[]='A';
cout<<"S1[10]=A"<<endl;
S1.showstr();
const String S2("ABCDEFGHIJKLMN");
cout<<"S2[10]="<<S2[]<<endl;
return ;
}

运算符重载(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. python接口自动化20-requests获取响应时间(elapsed)与超时(timeout)

    前言 requests发请求时,接口的响应时间,也是我们需要关注的一个点,如果响应时间太长,也是不合理的. 如果服务端没及时响应,也不能一直等着,可以设置一个timeout超时的时间 关于reques ...

  2. 杂项:mPaaS

    ylbtech-杂项:mPaaS 1. 概述返回顶部 mPaaS 是源于支付宝 App 的移动开发平台,为移动开发.测试.运营及运维提供云到端的一站式解决方案,能有效降低技术门槛.减少研发成本.提升开 ...

  3. python selenium-7自动发送邮件

    https://jingyan.baidu.com/article/647f0115b78f8d7f2148a8e8.html 1.发送HTML格式的邮件 import smtplib from em ...

  4. Fix-Dell iDRAC 7 error: RAC0218: The maximum number of user sessions is reached

    Hi Everyone, We came across the following error while performing some preventative maintenance check ...

  5. 一篇文章让你入门Shell !

    Shell脚本,就是利用Shell的命令解释的功能,对一个纯文本的文件进行解析,然后执行这些功能,也可以说Shell脚本就是一系列命令的集合. Shell可以直接使用在win/Unix/Linux上面 ...

  6. delphi WebBrowser的使用方法详解(五)-难点释疑

    网页代码:<SELECT id=fy onchange=TouchRefresh(1) name=fy> <OPTION selected value=15>每頁顯示15筆&l ...

  7. HIbernate编程模型

    1.Hibernate: ORM框架,简化SQL开发,编程接口丰富,简化JDBC编程 2.有点: Lazy机制配合Fetch的HQL高级查询,提高开发效率 难点:理解Lazy与Fetch JOIN的原 ...

  8. 编写函数digit(num, k),函数功能是:求整数num从右边开始的第k位数字的值,如果num位数不足k位则返回0。

    function digit(num,k){         var knum = 0;         for(var i=1; i<=k; i++){                 knu ...

  9. python实现定时发送系列

    1.发送邮件实现 2.定时任务实现 3.定时发送邮件实现 4.微信定时发送信息 详细源代码见:https://github.com/15387062910/timing_send 参考: 廖雪峰博客 ...

  10. cplex-Java-样例代码解析

    import ilog.cplex.IloCplex; import ilog.concert.*; /** * * * * 最大化 x1 + 2x2 + 3x3</br> * 约束 &l ...