操作符重载

  • 自定义类型需要操作符重载
  • 运算符重载入门技术推演
  • 友元函数和成员函数实现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)的更多相关文章

  1. 【STL】重载运算符

    重载运算符 为什么要重载运算符: C++中预定义的运算符的操作对象只能是基本数据类型.但实际上,对于许多用户自定义类型(例如结构体),也需要类似的运算操作.这时就必须在C++中重新定义这些运算符,赋予 ...

  2. c++重载运算符注意

    c++重载运算符的时候加&或不加: 如果加了&表示引用,说明用的都是同一块内存.如果不加,那么用的就是一份拷贝,即不同的内存. 一般连续操作的时候要加&. 可以重新定义一个对象 ...

  3. [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)

    operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...

  4. C++结构体:默认构造函数,复制构造函数,重载=运算符

    C++结构体提供了比C结构体更多的功能,如默认构造函数,复制构造函数,运算符重载,这些功能使得结构体对象能够方便的传值. 比如,我定义一个简单的结构体,然后将其作为vector元素类型,要使用的话,就 ...

  5. C++习题 复数类--重载运算符2+

    Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意.例如,c1+ ...

  6. C++习题 复数类--重载运算符+

    Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.将运算符函数重载为非成员.非友元的普通函数.编写程序,求两个复数之和. Input ...

  7. C++中,用类和重载运算符写高精模板

    先放代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; s ...

  8. 从C过渡到C++的几个知识点(结构体、引用、重载运算符)

    一.结构体和类(class) 下面一个使用结构体类型的例子 #include <iostream> using namespace std; struct Point{ // 声明Poin ...

  9. c/c++ 重载运算符 函数调用运算符

    重载运算符 函数调用运算符 把一个类的对象a,当成函数来使用,比如a(),所以需要重载operator()方法.重载了函数调用运算符的类的对象,就是函数对象了. 还有什么是函数对象呢??? lambd ...

随机推荐

  1. wordpress小程序安装教程

    推荐服务器特价优惠注册即可购买,1G双核一年只要88,真的是白菜价格,点击下面图片即可进入购买地址. 开源小程序发布一段时间了,很多人最近咨询了关于小程序的教程,实在太忙了,抽空写个基本的安装教程. ...

  2. 使用Query Store监控性能

    Query Store是SQL Server 2016中引入的语句性能监控和调优工具,它不仅自动捕获查询.执行计划和运行时统计信息的历史记录,而且还可以识别出由于执行计划更改而导致的性能差异,简化了性 ...

  3. 使用 buildx 构建多平台 Docker 镜像

    原文链接:使用 buildx 构建多平台 Docker 镜像 在工作和生活中,我们可能经常需要将某个程序跑在不同的 CPU 架构上,比如让某些不可描述的软件运行在树莓派或嵌入式路由器设备上.特别是 D ...

  4. 【跟唐老师学习云网络】 - 第7篇 Tcpdump大杀器抓包

    [摘要] 前面章节的网络协议栈相关的信息建议大家多学习一遍,因为这些都是最基础的东西,想玩好云网络必备基本功.. 一.上帝视角 之前提到过定位问题可以开启上帝视角,那么如何开启就要依靠tcpdump这 ...

  5. 解密国内BAT等大厂前端技术体系-阿里篇(长文建议收藏)

    进入2019年,大前端技术生态似乎进入到了一个相对稳定的环境,React在2013年发布至今已经6年时间了,Vue 1.0在2015年发布,至今也有4年时间了. 整个业界在前端框架不断迭代中,也寻找到 ...

  6. react-native IOS TextInput长按提示显示为中文(select | selectall -> 选择 | 全选)

    根据手机系统语言(简体中文/英文),提示不同的长按效果 长按提示效果图 英文长按提示 中文长按提示 解决 1.手机系统语言为简体中文: 设置->通用->语言与地区 2.ios/项目/inf ...

  7. markdown语法之字体、字号、颜色以及背景色

    字体.字号与颜色 html标签:<font> font标签属性: face:字体 size:规定文本的尺寸大小. 可能的值:从 1 到 7 的数字. 浏览器默认值是 3. color: 颜 ...

  8. jQuery操作元素对象的样式

    在jQuery中操作元素为了加快速度,或者书写速度,可以用到json的格式: <!DOCTYPE html> <html> <head> <meta char ...

  9. JavaScript基础4

    数组 创建数组  A.通过构造函数创建数组 * a): var arr=new Array();//定义一个空数组,无长度的空数组. * b):var arr1=new Array(num); * 当 ...

  10. [TimLinux] MySQL InnoDB的外键约束不支持set default引用选项

    1. 外键 MySQL的MyISAM是不支持外键的,InnoDB支持外键,外键是MySQL中的三大约束中的一类:主键约束(PRIMARY KEY),唯一性约束(UNIQUE),外键约束(FOREIGN ...