C++的大多数运算符都可以通过operator来实现重载。

简单的operator+

#include <iostream>
using namespace std; class A
{
public:
A(int x){i=x;}
int i;
A &operator+(A &p)
{
this->i+=p.i;
return *this;
}
}; void main()
{
A a(),b(),c();
c=a+b;
cout<<c.i<<endl;
system("pause");
}

简单的operator++

#include <iostream>
using namespace std; class A
{
public:
A(int x){i=x;}
int i;
A &operator++()
{
this->i++;
return *this;
}
}; void main()
{
A a();
a++;
cout<<a.i<<endl;
system("pause");
}

深层operator=

#include <iostream>
using namespace std; class A
{
public:
A(int x){i=new int;*i=x;}
~A(){delete i;i=;}
A(const A&p)
{
i=new int;
*i=*(p.i);
}
int *i;
A &operator=(A &p)
{
*i=*(p.i);
return *this;
}
int pp(){return *i;}
}; void main()
{
A a(),b();
a=b;
cout<<a.pp()<<endl;
system("pause");
}

简单的输出运算符operator<<

注意:由于ostream类没有公有的复制构造函数,因此函数无法调用该类的复制构造函数来复制对象,必须按照引用的方式来接收与返回ostream对象。

#include <iostream>
using namespace std; class A
{
public:
A(int x,int y){rx=x;ry=y;}
int rx;
int ry;
}; ostream &operator << (ostream &s,const A &c)//cout是ostream的对象
{
s<<c.rx;//使用cout输出对象的值,语句可以直接翻译成cout<<c.rx
s<<c.ry;//使用cout输出对象的值,语句可以直接翻译成cout<<c.ry
return s;//返回cout
} void main()
{
A a(,),b(,);
cout<<a<<b;//(ostream &s,const A &c)==(cout,a)
system("pause");
}

简单的输出运算符-使用友元的方式operator<<

#include <iostream>
using namespace std; class A
{
public:
A(int x,int y){rx=x;ry=y;}
friend ostream &operator << (ostream &s,const A &c)
{
s<<c.rx;
s<<c.ry;
return s;
}
private:
int rx;
int ry;
}; void main()
{
A a(,),b(,);
cout<<a<<b;
system("pause");
}

operator++(++i与i++的实现)

#include <iostream>
using namespace std; class A
{
public:
A(int x){rx=x;}
int operator++() //++i
{
this->rx++; //其实这里this指针可以不用写因为你直接写rx++效果也是一样的,编译器会在编译的时候自动的为你加上this
return rx; //但是刚开始学语言的时候建议写一下可以加深this指针的概念
}
int operator++(int) //i++
{
int i=;
i=this->rx;
this->rx++;
return i;
}
friend ostream &operator << (ostream &s,const A &c)
{
s<<c.rx;
return s;
}
private:
int rx;
}; void main()
{
A a(),b();
cout<<a++<<endl;
cout<<++b<<endl;
system("pause");
}

operator输入输出,自增运算符

#include <iostream>
using namespace std; class A
{
public:
A(int x){rx=x;}
int operator++() //++i
{
this->rx++; //其实这里this指针可以不用写因为你直接写rx++效果也是一样的,编译器会在编译的时候自动的为你加上this
return rx; //但是刚开始学语言的时候建议写一下可以加深this指针的概念
}
int operator++(int) //i++
{
int i=;
i=this->rx;
this->rx++;
return i;
}
friend istream &operator >> (istream &s,A &c) //输入操作符重载
{
s>>c.rx;
return s;
}
friend ostream &operator << (ostream &s,const A &c) //输出操作符重载
{
s<<c.rx;
return s;
}
private:
int rx;
}; void main()
{
A a(),b();
cin>>a;
cin>>b;
cout<<a++<<endl;
cout<<++b<<endl;
system("pause");
}

