运算符重载
运算符:单目/双目/三目
注意:
1.不能改变优先级,结合性
2.不能改变所需要的操作数
3.不能创建新的运算符
单目运算符
单目运算符:++ --(分为前置和后置)
前置运算符:
运算符重载本质是一个成员函数;
成员函数方法:
<返回值> operator<运算符>(<参数列表>)
CPoint operator ++( );
友元函数
friend <返回值> operator <运算符>(<参数列表>)
friend CPoint operator++(CPoint &c)
class CPoint
{
private:
int x;
int y;
public:
CPoint(int x,int y)
{
this->x = x;
this->y = y;
}
~CPoint(){}
void show()
{
cout << "(" << x << "," << y << ")" << endl;
}
//CPoint operator++(); //成员函数的实现
friend CPoint operator++(CPoint &c);
};
/*CPoint CPoint::operator++()
{
this->x++;
this->y++;
return *this;
}*/
CPoint operator++(CPoint &c)
{
c.x++;
c.y++;
return c;
}
int main()
{
CPoint c1(3,4);
c1.show();
(++c1).show();
system("pause");
return 0;
}
后置运算符
成员函数:
<返回值> operator <运算符>(<参数列表>)
CPoint operator++(int); (this指针)
int没有任何实际意义,仅仅是后置单目运算符的标志
友元函数:
friend <返回值> operator <运算符>(<参数列表>)
friend CPoint operator++(CPoint &c,int)
1.参数列表 需要加一个int作为标识符
2.实现:先用一个中转对象保存原数值
#include <iostream>
using namespace std;
class CPoint
{
private:
int x;
int y;
public:
CPoint(int x,int y)
{
this->x = x;
this->y = y;
}
~CPoint(){}
void show()
{
cout << "(" << x << "," << y << ")" << endl;
}
CPoint operator++(int); //成员函数实现
friend CPoint operator++(CPoint &c,int);
};
CPoint CPoint::operator++(int)
{
CPoint temp = *this; //后置++ 需要返回最初的值
this->x++;
this->y++;
return temp;
}
CPoint operator++(CPoint &c,int)
{
CPoint c1 = c; //c1保存最初的值
c.x++;
c.y++;
return c1;
}
int main()
{
CPoint c1(3,4);
c1.show(); //(3,4)
(c1++).show(); // (3,4)
c1.show(); //(4,5)
system("pause");
return 0;
}
双目运算符
+ - * /
成员函数:
<返回值> operator <运算符>(<参数列表>)
Complex operator+(Complex c2);
参数列表中参数表示右操作数,左操作数用this指针操作
友元函数:
friend Complex operator+(Complex c1,Complex c2)
用成员函数时候 总会有一个this指针指向其中一个操作数;
用友元函数时候 没有this指针,需要通过参数列表将所有的操作数传进去
左右移 运算符
friend ostream& operator<<(ostream& os,Complex c);
赋值运算符
赋值运算符 =
一般情况下,系统为每一个类都生成一个默认的赋值运算符
浅拷贝的问题出在默认构造函数 解决方法:深拷贝(自己写拷贝构造)
指针悬挂问题出在默认赋值运算符 解决方法:自己重载赋值运算符
注意点:
1不可以用友元重载
2不可以被继承
3不可以声明为虚函数
成员变量有指针/有赋值操作,需要自己重载赋值运算符
数组下标运算符 []
不能使用友元重载
//函数返回值当左值,需要返回一个引用
//应该返回一个引用(元素本身) 而不是一个值
int& myArray::operator[](int i)
{
if(i>m_length)
{
cout << "数组下标越界" << endl;
}
return m_space[i];
}
实现字符串类
myarray.h
#ifndef MYARRAY_H_INCLUDED
#define MYARRAY_H_INCLUDED
#include <iostream>
#include <cstring>
using namespace std;
class MyString
{
public:
MyString();
MyString(const int number);
MyString(const char *ptr);
MyString(const MyString &str);
~MyString();
MyString& operator =(const MyString &str);
MyString& operator =(const char *p);
char& operator [](const int index);
bool operator ==(const MyString& str)const;
bool operator ==(const char *p) const;
bool operator !=(const MyString& str)const;
bool operator !=(const char *p) const;
bool operator >(const MyString& str)const;
bool operator >(const char *p) const;
bool operator <(const MyString& str)const;
bool operator <(const char *p) const;
MyString operator +(const MyString &str)const;
friend ostream& operator <<(ostream &out, const MyString &str);
friend istream& operator >>(istream &in, const MyString &str);
private:
int len;
char *pstr;
};
#endif // MYARRAY_H_INCLUDED
myarray.cpp
#include "myarray.h"
MyString::MyString()
{
len=0;
pstr = new char[1];
strcpy(pstr,"");
}
MyString::MyString(const int number)
{
len = number;
pstr = new char[len+1];
strcpy(pstr,"");
}
MyString::MyString(const char *ptr)
{
len = strlen(ptr);
pstr = new char[len+1];
strcpy(pstr,ptr);
}
MyString::MyString(const MyString &str)
{
len = str.len;
pstr = new char[len+1];
strcpy(pstr,str.pstr);
}
MyString::~MyString()
{
delete[] pstr;
pstr=NULL;
len=0;
}
MyString& MyString::operator =(const MyString &str)
{
len = str.len;
if(pstr!=NULL)
delete[] pstr;
pstr = NULL;
pstr = new char[len+1];
strcpy(pstr,str.pstr);
return *this;
}
MyString& MyString::operator =(const char *p)
{
cout<<"1NULL"<<endl;
if(p==NULL)
{
cout<<strlen(p)<<endl;
len =0;
pstr = new char[1];
strcpy(pstr,"");
return *this;
}
cout<<"3NULL"<<endl;
len = strlen(p);
if(pstr!=NULL)
delete[] pstr;
pstr = NULL;
pstr = new char[len+1];
strcpy(pstr,p);
return *this;
}
char& MyString::operator [](const int index)
{
return *(pstr+index);
}
bool MyString::operator ==(const MyString& str)const
{
if(strcmp(pstr,str.pstr)==0)
return true;
else
return false;
}
bool MyString::operator ==(const char *p) const
{
if(p==NULL)
{
if(len==0)
return true;
else
return false;
}
if(strcmp(pstr,p)==0)
return true;
else
return false;
}
bool MyString::operator !=(const MyString& str)const
{
if(strcmp(pstr,str.pstr)!=0)
return true;
else
return false;
}
bool MyString::operator !=(const char* p)const
{
if(p==NULL)
{
if(len==0)
return true;
else
return false;
}
if(strcmp(pstr,p)!=0)
return true;
else
return false;
}
bool MyString::operator >(const MyString& str)const
{
if(strcmp(pstr,str.pstr)>0)
return true;
else
return false;
}
bool MyString::operator >(const char* p)const
{
if(strcmp(pstr,p)>0)
return true;
else
return false;
}
bool MyString::operator <(const MyString& str)const
{
if(strcmp(pstr,str.pstr)<0)
return true;
else
return false;
}
bool MyString::operator <(const char* p)const
{
if(strcmp(pstr,p)<0)
return true;
else
return false;
}
MyString MyString::operator +(const MyString &str)const
{
MyString temp;
temp.len=strlen(str.pstr)+len;
temp.pstr = new char[len+1];
strcpy(temp.pstr,pstr);
strcat(temp.pstr,str.pstr);
return temp;
}
ostream& operator <<(ostream &out, const MyString &str)
{
out << str.pstr;
return out;
}
istream& operator>>(istream& in, const MyString& str)
{
in >> str.pstr;
return in;
}
main.cpp
int main1()
{
MyString s1;
MyString s2("s2");
MyString s2_2 = NULL;
MyString s3 = s2;
MyString s4 = "s42323233";
s4 = s2;
s4 = "headad";
cout << s4[2] << endl;
cout << s4 << endl;
return 0;
}
int main2()
{
MyString s1;
MyString s2("s2");
MyString s3("s223123");
if (s2 == NULL)
{
cout << "相等" << endl;
}
else
{
cout << "不相等" << endl;
}
if (s2 == s3)
{
cout << "相等" << endl;
}
else
{
cout << "不相等" << endl;
}
return 0;
}
int main3()
{
MyString s1;
MyString s2("s2");
MyString s3("s3");
if (s3 < "bbbb")
{
cout << "s3 < bbbb" << endl;
}
else
{
cout << "s3 > bbbb" << endl;
}
if (s3 > s2 )
{
cout << "s3 > s2" << endl;
}
else
{
cout << "s3 < s2" << endl;
}
return 0;
}
int main()
{
MyString s1(128);
cin >> s1;
cout << s1 << endl;
return 0;
}