operator重载的使用
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重载的使用的更多相关文章
- C++ operator重载运算符和隐式转换功能的实现
C++ operator重载运算符和隐式转换功能的实现: #include <iostream> using namespace std; class OperatorTest { pub ...
- operator重载运算符
1.重载运算符的函数一般格式如下 函数类型 operator 运算符名称 (形参表列) {对运算符的重载处理} 例如,想将"+"用于Complex(复数)的加法运算, ...
- operator 重载内置运算符
operator 关键字来重载内置运算符,或提供类或结构声明中的用户定义转换.它可以定义不同类型之间采用何种转化方式和转化的结果. operator用于定义类型转化时可采用2种方式,隐式转换(impl ...
- Spline样条函数 //C++关键字:operator // 重载函数 // 隐含的this指针 // 指针和const限定符
在数学学科数值分析中,样条是一种特殊的函数,由多项式分段定义.样条插值是使用一种名为样条的特殊分段多项式进行插值的形式.由于样条插值可以使用低阶多项式样条实现较小的差值误差,这样就避免了使用高阶多项式 ...
- operator[] 重载
#include <iostream>#include <vector>#include <string> class Assoc { struct Pair ...
- operator ->重载是怎么做到的?
https://stackoverflow.com/questions/8777845/overloading-member-access-operators-c struct client { in ...
- [019]转--C++ operator关键字(重载操作符)
原博客:http://www.cnblogs.com/speedmancs/archive/2011/06/09/2076873.html operator是C++的关键字,它和运算符一起使用,表示一 ...
- C++的重载操作符(operator)介绍(转)
本文主要介绍C++中的重载操作符(operator)的相关知识. 1. 概述 1.1 what operator 是C++的一个关键字,它和运算符(如=)一起使用,表示一个运算符重载函数,在理解时可将 ...
- c++ 运算符重载operator
一般格式为: 函数类型 operator 运算符名称(形参列表){ 对运算符的重载 } 注意函数名是由operator和运算符组成.在上面的一般格式中,operator是关键字,是专门用于重载运算符函 ...
随机推荐
- solr的collection,shard,replica,core概念
一.collection 1.由多个cores组成一个逻辑索引叫做一个collection.一个collection本质上是一个可以跨越多个核的索引,同时包含冗余索引. 2.collection由不同 ...
- Ajax的进阶学习(一)
在Ajax课程中,我们了解了最基本的异步处理方式.本章,我们将了解一下Ajax的一些全局请求事件.跨域处理和其他一些问题. 加载请求 在Ajax异步发送请求时,遇到网速较慢的情况,就会出现请求时间较长 ...
- eclipse下python的selenium自动化环境的搭建
前提:安装python,我用的2.7.8版本,并在环境变量path里设置;E:\Python1.解压setuptools(Python包管理工具),cmd到目录执行python setup.py in ...
- vim使用详解
1 插入类命令 i // 在当前字符前插入 I // 在当前行首插入 a // 在当前字符后写入 A ...
- C++ 数组参数
在C++中数组不是按值传递的,传递的是第1个元素,即第0个元素的指针. 1.数组长度不是参数类型的一部分,函数不知道传递给它的数组的实际长度,因此当编译器对实参类型进行参数类型检查时,并不检查数组的长 ...
- java.lang.ClassCastException: sun.jdbc.odbc.JdbcOdbcStatement cannot be cast to java.beans.Statement
当导入的包为:import java.sql.Statement;时,无任何错误 当导入的包为:import java.beans.Statement;时,出错
- ruby 学习 -- Array --2
定义: [1, 2, 3] # An array that holds three Fixnum objects [-10...0, 0..10,] # An array of two ranges; ...
- 批处理命令 - if
0.功能 Performs conditional processing in batch programs. 执行批处理程序中的条件处理. 1.简介 IF [NOT] ERRORLEVEL numb ...
- Sqoop详细介绍包括:sqoop命令,原理,流程
一简介 Sqoop是一个用来将Hadoop和关系型数据库中的数据相互转移的工具,可以将一个关系型数据库(例如 : MySQL ,Oracle ,Postgres等)中的数据导进到Hadoop的HDFS ...
- QT静态编译
Qt静态编译(链接)和动态编译区别 Qt的静态编译译(其实应该叫链接,不是编译),将各模块编译成静态库,这样在编译自己写的Qt程序时,会将这些静态库编译(链接)到你的EXE文件中去的.Qt的动态编译, ...