C++——运算符重载
运算符重载编程基础
例如:
//全局函数 完成 +操作符 重载 Complex operator+(Complex &c1, Complex &c2)
//类成员函数 完成 -操作符 重载 Complex operator-(Complex &c2)
#include <iostream>
using namespace std; class Complex
{
public:
int a;
int b;
public:
Complex(int a=, int b=)
{
this->a = a;
this->b = b;
}
void printCom()
{
cout<<a<<" + " << b << "i" <<endl;
}
}; //2 函数名 升级
Complex operator+(Complex &c1, Complex &c2)
{
cout<<"12345上山 打老虎"<<endl;
Complex tmp(c1.a + c2.a, c1.b+ c2.b);
return tmp; //
} void main()
{ int a = , b = ;
int c;
c = a + b; //1 基础数据类型 编译器已经知道了. 如何运算 // a + bi 复数运算规则
Complex c1(, ), c2(, );
Complex c3; //2 类 也是一种数据类型 用户自定义数据类型 C++编译器 是不知道如何进行运算 //步骤3
Complex c4 = c1 + c2;
c4.printCom(); //总结: 1 运算符重载的本质 是 函数调用 cout<<"hello..."<<endl;
system("pause");
return ;
}
二、实列
#include <iostream>
using namespace std; /*
class ostream
{ };
*/ class Complex
{
private:
int a;
int b;
//friend void operator<<(ostream &out, Complex &c1);
friend ostream& operator<<(ostream &out, Complex &c1); public:
Complex(int a=, int b=)
{
this->a = a;
this->b = b;
}
void printCom()
{
cout<<a<<" + " << b << "i" <<endl;
}
public: //实现 + 运算符重载
Complex operator+(Complex &c2)
{
Complex tmp(a + c2.a, b + c2.b);
return tmp;
} //前置++
Complex& operator++()
{
a++;
b++;
return *this;
} //后置++
Complex operator++(int)
{
//先使用 在让c1加加
Complex tmp = *this;
//return c1;
this->a ++;
this->b ++;
return tmp;
}
//成员函数法 实现 -运算符重载
Complex operator-(Complex &c2)
{
Complex tmp(this->a - c2.a, this->b - c2.b);
return tmp;
} //前置--
Complex& operator--()
{
this->a --;
this->b --;
return *this;
} //后置--
Complex operator--(int)
{
Complex tmp = *this;
this->a--;
this->b--;
return tmp;
}
}; void main31()
{
Complex c1(, ), c2(, ); //1 全局函数法 实现 + 运算符重载
// Complex operator+(Complex &c1, Complex &c2);
Complex c3 = c1 + c2;
c3.printCom(); //2 成员函数 法 实现 -运算符重载
//c1.operator-(c2);
//Complex operator-(Complex &c2)
Complex c4 = c1 - c2;
c4.printCom(); //前置++操作符 用全局函数实现
++c1;
c1.printCom(); //前置--操作符 成员函数方法
--c1;
c1.printCom();
//Complex& operator++(Complex &c1)
//c1.operator--(); //后置++操作符 用全局函数实现
c1++;
c1.printCom(); //后置--操作符 用成员函数实现
c1--;
c1.printCom();
//c1.operator--() cout<<"hello..."<<endl;
system("pause");
return ;
} /*
void operator<<(ostream &out, Complex &c1)
{
out<<"12345 生活真是苦"<<endl;
out<<c1.a << " + " << c1.b << "i" << endl;
}
*/ ostream& operator<<(ostream &out, Complex &c1)
{
out<<"12345 生活真是苦"<<endl;
out<<c1.a << " + " << c1.b << "i" << endl;
return out;
} void main()
{
int a = ;
Complex c1(, ), c2(, );
cout<<a<<endl; //按照数据类型 //
cout << c1 ;
//2 ostream 类中 添加 成员函数 .operator<<
//ostream
//cout.operator<<(c1); //2 函数返回值当左值 需要返回一个引用
cout << c1 << "aaddddd";
//
//cout.operator<<(c1) .operator<<("aaddddd");
//void.operator<<("aaddddd"); system("pause");
}
C++——运算符重载的更多相关文章
- C++ 运算符重载时,将运算符两边对象交换问题.
在C++进行运算符重载时, 一般来讲,运算符两边的对象的顺序是不能交换的. 比如下面的例子: #include <iostream> using namespace std; class ...
- C#高级编程笔记2016年10月12日 运算符重载
1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...
- C++运算符重载
C++运算符重载 基本知识 重载的运算符是具有特殊名字的函数,他们的名字由关键字operator和其后要定义的运算符号共同组成. 运算符可以重载为成员函数和非成员函数.当一个重载的运算符是成员函数时, ...
- 标准C++之运算符重载和虚表指针
1 -> *运算符重载 //autoptr.cpp #include<iostream> #include<string> using namespace std ...
- python运算符重载
python运算符重载就是在解释器使用对象内置操作前,拦截该操作,使用自己写的重载方法. 重载方法:__init__为构造函数,__sub__为减法表达式 class Number: def __in ...
- PoEduo - C++阶段班【Po学校】-Lesson03-5_运算符重载- 第7天
PoEduo - Lesson03-5_运算符重载- 第7天 复习前面的知识点 空类会自动生成哪些默认函数 6个默认函数 1 构造 2 析构 3 赋值 4 拷贝构造 5 oper ...
- 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换
[源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自 ...
- 我的c++学习(8)运算符重载和友元
运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能.这个函数叫做运算符重载函数(常为类的成员函数). 方法与解释 ◆ 1.定义运 ...
- c/c++面试题(6)运算符重载详解
1.操作符函数: 在特定条件下,编译器有能力把一个由操作数和操作符共同组成的表达式,解释为对 一个全局或成员函数的调用,该全局或成员函数被称为操作符函数.该全局或成员函数 被称为操作符函数.通过定义操 ...
- 实验12:Problem H: 整型数组运算符重载
Home Web Board ProblemSet Standing Status Statistics Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...
随机推荐
- wangEditor 图片上传失败提示
wangEditor 官网自定义上传事件:https://www.kancloud.cn/wangfupeng/wangeditor2/123689 声明:我用的wangEditor版本是2.1.23 ...
- 原型对象(JS中的父类)
原型 prototype 我们所创建的每一个函数,解析器都会向函数中添加 一个属性prototype ,这个属性对应的对象就是我们所谓的原型对象 判断函数中是否含有prototype属性,有则返回 ...
- python的magic methods
https://pycoders-weekly-chinese.readthedocs.io/en/latest/issue6/a-guide-to-pythons-magic-methods.htm ...
- 使用tensorboard报错 ImportError: No module named past.builtins
安装 future pip install future conda install future
- Java 的信号灯
Semaphore可以维护当前访问自身的线程个数,并提供了同步机制.使用Semaphore可以控制同时访问资源的线程个数,例如,实现一个文件允许的并发访问数. Semaphore实现的功能就类似厕所有 ...
- 自定义实现系统max方法
function MyMath(){ //添加了一个方法 this.getMax=function(){ //所有数字中的最大值 var max=arguments[0]; for(var i=0;i ...
- kubectl 使用token的方式连接到集群
首先得有一个账户 kubectl create serviceaccount dashboard-admin -n kube-system #创建一个名叫dashboard-admin 命名空间在ku ...
- mysql服务命令行操作
启动 net start mysql 关闭 net stop mysql 登陆 mysql -hlocalhost -uusername -ppassword 退出 exit 显示数据库 show d ...
- rasa学习(domain.yml、nlu.md、stories.md)(一)
一. 什么是rasa Rasa是一个用于自动文本和基于语音的对话的开源机器学习框架.了解消息,保持对话以及连接到消息传递通道和API Rasa分为Rasa core和 Rasa nlu两部分: Ras ...
- Oracle sql判断一个字段是否全数字 或含有中文
update (select length(t.name), t.* -- name,length(name) from g_enterprise_info t where nvl2(translat ...