c++语言中运算符重载都是通过函数来实现的,所以其实质为函数重载,当c++语言原有的一个运算符被重载之后,它原来所具有的语义并没有消失,只相当于针对一个特定的类定义了一个新的运算符。

<1>用成员函数重载运算符

例1:

#include <iostream>
using namespace std;
class RMB{
public:
RMB(unsigned int d,unsigned int c);
RMB operator+(RMB&);
RMB& operator++();
void display(){
cout<<(yuan+jf/100.0)<<endl;
}
protected:
unsigned int yuan;
unsigned int jf;
};
RMB::RMB(unsigned int d,unsigned int c){
yuan=d;
jf=c;
while(jf>=){
yuan++;
jf-=;
}
}
RMB RMB::operator +(RMB& s){
unsigned int c=jf+s.jf;
unsigned int d=yuan+s.yuan;
RMB result(d,c);
return result;
}
RMB& RMB::operator++(){
jf++;
if(jf>=){
jf-=;
yuan++;
}
return *this;
}
int main(){
RMB d1(,);
RMB d2(,);
RMB d3(,);
d3=d1+d2;
++d3;
d3.display();
return ;
}

结果:4.11

例2:

#include <iostream>
#include <string>
using namespace std;
class String
{
char name[];
public:
String(char* str){
strcpy(name,str);
}
String(){
}
~String(){
}
String operator+(const String&);
void display(){
cout<<"The string is:"<<name<<endl;
}
};
static char* str;
String String::operator+(const String& a){
strcpy(str,name);
strcat(str,a.name);
return String(str);//等价于return str;进行自动类型转换
}
int main(){
str=new char[];
String demo1("Visual C++");
String demo2("6.0");
demo1.display();
demo2.display();
String demo3=demo1+demo2;
demo3.display();
String demo4=demo3+"Programming .";
demo4.display();
delete str;
return ;
}

结果:

例3:

#include <iostream>
#include <string>
using namespace std;
class String
{
char name[];
public:
String(char* str){
cout<<"constructor,char*--->String\n";
strcpy(name,str);
}
String(String &s){
cout<<"copy constructor\n";
strcpy(name,s.name);
}
String(){
cout<<"default constructor\n";
}
~String(){
}
void operator=(const String&s){
strcpy(name,s.name);
cout<<"operator ="<<endl;
}
String operator+(const String&);
void display(){
cout<<"The string is:"<<name<<endl;
}
};
static char* str;
String String::operator+(const String& a){
String s;
cout<<"operator +\n";
strcpy(s.name,name);
strcat(s.name,a.name);
// return String(str);//等价于return str;进行自动类型转换
return s;
}
int main(){
str=new char[];
String demo1("Visual C++");
String demo2("6.0");
demo1.display();
demo2.display();
String demo3;
demo3=demo1+demo2;
cout<<"---------------\n";
demo3.display();
String demo4;
demo4=demo3+"Programming .";
demo4.display();
delete str;
cout<<"---------------\n";
String demo5=demo4;
demo5.display();
return ;
}

结果:

ps:注意拷贝构造函数的应用!23333

<2>友元函数重载运算符

c++中运算符重载的更多相关文章

  1. string类中运算符重载实现

    C++中预定义的加.减等运算符的操作对象只能是基本的数据类型.如果要在用户自定义的类型对象上应用同样的运算符,就需要通过运算符重载来重新定义其实现,使它能够用于自定义类型执行特定的操作,所以运算符重载 ...

  2. 08 c++中运算符重载(未完成)

    参考:轻松搞定c++语言 定义:赋予已有运算符多重含义,实现一名多用(比较函数重载) 运算符重载的本质是函数重载 重载函数的格式: 函数类型 operator 运算符名称(形参表列)  {  重载实体 ...

  3. 问题 B: 矩形类中运算符重载【C++】

    题目描述 定义一个矩形类,数据成员包括左下角和右上角坐标,定义的成员函数包括必要的构造函数.输入坐标的函数,实现矩形加法,以及计算并输出矩形面积的函数.要求使用提示中给出的测试函数并不得改动. 两个矩 ...

  4. c++中运算符重载,+,-,--,+=,-=,*,/,*=,/=,

    #include<iostream> #include<stdlib.h> using namespace std; class Complex { public: Compl ...

  5. Python 中的运算符重载

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理 一种运算符对于不同类型的对象,有不同的使用方式.例如, + 用于整型对象,表示两个数相加:用于字符串 ...

  6. C++之------运算符重载

    ①  什么是运算符重载? 何为C++的运算符重载呢? 其实就是运算符给它重新赋予新的含义或者多重含义.让它有另外一种新的功能. 为什么需要运算符重载? 面向对象中为了实现类的多态性,我们就引用了运算符 ...

  7. 雷林鹏分享:C# 运算符重载

    C# 运算符重载 您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户自定义类型的运算符.重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的.与其 ...

  8. C++运算符重载的妙用

    运算符重载(Operator overloading)是C++重要特性之中的一个,本文通过列举标准库中的运算符重载实例,展示运算符重载在C++里的妙用.详细包含重载operator<<,o ...

  9. 《挑战30天C++入门极限》C++运算符重载函数基础及其值返回状态

        C++运算符重载函数基础及其值返回状态 运算符重载是C++的重要组成部分,它可以让程序更加的简单易懂,简单的运算符使用可以使复杂函数的理解更直观. 对于普通对象来说我们很自然的会频繁使用算数运 ...

随机推荐

  1. Effective C++ 笔记:条款 30 inline

    30 : Understand the ins and outs of inlining 1 inline申请书 1.1 类内部实现函数包含隐藏的inline申请 class Human { publ ...

  2. spring-boot json数据交互

    SpringBoot学习之Json数据交互 最近在弄监控主机项目,对javaweb又再努力学习.实际的项目场景中,前后分离几乎是所以项目的标配,全栈的时代的逐渐远去,后端负责业务逻辑处理,前端负责数据 ...

  3. P1613 跑路(倍增 + floyd)

    https://www.luogu.org/problemnew/show/P1613 思路: 1.读入 2.建图 3.对于每一个点,向距离它 2^k 长度的点连一条长度为 1 的边 4.在新图上跑1 ...

  4. 移动端300ms延迟解决的几种方法;

    方案一:禁用缩放 当HTML文档头部包含如下meta标签时: <meta name="viewport" content="user-scalable=no&quo ...

  5. A - ACboy needs your help again!

    ACboy was kidnapped!! he miss his mother very much and is very scare now.You can't image how dark th ...

  6. 关于git 命令的一些事

    克隆代码命令 http://www.yiibai.com/git/git_clone.html 关键:得实现新建本地仓库文件夹 ==> git clone 远程网址 git 上传主要代码:htt ...

  7. 2019.02.28 bzoj3527: [Zjoi2014]力(fft)

    传送门 fftfftfft菜题. 题意简述:给一个数列aia_iai​,对于i=1→ni=1\rightarrow ni=1→n求出ansi=∑i<jai(i−j)2−∑i>jai(i−j ...

  8. ASM的一些小坑

    变量必需放到数据段,才有直接对地址赋值的访问权限 segment .data n1 dw 55h segment .text global _nasm_function _nasm_function: ...

  9. datatable实例教程

    网站的后台,多数是需要使用datatable来展示数据的,因为datatable的功能比较强大,可以更好的使用. 引用css <link href="../../static/asse ...

  10. ESP-IDF版本2.1.1

    版本2.1.1是一个错误修复版本.它包括对KRACK和BlueBorne漏洞的修复. 版本2.1.1的文档可在http://esp-idf.readthedocs.io/en/v2.1.1/上找到. ...