c++对象无法直接使用运算符,要用函数operator来创建函数后才能在主函数应用
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
/*
运算符重载--函数重载
友元
*/
/*
值返回
引用返回
返回类型 operator 运算符(参数)
*/
#if 0
class A
{
public:
A(int i = 0, int j = 0) :m_i(i), m_j(j) {}
A operator+(const A& s) //a.+(b)
{
//this ->&a a+b
//this ->&b b+a
return A(m_i + s.m_i,m_j + s.m_j);
}
void print()
{
cout << "m_i = "<<m_i << " m_j = " << m_j << endl;
}
friend A operator-(const A& a, const A& b);
A operator++(int)// a++
{
/* int i = m_i;
int j = m_j;
m_i = m_i + 1;
m_j = m_j + 1;
return A(i,j);*/
cout << "后++" << endl;
return A(m_i++, m_j++);
}
A& operator++() //++b
{
cout << "前++" << endl;
++this->m_i;
++m_j;
return *this;
}
private:
int m_i;
int m_j;
};
A operator-(const A& a, const A& b)
{
return A(a.m_i - b.m_i, a.m_j - b.m_j);
}
void main()
{
A a(10, 20);
A b(20, 30);
A c;
(a + b).print(); //a.+(b) +(a,b)
(b + a).print(); //b.+(a)
(a - b).print(); //a.-(b) -(a,b)
(a++).print(); //a.++() ++(a) 10,20
a.print(); //11,21
(++b).print(); //b.++() 21,31
b.print();// 21,31
++c = b;
c.print();
}
#endif
/*
cout -- ostream
cin ---istream
*/
#if 0
class A
{
public:
A(int i = 0) :m_i(i) {}
void print()
{
cout << m_i << endl;
}
A& operator=(const A& s)
{
cout << "=" << endl;
if (this == &s)
return *this;
m_i = s.m_i;
return *this;
}
friend ostream& operator<<(ostream& os, const A& a);
private:
int m_i;
};
ostream& operator<<(ostream& os, const A& a)
{
os << "<<" << endl;
os << a.m_i;
return os;
}
void main()
{
A a(10);
A b(4);
b = a; //b.=(a)
b.print();
a = a;
cout << a <<endl; //cout.<<(a) <<(cout,a)
}
#endif
//ostream & operator<<(ostream &os,const A &a)
/*
(a = b) = c
*/
#if 0
#include<string>
void main()
{
string a = "123";
string b("456");
string c = a + b; //strcat 123456
cout << c << endl;
c[2] = 'k'; //c.[](2)
cout << c << endl;
c = a;
cout << c << endl;
}
#endif
/*
如果一个类有指针作为数据成员,则程序员就必须要写析构,深拷贝,深赋值
*/
class STR
{
public:
STR();
STR(const char* str);
STR(int n, char c);
void print()
{
cout << "m_str = "<<m_str << endl;
}
~STR()
{
delete[]m_str;
m_str = nullptr;
}
STR& operator=(const STR& t) //深赋值
{
if (this == &t)
return *this;
delete[]m_str;
m_str = new char[strlen(t.m_str) + 1];
strcpy(m_str, t.m_str);
return *this;
}
STR(const STR& s)
{
m_str = new char[strlen(s.m_str) + 1];
strcpy(m_str, s.m_str);
}
STR operator+(const STR& s);
friend ostream& operator<<(ostream& os, const STR& s);
private:
char* m_str;
};
STR::STR()
{
m_str = new char[1];
*m_str = '\0';
}
STR::STR(const char* str)
{
m_str = new char[strlen(str) + 1];
strcpy(m_str, str);
}
STR::STR(int n, char c)
{
m_str = new char[n + 1];
memset(m_str, c, n);
m_str[n] = '\0';
}
ostream& operator<<(ostream& os, const STR& s)
{
os << s.m_str;
return os;
}
STR STR::operator+(const STR& s)
{
char* temp = new char[strlen(m_str) + strlen(s.m_str) + 1];
strcpy(temp, m_str);
strcat(temp, s.m_str);
STR t(temp);
delete []temp;
temp = nullptr;
return t;
}
void main()
{
STR s;
STR s1("123456");
STR s2(4, 'k');
//s1.print();
//s2.print();
cout << "s1 = "<<s1 << endl;
cout << "s2 = "<<s2 << endl;
s = s1;
cout << "s = "<<s << endl;
// s1 = s2;
// cout << "s1 = " << s1 << endl;
s = s1 + s2; //123456kkkk
cout << "s = " << s << endl;
}