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. POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)

    http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...

  2. POJ 2109

    #include<iostream> #include<stdio.h> #include<math.h> using namespace std; int mai ...

  3. 【poj2891-Strange Way to Express Integers】拓展欧几里得-同余方程组

    http://poj.org/problem?id=2891 题意:与中国剩余定理不同,p%ai=bi,此处的ai(i=1 2 3 ……)是不一定互质的,所以要用到的是同余方程组,在网上看到有人称为拓 ...

  4. Android上使用MP3格式录制声音

    0. 下载LAME 并解压缩 http://lame.sourceforge.net/download.php http://sourceforge.net/projects/lame/files/l ...

  5. DB2操作流程

    DB2如何创建表空间 如何创建数据库 如何创建缓冲池标签: db2数据库system脚本linuxwindows2012-06-13 19:16 8411人阅读 评论(0) 收藏 举报 版权声明:本文 ...

  6. Java:集合for高级循环遍历

    增强for循环: 格式:for(变量数据类型 要遍历的变量 :元素所在数组(集合)名称) 也即 for(Type element: array或collection) 使用foreach遍历集合: 只 ...

  7. 使用shell查看局域网中主机的IP地址

    此脚本只是快速查看所在局域网中其它主机的IP地址,如果对方设置了禁ping,则无法显示出此主机的IP地址: #!/bin/bash ` do ping -c2 .$i &>/dev/nu ...

  8. Oracle下的IF EXISTS()

    妈蛋..作为一个使用了SQL SERVER有4 5年的程序猿,开始用Oracle真他妈不习惯.写法真他妈不一样.比如像写个像IF EXISTS(SELECT * FROM sys.tables WHE ...

  9. HTML5塔防游戏——《三国塔防》 - Yorhom's Game Box

    h3{ font-size:20px; } HTML5塔防游戏--<三国塔防> 游戏介绍: <三国塔防>是一款基于HTML5和Javascript的2D塔防游戏.游戏中除了塔防 ...

  10. angularjs ngRoute demo

    <!doctype html> <html lang="en" ng-app="AMail"> <head> <met ...