c++-重载运算符(+-,++,--,+=,-=,cin,cout)
操作符重载
- 自定义类型需要操作符重载
- 运算符重载入门技术推演
- 友元函数和成员函数实现2元运算符重载
- 友元函数和成员函数实现1元运算符重载(前置++,前置--,后置++,后置--)
- 友元函数实现运算符重载应用场景
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
}
void printComplex()
{
cout << "( " << this->a << ", " << this->b << "i )" << endl;
}
friend Complex complexAdd(Complex &c1, Complex &c2);
//friend Complex operator+(Complex &c1, Complex &c2);
//friend Complex operator-(Complex &c1, Complex &c2);
Complex complexAdd(Complex &another)
{
Complex temp(this->a + another.a, this->b + another.b);
return temp;
}
Complex operator+(Complex &another)
{
Complex temp(this->a + another.a, this->b + another.b);
return temp;
}
Complex operator-(Complex &another)
{
Complex temp(this->a - another.a, this->b - another.b);
return temp;
}
private:
int a;//实数
int b;//虚数
};
Complex complexAdd(Complex &c1, Complex &c2)
{
Complex temp(c1.a + c2.a, c1.b + c2.b);
return temp;
}
//操作符重载写在全局
#if 0
Complex operator+(Complex &c1, Complex &c2)
{
Complex temp(c1.a + c2.a, c1.b + c2.b);
return temp;
}
Complex operator-(Complex &c1, Complex &c2)
{
Complex temp(c1.a - c2.a, c1.b - c2.b);
return temp;
}
#endif
int main(void)
{
Complex c1(1, 2);
Complex c2(2, 4);
c1.printComplex();
c2.printComplex();
//Complex c3 = complexAdd(c1, c2);
//Complex c3 = c1.complexAdd(c2);
//Complex c3 = c1 + c2; //operator+(c1, c2) 全局的调用方式
//c1.operator+(c2)
//Complex c3 = operator+(c1, c2);
Complex c3 = c1.operator+(c2);
c3.printComplex();
Complex c4 = c1 + c2;
c4.printComplex();
return 0;
}
双目运算符重载(-=,+=)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
}
void printComplex()
{
cout << "( " << this->a << ", " << this->b << "i )" << endl;
}
//friend Complex & operator+=(Complex &c1, Complex &c2);
friend Complex &operator-=(Complex &c1, Complex &c2);
Complex &operator+=(Complex &another)
{
this->a += another.a;
this->b += another.b;
return *this;
}
private:
int a;//实数
int b;//虚数
};
//全局
#if 0
Complex & operator+=(Complex &c1, Complex &c2)
{
c1.a += c2.a;
c1.b += c2.b;
return c1;
}
#endif
Complex &operator-=(Complex &c1, Complex &c2)
{
c1.a -= c2.a;
c1.b -= c2.b;
return c1;
}
int main(void)
{
Complex c1(1, 2);
Complex c2(2, 4);
(c1 += c2)+=c2;//c1.operator+=(c2) .operator(c2)
c1.printComplex();
c2.printComplex();
c1 -= c2;
c1.printComplex();
return 0;
}
单目运算符
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
}
void printComplex()
{
cout << "( " << this->a << ", " << this->b << "i )" << endl;
}
//friend Complex & operator++(Complex &c);
//friend const Complex operator++(Complex &c1, int);
Complex &operator++()
{
this->a++;
this->b++;
return *this;
}
const Complex operator++(int)//亚元
{
Complex temp(this->a, this->b);
this->a++;
this->b++;
return temp;
}
private:
int a;//实数
int b;//虚数
};
#if 0
//重载的是前++运算符
Complex & operator++(Complex &c)
{
c.a++;
c.b++;
return c;
}
#endif
//重载的是后++运算符
#if 0
const Complex operator++(Complex &c1, int)
{
Complex temp(c1.a, c1.b);
c1.a++;
c1.b++;
return temp;
}
#endif
int main(void)
{
Complex c1(1, 2);
//++++c1;
c1++;
c1.printComplex();
//++++c1;
return 0;
}
左移右移操作符重载
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
}
void printComplex()
{
cout << "( " << this->a << ", " << this->b << "i )" << endl;
}
friend ostream& operator<<(ostream & os, Complex &c);
friend istream & operator>>(istream &is, Complex &c);
//<<操作符只能写在全局,不能够写在成员方法中。否则调用的顺序会变饭,c1<<cout;
#if 0
ostream& operator<<(ostream &os) //c1.operator<<(cout)
{
os << "( " << this->a << ", " << this->b << "i )";
return os;
}
#endif
private:
int a;//实数
int b;//虚数
};
#if 1
ostream& operator<<(ostream & os, Complex &c)
{
os << "( " << c.a << ", " << c.b << "i )";
return os;
}
istream & operator>>(istream &is, Complex &c)
{
cout << "a:";
is >> c.a;
cout << "b:";
is >> c.b;
return is;
}
#endif
int main(void)
{
Complex c1(1, 2);
cin >> c1;//operaotr>>(cin, c1)
cout << c1;
//c1 << cout;
//cout.operator<<(c1);
//cout << c1 << " " <<c1<< endl;//operator<<(cout, c1);
return 0;
}
c++-重载运算符(+-,++,--,+=,-=,cin,cout)的更多相关文章
- 【STL】重载运算符
重载运算符 为什么要重载运算符: C++中预定义的运算符的操作对象只能是基本数据类型.但实际上,对于许多用户自定义类型(例如结构体),也需要类似的运算操作.这时就必须在C++中重新定义这些运算符,赋予 ...
- c++重载运算符注意
c++重载运算符的时候加&或不加: 如果加了&表示引用,说明用的都是同一块内存.如果不加,那么用的就是一份拷贝,即不同的内存. 一般连续操作的时候要加&. 可以重新定义一个对象 ...
- [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)
operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...
- C++结构体:默认构造函数,复制构造函数,重载=运算符
C++结构体提供了比C结构体更多的功能,如默认构造函数,复制构造函数,运算符重载,这些功能使得结构体对象能够方便的传值. 比如,我定义一个简单的结构体,然后将其作为vector元素类型,要使用的话,就 ...
- C++习题 复数类--重载运算符2+
Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意.例如,c1+ ...
- C++习题 复数类--重载运算符+
Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.将运算符函数重载为非成员.非友元的普通函数.编写程序,求两个复数之和. Input ...
- C++中,用类和重载运算符写高精模板
先放代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; s ...
- 从C过渡到C++的几个知识点(结构体、引用、重载运算符)
一.结构体和类(class) 下面一个使用结构体类型的例子 #include <iostream> using namespace std; struct Point{ // 声明Poin ...
- c/c++ 重载运算符 函数调用运算符
重载运算符 函数调用运算符 把一个类的对象a,当成函数来使用,比如a(),所以需要重载operator()方法.重载了函数调用运算符的类的对象,就是函数对象了. 还有什么是函数对象呢??? lambd ...
随机推荐
- Netty学习篇⑥--ByteBuf源码分析
什么是ByteBuf? ByteBuf在Netty中充当着非常重要的角色:它是在数据传输中负责装载字节数据的一个容器;其内部结构和数组类似,初始化默认长度为256,默认最大长度为Integer.MAX ...
- C博客作业00—我的第一篇博客
C博客作业00-我的第一篇博客 1. 你对网络专业或者计算机专业了解是怎样? 泛泛了解 - 原先只知道网络工程隶属于计算机工程学院,与院中其他专业一样,同样都需要学习大量的计算机基础知识,然后再分支学 ...
- 高性能消息队列(MQ)Kafka 简单由来介绍(1)
Kafka是由Apache软件基金会开发的一个开源流处理平台,由Scala和Java编写.Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者在网站中的所有动作流数据. 这种动作(网页浏 ...
- python字符串删除,列表删除以及字典删除的总结
一:字符串删除 1,字符串本身是不可变的,一个字符串定义以后,对他本身是不能做任何操作的,所以的增删改都是对原字符串拷贝的副本的操作,原来的字符串还是原来的字符串,它本身并没 有变 2,字符串本身是 ...
- 【Android - 组件】之Activity的启动模式
Activity的启动模式目前有四种:standard.singleTop.singleTask 和 singleInstance. 1.standard standard 是标准模式,也是系统的默认 ...
- Vsftp与PAM虚拟用户
使用yum 安装vsftp yum install vsftpd pam pam-* db4 db4-* 创建一个保存用户及密码的文件 cd /etc/vsftpd/ touch virtual_lo ...
- 远程连接mysql出现1045错误的解决办法
第一步:停止MySQL服务 第二步:在你MySQL的安装目录下找到my.ini,文件,打开文件查找到 [mysqld] ,在其下方添加上一行 skip-grant-tables,然后保存. 第三步:启 ...
- ThreadLocal 源码解读
一.引入 public class Thread implements Runnable { /* 前面略 */ /* ThreadLocal values pertaining to this th ...
- Unity 3D中C#的性能优化小陷阱
本篇内容主要来自Unity官方手册: 一般性能优化 一些地方为本人瞎编杜撰,请酌情参考.如有错误,欢迎指出. Unity里C#编程虽然既简单还很爽,但是性能小陷阱还不少.我总强迫自己让代码最优,因此很 ...
- springboot整合thymleaf模板引擎
thymeleaf作为springboot官方推荐使用的模板引擎,简单易上手,功能强大,thymeleaf的功能和jsp有许多相似之处,两者都属于服务器端渲染技术,但thymeleaf比jsp的功能更 ...