操作符重载

  • 自定义类型需要操作符重载
  • 运算符重载入门技术推演
  • 友元函数和成员函数实现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. Fortran流程控制与逻辑运算、循环--xdd

    1.IF语句 1 if() then ... end if 2 if() then ... else ... end if 3 if() then ... else if() then ... els ...

  2. (四十五)golang--反射

    反射基本介绍: (1)反射可以在运行时动态获取变量的各种信息,比如变量的类型.类别: (2)如果是结构体变量,还可以获取结构体本身的信息(包括结构体字段.方法): (3)通过反射,可以修改变量的值,可 ...

  3. 30L,17L,13L容器分油,python递归,深度优先算法

    伪代码: 全部代码: a=[] b=[] def f(x,y,z): b.append([x,y,z]) if x==15 and y==15: print(x,y,z) i=0; for x in ...

  4. (四十六)golang--网络编程(简易的聊天系统)

    Go主要的目标之一就是面向大规模后端服务程序,网络通信这块是服务端程序必不可少也是至关键的一部分. 网络编程有两种: (1)TCP Socket编程:是网络编程的主流,之所以叫TCP Socket编程 ...

  5. Web渗透测试流程

    什么是渗透测试? 渗透测试 (penetration test)并没有一个标准的定义,国外一些安全组织达成共识的通用说法是:渗透测试是通过模拟恶意黑客的攻击方法,来评估计算机网络系统安全的一种评估方法 ...

  6. Chapter 02—Creating a dataset(Part3-补充材料Stat/Transfer)

    Stat/Transfer:在电子表格(worksheet),数据库(database),统计包(statistical package)间进行数据转换,具有简单高效的特点. 资料来源于:http:/ ...

  7. Spring 读取资源

    Spring 读取资源 主要介绍3种方式(当然不止三种,但是这三种基本能应付大多需求)FileSystemResource:以文件的绝对路径方式进行访问ClassPathResourcee:以类路径的 ...

  8. 基于PyTorch实现MNIST手写字识别

    本篇不涉及模型原理,只是分享下代码.想要了解模型原理的可以去看网上很多大牛的博客. 目前代码实现了CNN和LSTM两个网络,整个代码分为四部分: Config:项目中涉及的参数: CNN:卷积神经网络 ...

  9. Java 基于Spire.Cloud.Excel 将Excel转为PDF

    Spire.Cloud.Excel Sdk 提供GeneralApi接口和WorkbookApi接口,支持将本地Excel和云端Excel文档转换为ODS, PDF, XPS, PCL, PS等格式. ...

  10. 小白学 Python 爬虫(15):urllib 基础使用(五)

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...