当前位置:网站首页>Operator overloading
Operator overloading
2022-07-29 07:56:00 【A tong Muyou】
+,- Operator overloading
You need to access private functions , It can be placed in a class , You can also use friend functions , take operator+ Treat as friend .
com com::operator+(com &obj)
{
com t;
t.m_a = obj.m_a + m_a;
t.m_b = obj.m_b + m_b;
return t;
}Operator overload rule
1. Cannot overload : Scope qualifiers (::), Ternary operator (? :), comma (,),(.*),(sizeof)
2. Overloading cannot change priority and associativity
3. Its operand cannot be changed
4. Cannot have default parameters
Reload step
1. Write the function name
2. Write formal parameters according to operands
3. Write out the return value type according to the usage scenario
4. Operator overloading cannot have default parameters , Otherwise, the operator operands will be changed
Overload output , Input operator
Need to overload outside the class , Because there is ostream And istream
#include <iostream>
using namespace std;
class com
{
friend ostream& operator<<(ostream &out, const com &obj);
private:
int m_a;
int m_b;
public:
com():m_a(1), m_b(2)
{
}
com(int a, int b)
{
m_a = a;
m_b = b;
}
void print()
{
cout << m_a << " + " << m_b << endl;
}
void print1()
{
cout << m_a << " - " << m_b << endl;
}
com operator++();
com operator++(int);
bool operator&&(com& b);
com operator+(com &obj);
com operator-(com &obj);
};
com com::operator-(com &obj)
{
com t;
t.m_a = m_a - obj.m_a;
t.m_b = m_b - obj.m_b;
return t;
}
com com::operator+(com &obj)
{
com t;
t.m_a = obj.m_a + m_a;
t.m_b = obj.m_b + m_b;
return t;
}
bool com::operator&&(com& b)
{
if(m_a == 0 && m_b == 0)
return false;
else
if(b.m_a == 0 && b.m_b)
return false;
else
return true;
}
com com::operator++()
{
this->m_a++;
this->m_b++;
return *this;
}
com com::operator++(int)
{
com t = *this;
m_a++;
m_b++;
return t;
}
ostream& operator<<(ostream &out, const com &obj)
{
out << obj.m_a << " " << obj.m_b;
return out;
}
int main(int argc, char *argv[])
{
com c1(2,0);
com c2(0,1);
com c3(0,0);
// c1++;
// ++c1;
// cout <<++c1<< endl;
if(c3 && (c2.operator+(c1)))
{
cout << "ss" << endl;
}
else
{
cout << "frue" << endl;
}
}
Overloaded logical operators
Can overload , But the short circuit principle cannot be realized
边栏推荐
- 关于pip升级损坏导致的问题记录
- Data unit: bit, byte, word, word length
- Popular cow G
- C language data type
- You study, I reward, 21 day learning challenge | waiting for you to fight
- MySQL uses date_ FORMAT(date,'%Y-%m')
- Day 014 2D array exercise
- Measured waveform of boot capacitor short circuit and open circuit of buck circuit
- [skill accumulation] presentation practical skill accumulation, common sentence patterns
- Useful websites
猜你喜欢

Analyze the roadmap of 25 major DFI protocols and predict the seven major trends in the future of DFI
![[untitled] format save](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled] format save
![[deep learning] data preparation -pytorch custom image segmentation data set loading](/img/7d/61be445febc140027b5d9d16db8d2e.png)
[deep learning] data preparation -pytorch custom image segmentation data set loading

The new generation of public chain attacks the "Impossible Triangle"
![[paper reading] tomoalign: a novel approach to correcting sample motion and 3D CTF in cryoet](/img/3a/75c211f21758ca2d9bb1a40d739d80.png)
[paper reading] tomoalign: a novel approach to correcting sample motion and 3D CTF in cryoet

Excellent urban design ~ good! Design # visualization radio station will be broadcast soon

QT connects two qslite databases and reports an error qsqlquery:: exec: database not open

Research on autojs wechat: the final product of wechat automatic information sending robot (effective demonstration)
![[cryoelectron microscope | paper reading] interpretation of sub fault average m software: multi particle cryo EM refining with M](/img/5e/69987afcd1e50ba37bc49441dd3a50.png)
[cryoelectron microscope | paper reading] interpretation of sub fault average m software: multi particle cryo EM refining with M

MySQL 45讲 | 08 事务到底是隔离的还是不隔离的?
随机推荐
LANDSCAPE
Limitations of push down analysis
String class
Pytorch's skill record
Matrix decomposition and gradient descent
[cryoelectron microscope] relation4.0 - subtomogram tutorial
Data unit: bit, byte, word, word length
Compare three clock circuit schemes of single chip microcomputer
FLink CDC 的mysql connector中,mysql的字段是varbinary, 官方
[memo] summary of the reasons why SSH failed? Remember to come next time.
Mutationobserver document learning
【深度学习】数据准备-pytorch自定义图像分割类数据集加载
LVM logical volume group management
功能自动化测试实施的原则以及方法有哪些?
CentOS deploy PostgreSQL 13
Greenplus enterprise deployment
Android interview question | how to write a good and fast log library?
MapReduce各阶段步骤
Write some DP
Measured waveform of boot capacitor short circuit and open circuit of buck circuit