前言
在C++中有很多运算符,有单目运算符、双目运算符、三目运算符等,运算符重载使我们可以根据自己的需求实现加减乘除等各种运算,是不是所有运算符都可以重载呢?重载的运算符函数如何去确定函数原型呢?重载的方法分为成员函数法和友元函数法,那么他们有什么不同呢?下面就通过具体的运算符重载程序来详细分析如何确认重载函数的原型,如何选择成员函数或友元函数。
运算符重载知识点总结
1. 运算符重载为我们提供了对自定义数据进行运算的机制,让我们可以自定义加减等运算; 2. 运算符也是一个函数,运算符重载的本质是函数重载; 3. 运算符重载不改变原优先级; 4. 运算符重载不改变原有的结合顺序(自左向右、自右向左); 5. 运算符重载不能改变操作数的个数; 6. 不能自己创建新的运算符; 7. 操作符. :: .* ?: sizeof不可重载; 8. 运算符重载重载有成员函数和友元函数两种,区别在于成员函数有this指针(可以减少一个函数参数),友元函数没有this指针; 9. =, [], ()和->操作符只能通过成员函数进行重载 ; 10. << >>左移右移只能用友元函数;
实例分析
详细的分析都在程序中,一定要多看程序、多写程序
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class PluralClass
{
public:
PluralClass(int r = 0, int v = 0)
{
this->real = r;
this->virt = v;
}
void print_data(void)
{
cout << real << "+" << virt << "i" << endl;
}
public:
PluralClass operator-(PluralClass& p)
{
PluralClass temp((this->real - p.real), (this->virt - p.virt));
return temp;
}
PluralClass& operator--()
{
this->real--;
this->virt--;
return *this;
}
PluralClass operator--(int)
{
PluralClass temp = *this;
this->real--;
this->virt--;
return temp;
}
private:
int real;
int virt;
friend PluralClass operator+(PluralClass& p1, PluralClass& p2);
friend PluralClass& operator++(PluralClass& p);
friend PluralClass operator++(PluralClass& p, int);
friend ostream& operator<<(ostream& out, PluralClass& p);
};
PluralClass operator+(PluralClass& p1, PluralClass& p2)
{
PluralClass temp((p1.real + p2.real), (p1.virt + p2.virt));
return temp;
}
PluralClass& operator++(PluralClass& p)
{
p.real++;
p.virt++;
return p;
}
PluralClass operator++(PluralClass& p, int)
{
PluralClass temp = p;
p.real++;
p.virt++;
return temp;
}
void FuncTest1(void)
{
PluralClass p1;
p1.print_data();
PluralClass p2(1, 2), p3(1, 2);
p2.print_data();
p3.print_data();
PluralClass p4;
p4 = p2 + p3;
p4.print_data();
p1 = p4 - p2;
p1.print_data();
++p1;
p1.print_data();
--p1;
p1.print_data();
p1++;
p1.print_data();
p1--;
p1.print_data();
}
ostream& operator<<(ostream& out, PluralClass& p)
{
out << p.real << "+" << p.virt << "i";
return out;
}
void FuncTest2()
{
PluralClass p1(1, 2);
cout << p1 << endl;
}
class MyClassStr
{
public:
MyClassStr(const char* str)
{
this->len = strlen(str);
this->str = new char[len + 1];
strcpy(this->str, str);
}
~MyClassStr()
{
if (this->str != NULL)
{
delete[] this->str;
}
this->len = 0;
}
public:
void PrintStr()
{
cout << this->str << endl;
}
public:
MyClassStr& operator=(MyClassStr& s)
{
if (this->str != NULL)
{
delete[] this->str;
this->len = 0;
}
this->len = s.len;
this->str = new char[this->len + 1];
strcpy(this->str, s.str);
return *this;
}
private:
int len;
char* str;
public:
char& operator[](int index)
{
return this->str[index];
}
public:
bool operator==(MyClassStr& s)
{
if (this->len != s.len)
{
return false;
}
if (0 == strcmp(this->str, s.str))
{
return true;
}
else
{
return false;
}
}
bool operator!=(MyClassStr& s)
{
return !(*this == s);
}
public:
void operator()(int a, int b)
{
cout << "hello word!" << endl;
cout << a << " " << b << endl;
}
public:
int operator>(MyClassStr& s) const
{
return strcmp(this->str, s.str);
}
int operator<(MyClassStr& s)
{
return strcmp(s.str, this->str);
}
int operator>(const char* p) const
{
return strcmp(this->str, p);
}
int operator<(const char* p) const
{
return strcmp(p, this->str);
}
public:
MyClassStr(int len = 0)
{
if (len == 0)
{
this->len = 0;
this->str = new char[this->len + 1];
strcpy(this->str, "");
}
else
{
this->len = len;
this->str = new char[this->len + 1];
memset(this->str, 0, this->len);
}
}
char* ret_str()
{
return this->str;
}
int ret_len()
{
return this->len;
}
friend istream& operator>>(istream& in, MyClassStr& s);
};
void FuncTest3()
{
MyClassStr S1("abcdefg");
S1.PrintStr();
MyClassStr S2("12345");
S2.PrintStr();
S2 = S1;
S2.PrintStr();
}
void FuncTest4()
{
MyClassStr S1("qwertyu");
cout << S1[2] << endl;
S1[0] = '1';
S1.PrintStr();
}
void FuncTest5()
{
MyClassStr S1("12345");
MyClassStr S2("abcde");
if (S1 == S2)
{
cout << "S1 = S2" << endl;
}
else
{
cout << "S1 != S2" << endl;
}
S1 = S2;
if (S1 != S2)
{
cout << "S1 != S2" << endl;
}
else
{
cout << "S1 = S2" << endl;
}
}
void FuncTest6()
{
MyClassStr s1("123");
s1(1, 3);
}
void FuncTest7()
{
MyClassStr s1("aaaaa"), s2("12");
if (s1 > s2)
{
cout << "s1 > s2" << endl;
}
else
{
cout << "s1 < s2" << endl;
}
if (s1 > "a")
{
cout << "s1 > \"a\"" << endl;
}
else
{
cout << "s1 < \"a\"" << endl;
}
}
istream& operator>>(istream& in, MyClassStr& s)
{
cin >> s.str;
return in;
}
void FuncTest8()
{
MyClassStr s1(6);
strcpy(s1.ret_str(), "abcde");
s1.PrintStr();
cout << "请输入:";
cin >> s1;
s1.PrintStr();
}
int main(void)
{
FuncTest1();
FuncTest2();
FuncTest3();
FuncTest4();
FuncTest5();
FuncTest6();
FuncTest7();
FuncTest8();
system("pause");
return 0;
}
【四、const与this指针详解】详解C与C++中const的异同,类中的const
|