运算符重载(二)(C++)------实例
作者:互联网
程序实例
复数类
复数类型的加法运算:实部加实部,虚部加虚部
1.重载 " + ":
CComplex CComplex:: operator+(const CComplex &src)
{
return CComplex(src.mreal + mreal, src.mimage + mimage);
}
我们发现了一个问题,类似 com4 = com1 + 10; 是可以成功的,因为右操作数作为参数传递进来进行了类型转换,而com4 = 10 + com1;是错误的,因为操作符左边需要调用重载函数,因此我们将其实现为全局函数(友元):
CComplex operator+(const CComplex &lhs, const CComplex &rhs)
// 定义为全局 可实现类似 comp4 = 10 + comp3,左边参数会进行隐式转换
{
return CComplex(lhs.mreal + rhs.mreal, lhs.mimage + rhs.mimage);
}
2.重载输入输出操作符" << “、” >> ":
ostream& operator<<(ostream &out, const CComplex &src)
{
out << "mreal = " << src.mreal << ", mimage = " << src.mimage << endl;
return out;
}
istream& operator>>(istream &in, CComplex &src) // 不能声明为const 因为要进行修改
{
in >> src.mreal >> src.mimage;
return in;
}
3.重载" += "操作符
void CComplex :: operator+=(const CComplex &src)
{
mreal += src.mreal;
mimage += src.mimage;
}
4.重载前置++操作符
CComplex& CComplex :: operator++()
{
mreal++;
mimage++;
return *this;
}
5.重载后置++操作符
// 后置单目运算符重载为类的成员函数时,函数要带有一个整型形参。
CComplex CComplex :: operator++(int)
{
return CComplex(mreal++, mimage++);
}
6.完整Code
class CComplex
{
public:
CComplex(int real = 0, int image = 0):mreal(real), mimage(image) {};
CComplex operator+(const CComplex &src);
void operator+=(const CComplex &src);
CComplex operator++(int);
CComplex& operator++();
private:
int mreal;
int mimage;
friend istream& operator>>(istream &in, CComplex &src);
friend ostream& operator<<(ostream &out, CComplex &src);
friend CComplex operator+(const CComplex &lhs, const CComplex &rhs);
};
CComplex CComplex:: operator+(const CComplex &src)
{
return CComplex(src.mreal + mreal, src.mimage + mimage);
}
ostream& operator<<(ostream &out, const CComplex &src)
{
out << "mreal = " << src.mreal << ", mimage = " << src.mimage << endl;
return out;
}
istream& operator>>(istream &in, CComplex &src) // 不能声明为const 因为要进行修改
{
in >> src.mreal >> src.mimage;
return in;
}
void CComplex :: operator+=(const CComplex &src)
{
mreal += src.mreal;
mimage += src.mimage;
}
CComplex CComplex :: operator++(int)//后置++
{
return CComplex(mreal++, mimage++);
}
CComplex& CComplex :: operator++()//前置+++
{
mreal++;
mimage++;
return *this;
}
CComplex operator+(const CComplex &lhs, const CComplex &rhs)
// 定义为全局 可实现类似 comp3 = 10 + comp2,左边参数会进行隐式转换
{
return CComplex(lhs.mreal + rhs.mreal, lhs.mimage + rhs.mimage);
}
int main()
{
CComplex com(10, 10);
cout << com;
CComplex com1(20, 10);
com1 += com;
cout << com1;
CComplex com2;
com2 = com + com1;
cout << com2;
CComplex com3 = com2++;
cout << com3;
cout << com2;
com3 = ++com2;
cout << com3;
cout << com2;
CComplex com4;
cin >> com4;
cout << com4;
com4 += com1;
cout << com4;
com4 = com1 + 10;
cout << com4;
com4 = 10 + com1;
cout << com1;
cout << com4;
}
String类实现(部分)
class CString
{
public:
CString(char *p = NULL)
{
if(p != NULL)
{
_pstr = new char[strlen(p) + 1];//加1是因为字符串是以'\0'结束,strlen是求的有效长度
strcpy(_pstr, p);
}
else
{
_pstr = new char[1];
*_pstr = '\0';//这样处理是为了不用每次使用都要考虑nullptr的情况
}
}
~CString()
{
if (_pstr != NULL)
{
delete []_pstr;
_pstr = NULL;
}
}
CString(const CString &src)
{
_pstr = new char[strlen(src._pstr) + 1];
strcpy(_pstr, src._pstr);
}
CString& operator=(const CString &src)
{
if(this == &src)
return *this;
delete []_pstr;
_pstr = new char[strlen(src._pstr) + 1];
strcpy(_pstr, src._pstr);
return *this;
}
bool operator>(const CString &src)const
{
return (strcmp(_pstr, src._pstr) > 0);
}
bool operator<(const CString &src)const
{
return (strcmp(_pstr, src._pstr) < 0);
}
bool operator==(const CString &src)const
{
return (strcmp(_pstr, src._pstr) == 0);
}
int length()const
{
return strlen(_pstr);
}
char operator[](int index)const//只进行读操作,写成常方法(const),普通对象和常对象均可以调用
{
return _pstr[index];
}
const char* c_str()const
{
return _pstr;
}
private:
char *_pstr;
friend CString operator+(const CString &lhs, const CString &rhs);
friend ostream& operator<<(ostream &out, const CString &str);
friend istream& operator>>(istream &in, CString &str);
};
CString operator+(const CString &lhs, const CString &rhs)
{
char *newString = new char[lhs.length() + rhs.length() + 1];
strcpy(newString, lhs._pstr);
return CString(strcat(newString, rhs._pstr));
}
ostream& operator<<(ostream &out, const CString &str)
{
out << str._pstr;
return out;
}
istream& operator>>(istream &in, CString &str)
{
char temp[100];
in >> temp;
str._pstr = new char[strlen(temp) + 1];
strcpy(str._pstr, temp);
return in;
}
int main()
{
CString str1 = "aaa";
CString str2 = str1;
CString str3;
str3 = str1;
CString str4 = str1 + str3;
str4 = str1 + "bbb";
str4 = "aaa+ str1;
cout << str4 << endl;
if (str4 > str1)
{
cout << "str4 > str1" << endl;
}
else
{
cout << "str4 < str1" << endl;
}
int len = str4.length();
for (int i = 0; i < len; i++)
{
//str4.operator[](int index)
cout << str4[i]<< " ";
}
cout << endl;
//string char[]
char buf[1024] = { 0 };
strcpy(buf, str4.c_str());
cout << "buf:" << buf << endl;
CString test;
cin >> test;
cout << test << endl;;
CString test1 = "AAAA";
cout << test1 << endl;
cout << (test == test1) << endl;
return 0;
}
标签:src,pstr,CComplex,CString,C++,运算符,operator,mimage,重载 来源: https://blog.csdn.net/weixin_44737923/article/details/104687501