操作符重载

  • 自定义类型需要操作符重载
  • 运算符重载入门技术推演
  • 友元函数和成员函数实现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. python字符串、正则-xdd

    1.分割字符串 str.split(sep,maxsplit) #(分隔符,分几次) 2.合并字符串 str2=string.join(iterable) #str2='@'.join(list1) ...

  2. linux ftp配置及实操

    一.基础知识: 1.ftp:file transfer protocal 及文件传输协,工作与应用层. 2.ftp协议的实现: 服务器端实现软件:vsftpd,pureftpd,filezilla s ...

  3. redis的主从复制,以及使用sentinel自动处理主机宕机问题,集群

    以下部分想看懂得有一定的redis基础,且步骤是连贯的,错一步都不行.redis运行多个实例,不懂得自行百度. 1. redis主从同步 原理: 从服务器向主服务器发送 SYNC 命令. 接到 SYN ...

  4. 如何平滑优雅地在Rancher 2.x中升级cert-manager?

    作者: Nassos Michas丨European Dynamics SA, CTO 如果你正在使用由Rancher提供的Helm Chart在Rancher管理的Kubernetes集群中安装ce ...

  5. R 语言学习笔记(1)——R 工作空间与输入输出

    什么是工作空间? 工作空间(workspace)就是当前 R 的工作环境,它储存着所有用户定义的对象(objectives)包括了向量.矩阵.函数.数据框.列表等. 处理 R 文件的工作流程 #设置当 ...

  6. 一个null,差点把系统给弄崩溃了

    今天生产上面发现了一个奇异的bug,URL上面会带上一个ID,这个ID是关联别的系统的,类似这种格式 xxx.xxx.xxx.xxx  ,是别的系统自己填写的,我们的URL会带上id=xxx.xxx. ...

  7. 把Git Repository建到U盘上去

    Git很火.原因有三: 它是大神Linus Torvalds的作品,天然地具备神二代的气质和品质: 促进了生产力的发展,Git的分布式版本控制理念,并非首创,但非常适合开源社区的协作方式(不存在mas ...

  8. 搞IT产品,请谨记Mobile First

    我们在哪儿? 作为一名企业IT的老鸟,发现一个比较有意思的事情,就是我们的企业IT产品,仍然投入大量的精力,在基于PC的WEB端的设计和交付上,而在APP上的,移动端的考虑,一直都是在PC搞完之后,再 ...

  9. 用C在GBA上写光线追踪(0)配置开发编译环境

    前段时间用C#写了一个光线追踪程序,可以渲染圆球,平面这种基本图形,反射,光照,阴影,都大致尝试做了一下. ↑ C#实现的光线追踪     ↑ GBA上C实现的光线追踪 然而,在我打算继续深入优化的时 ...

  10. shell 循环读取文件及字符串转为数组

    文件/etc/hdocker_config内容如下: 30.72.63.94 30.72.63.95 30.72.63.96 30.72.63.97 /tmp/lasclocker.tar maste ...