operator重载的使用的更多相关文章

  1. C++ operator重载运算符和隐式转换功能的实现

    C++ operator重载运算符和隐式转换功能的实现: #include <iostream> using namespace std; class OperatorTest { pub ...

  2. operator重载运算符

    1.重载运算符的函数一般格式如下 函数类型    operator  运算符名称    (形参表列) {对运算符的重载处理} 例如,想将"+"用于Complex(复数)的加法运算, ...

  3. operator 重载内置运算符

    operator 关键字来重载内置运算符,或提供类或结构声明中的用户定义转换.它可以定义不同类型之间采用何种转化方式和转化的结果. operator用于定义类型转化时可采用2种方式,隐式转换(impl ...

  4. Spline样条函数 //C++关键字:operator // 重载函数 // 隐含的this指针 // 指针和const限定符

    在数学学科数值分析中,样条是一种特殊的函数,由多项式分段定义.样条插值是使用一种名为样条的特殊分段多项式进行插值的形式.由于样条插值可以使用低阶多项式样条实现较小的差值误差,这样就避免了使用高阶多项式 ...

  5. operator[] 重载

    #include <iostream>#include <vector>#include <string> class Assoc {    struct Pair ...

  6. operator ->重载是怎么做到的?

    https://stackoverflow.com/questions/8777845/overloading-member-access-operators-c struct client { in ...

  7. [019]转--C++ operator关键字(重载操作符)

    原博客:http://www.cnblogs.com/speedmancs/archive/2011/06/09/2076873.html operator是C++的关键字,它和运算符一起使用,表示一 ...

  8. C++的重载操作符(operator)介绍(转)

    本文主要介绍C++中的重载操作符(operator)的相关知识. 1. 概述 1.1 what operator 是C++的一个关键字,它和运算符(如=)一起使用,表示一个运算符重载函数,在理解时可将 ...

  9. c++ 运算符重载operator

    一般格式为: 函数类型 operator 运算符名称(形参列表){ 对运算符的重载 } 注意函数名是由operator和运算符组成.在上面的一般格式中,operator是关键字,是专门用于重载运算符函 ...

随机推荐

  1. cf div2 238 D

    D. Toy Sum time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  2. Linux下安装Scim-googlepinyin输入法和设置Sublime Text中文输入

    1.安装git sudo apt-get install git http://www.cnblogs.com/perseus/archive/2012/01/06/2314069.html 2.获取 ...

  3. OnClientClick="return confirm('确定要删除吗?')"

    OnClientClick="return confirm('确定要删除吗?')"   -----------------------前台代码 OnClientClick用于执行客 ...

  4. jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<Model>类型的子List<Model>的高级使用方法

    比如JsonResult中返回return Json(models);的models结构如下: models返回含有四个集合的序列,每个集合的序列中又包含一个子集合序列“Child”. 问题是如果我们 ...

  5. MongoDB (三) MongoDB 安装

    MongoDB安装在Windows上 在 Windows上,首先要安装 MongoDB下载最新发布的MongoDB: http://www.mongodb.org/downloads 确保得到正确的版 ...

  6. Shell练习 统计单词个数,降序排列

    原文:https://leetcode.com/problems/word-frequency/ Write a bash script to calculate the frequency of e ...

  7. libprotobuf ERROR

    google/protobuf/wire_format.cc:1059] Encountered string containing invalid UTF-8 data while parsing  ...

  8. Android SlidingMenu侧滑菜单使用

    把下载的侧滑菜单压缩包打开,会有一个library文件夹,在eclipse中import existing android code into workspace,导入library文件夹,并且选择作 ...

  9. phantomjs + selenium headless test

    1. 安装selenium pip install selenium 2. 安装phantomjs 如果你是Ubuntu12.04,默认安装的版本是1.4.这个会出错. 需要安装1.9.7 cd /u ...

  10. Spring笔记——Spring+JDBC组合开发

      使用Spring+JDBC集成步骤如下:   1. 配置数据源 2. 配置事务.配置事务时,需要在xml配置文件中引入用于声明事务的tx命名空间,事务的配置方式有两种:注解方式和基于XML配置方式 ...