成员函数,重载+号

实现两个对象属性相加

#include <iostream>
usingnamespace std;

classPerson
{
public:
// 成员函数重载+
// operator 是关键字  
// + 号是你要重载的运算符,想重载-就写-
Personoperator+(Person&p)
{
Person temp;
        temp.m_a =this->m_a + p.m_a;// this->m_a 表示当前调用的对象本身
        temp.m_b =this->m_b + p.m_b;
return temp;
}
int m_a;
int m_b;
};

int main()
{
Person p1;
    p1.m_a =10; p1.m_b =20;
Person p2;
    p2.m_a =11; p2.m_b =21;
// Person p3 = p1.operator+(p2); // 本质调用
Person p3 = p1 + p2;// 简写
    cout<<p3.a<<p3.b<<endl;
return0;
}

全局函数重载+ 号

通过全局函数实现重载 +

#include <iostream>
usingnamespace std;

classPersons
{
public:
int m_a;
int m_b;
};

// 全局函数重载+
Personsoperator+(Persons& p1,Persons& p2)
{
Persons temp;
    temp.m_a = p1.m_a + p2.m_a;
    temp.m_b = p2.m_b + p2.m_b;
return temp;
}

int main()
{
Persons p1;
    p1.m_a =10; p1.m_b =20;
Persons p2;
    p2.m_a =11; p2.m_b =21;
// Persons p3 = operator+(p1, p2); // 本质写法
Persons p3 =operator+(p1, p2);// 简写
    cout << p3.m_a <<" "<< p3.m_b;
return0;
}

重载<<操作符

实现cout<<s1; 输出s1对象的值

#include <iostream>
usingnamespace std;

classstu1
{// friend m_a 和m_b 都是私有成员,需要是朋友才能访问,所以加上友元(friend)
friend ostream&operator<<(ostream& cout, stu1& s);
public:
stu1(int a,int b)
{
        m_a = a;
        m_b = b;
}
private:
int m_a;
int m_b;
};

// 重载<<操作符
// 把cout引用返回去,要是不返回引用,main内不能进行添加输出(<<endl)
// 俗称链式编程
ostream&operator<<(ostream& cout, stu1 &s)
{
    cout << s.m_a << s.m_b;
return cout;
}
int main()
{
stu1 s1(10,10);
    cout << s1<<endl;
return0;
}

重载++ 递增运算符和<< 配合使用

#include <iostream>
usingnamespace std;

classstu2
{
friend ostream&operator<<(ostream& cout, stu2 s2);
public:
stu2()
{
        m_a =0;
}
    stu2&operator++()// 前置++
{
        m_a++;// 先++,在返回,所以是前置
return*this;// 放回自身
}
    stu2 operator++(int)// 占位参数 区分是后置++
{
        stu2 ss =*this;// 创建临时对象记录当前对象
        m_a++;
return ss;// 返回临时对象,不返回引用,因为局部对象不能返回
}

private:
int m_a;
};

ostream&operator<<(ostream& cout, stu2 s2)
{
    cout << s2.m_a;
return cout;
}

int main()
{
    stu2 s2;
    cout <<++s2<<endl;
    cout <<s2 << endl;
    cout << s2++<< endl;
return0;
}

= 赋值运算符重载

因为默认的赋值运算符是浅拷贝,使用析构函数delete的时候会出现报错,重载赋值,使用深拷贝

#include <iostream>
usingnamespace std;

classstu3
{
public:
stu3(int age)
{
        cout <<"构造函数调用"<<endl;
        m_age =newint(age);
}
~stu3()
{
        cout <<"析构函数调用"<<endl;
if(m_age !=NULL)
{
delete m_age;
            m_age =NULL;
}
}

// 重载
    stu3&operator=(stu3 &s)
{
// 先判断属性堆区是否为空,不为空释放干净,然后深拷贝
if(this->m_age !=NULL)
{
            cout <<"不干净"<<endl;
deletethis->m_age;
this->m_age =NULL;
}
// 深拷贝:
this->m_age =newint(*s.m_age);
return*this;

}
int* m_age;
};

int main()
{
stu3 s3(12);
stu3 s33(11);
    s3.operator=(s33);
//s3 = s33;
    cout <<*s3.m_age<<endl;
return0;
}

关系运算符重载 ==

自定义比较方式

#include <iostream>
usingnamespace std;

classstu4
{
public:
stu4(string name,int age)
{
        m_name = name;
        m_age = age;
}
    string m_name;
int m_age;
booloperator==(stu4& s4)
{
if(this->m_name == s4.m_name &&this->m_age == s4.m_age)
{
returntrue;
}
else
returnfalse;
}
};

int main()
{
stu4 s4("zhu", 12);
stu4 s44("zhu1", 14);
if(s4.operator==(s44))
        cout <<"相等"<< endl;
else
        cout <<"不相等"<< endl;
return0;
}

函数调用符重载(也称仿函数)

#include <iostream>
usingnamespace std;

classstu5
{
public:
void operator()(string test)
    {
        cout << test << endl;
}
};

int main()
{
    stu5 s5;
s5("hello world");
return0;
}