重载操作符就是为了实现类的多态性,让运算符与类结合,产生新的含义。
使用类的成员函数或友元函数(类外的普通函数)实现。
//重载+,=,<<,>>
#include<iostream>
using namespace std;
//重载操作符一定要有一个对象参与
class CNum
{
public:
int m_a;
public:
CNum()
{
m_a = 0;
}
int operator=(int num) //重载 =
{
m_a = num;
return m_a;
}
int operator+(int num) //重载 +
{
return m_a + num;
}
int operator+(CNum& num) //使用引用是为了修改参数里面的值,下面的例子同理
{
return this->m_a + num.m_a;
}
};
// 类外重载的需要两个参数,第一个是符号左边的,第二个是符号右边的
int operator+(int a, CNum& num)
{
return a + num.m_a;
}
//重载输出
ostream& operator<<(ostream& os,CNum& num) //同样的这里返回值使用了引用,
//因为需要使用的就是os本身,而不是拷贝构造的os
{
os << num.m_a;
return os;
}
//重载输入
istream& operator<<(istream& is,CNum& num)
{
is >> num.m_a;
return is;
}
int main()
{
//----------对象在左边-----类内重载-------------
CNum aa;
aa = 10; // =
cout << aa.m_a << endl;
CNum bb;
bb = aa = 123; // =
cout << aa.m_a << " " << bb.m_a << endl;
CNum cc;
cc = aa + 100; // +
cout << cc.m_a << " " << aa.m_a << endl;
CNum dd;
dd = aa + bb; //+
cout << dd.m_a << endl;
//-----------对象在右边-----类外重载-------------
dd = 456 + aa;
cout << dd.m_a << endl;
cout << dd << endl; //重载 <<
cin >> dd; //重载 >>
cout << dd << endl;
system("pause");
return 0;
}
//单目运算符在类内重载就行
#include <iostream>
using namespace std;
class CNum
{
public:
int m_a;
public:
CNum()
{
m_a = 100;
}
public:
//同一个作用域里,相同的函数名,根据参数来判断是哪个
int operator++() //++a
{
m_a = m_a + 1;
return m_a;
}
int operator++(int a) //a++ 加之前得有个东西记录一下,所以有参数
{
a = m_a;
m_a = m_a + 1;
return a;
}
};
int main()
{
CNum aa;
cout << aa++ << endl;
cout << ++aa << endl;
//int a = 100;
//cout << a++ << endl;
//cout << ++a << endl;
system("pause");
return 0;
}
在之前写过链表类,如果在遍历的时候,不想使用pTemp=pTemp->pNext,诶我就想用++,可以这么改。
#include<iostream>
using namespace std;
struct Node
{
int nValue;
Node* pNext;
};
class CList //链表类
{
private:
Node* m_pHead; //链表头尾,长度
Node* m_pEnd;
int m_nLength;
public:
Node* begin() //接口函数
{
return m_pHead;
}
Node* end()
{
return NULL;
}
public:
CList() //创建空链表
{
m_pHead = NULL;
m_pEnd = NULL;
m_nLength = 0;
}
CList(int nCount) //创建有nCount个结点的链表
{
// 初始化成员变量
m_pHead = NULL;
m_pEnd = NULL;
m_nLength = 0;
for (int i = 0;i < nCount;i++)
{
Push_Back(0);
}
}
~CList()
{
while(m_pHead)
Pop_Front();
}
public:
void Push_Back(int nValue) //在链表上添加一个数
{
//创建一个结点,把值装进去
Node* node = new Node;
node->nValue = nValue;
node->pNext = NULL;
//把这个结点放到链表的尾部
if(!m_pHead) //如果现在没有链表的时候
{
m_pHead = node;
m_pEnd = node;
}
else //链表有结点的时候
{
m_pEnd->pNext = node;
m_pEnd = node;
}
m_nLength++;
}
void Pop_Front() //删除一个链表结点
{
//判断链表有没有结点
if(m_pHead == NULL)
return;
//只有一个结点的时候
if(m_pHead == m_pEnd)
{
delete m_pHead;
m_pHead = NULL;
m_pEnd = NULL;
m_nLength = 0;
return ;
}
//有多个结点的时候
Node *pDel = m_pHead;
m_pHead = m_pHead->pNext;
delete pDel;
pDel = NULL;
m_nLength--;
}
void Show() //测试代码
{
Node* pTemp = m_pHead;
while (pTemp)
{
cout << pTemp->nValue <<" ";
pTemp = pTemp->pNext;
}
cout << "nLength:"<<m_nLength<<endl;
}
};
class List_Iterator
{
private:
Node* m_pTemp;
public:
List_Iterator(Node* pTemp):m_pTemp(pTemp)
{
}
public:
Node* operator++(int a)
{
m_pTemp = m_pTemp->pNext;
return m_pTemp;
}
int operator*() //为了输出方便
{
return m_pTemp->nValue;
}
bool operator!=(Node* pTemp)
{
if(m_pTemp!=pTemp)
return true;
else
return false;
}
};
int main()
{
CList lst;
lst.Push_Back(1);
lst.Push_Back(2);
lst.Push_Back(3);
lst.Push_Back(4);
//============================
List_Iterator ite = lst.begin();
while(ite != lst.end())
{
cout << *ite << " ";
ite++;
}
cout << endl;
//================对比==========
Node* pTemp = lst.begin();
while(pTemp != lst.end())
{
cout << pTemp->nValue << " ";
pTemp = pTemp->pNext;
}
cout << endl;
//===================================
system("pause");
return 0;
}