运算符重载
#include <iostream>
using namespace std;
//加号运算符重载
class Person
{
public:
//1.成员函数重载+号
/*Person operator+(Person &p)
{
Person temp;
temp.m_a = this->m_a + p.m_a;
temp.m_b = this->m_b + p.m_b;
return temp;
}*/
int m_a;
int m_b;
};
// 2.全局函数重载
Person operator+ (Person& p1, Person& p2)
{
Person temp;
temp.m_a = p1.m_a + p2.m_a;
temp.m_b = p1.m_b + p2.m_b;
return temp;
}
//2.1函数重载版本
Person operator+ (Person& p1, int num)
{
Person temp;
temp.m_a = p1.m_a + num;
temp.m_b = p1.m_b + num;
return temp;
}
void add1()
{
Person p1;
p1.m_a = 10;
p1.m_b = 20;
Person p2;
p2.m_a = 30;
p2.m_b = 40;
//成员含糊重载本质调用
//Person p3 = p1.operator+(p2)
//全局函数重载本质调用
//Person p3 = operator+(p1, p2);
//下面两个通用
Person p3 = p1 + p2;
cout << p3.m_a << endl;
cout << p3.m_b << endl;
//运算重载也可以发送函数重载
Person p4 = p1 + 100; //Person+int
cout << p4.m_a << endl;
cout << p4.m_b << endl;
}
int main()
{
add1();
system("pause");
return 0;
}
//总结1:对于内置的数据类型的表达式的运算是不可能改变的
//总结2:不要滥用运算符重载
#include<iostream>
using namespace std;
class Person
{
friend ostream& operator<<(ostream& out, Person p);
public:
Person(int a, int b)
{
m_a = a;
m_b = b;
}
private:
//利用成员函数重载左移运算符 p.operator<<(cout)简化版本p<<cout
//不会利用成员函数重载<<运算符,因为无法实现cout在左侧
int m_a;
int m_b;
};
//只利用全局函数重载左移运算符
ostream &operator<<(ostream& out, Person p)
{
out <<"m_a="<< p.m_a << ", m_b=" << p.m_b << endl;
return out;
}
void test()
{
Person p(10, 20);
cout << p <<endl;
}
int main()
{
test();
system("pause");
return 0;
}
//总结 重载左移运算符配合友元可以实现输出自定义数据类型
#include<iostream>
using namespace std;
class Myint
{
friend ostream& operator <<(ostream& cout, Myint p);
public:
Myint()
{
num = 0;
}
//重置前置++运算符,返回引用一直对一个数据进行递增操作
Myint& operator++()
{
//先进行++运算
num++;
//再返回自身
return *this;
}
//重置后置++运算符 int代表占位参数,可以区分前置和后置递增
Myint operator++(int)
{
// 先记录当时结果
Myint temp = *this;
// 后 递增
num++;
//最后将记录结果做返回
return temp;
}
private:
int num;
};
ostream& operator <<(ostream& cout, Myint p)
{
cout << "num=" << p.num << endl;
return cout;
}
void test1()
{
//测试
Myint m;
cout << m << endl;
cout << ++(++m) << endl;
}
void test2()
{
Myint m;
cout << m++ << endl;
cout << m << endl;
}
int main()
{
test1();
test2();
system("pause");
return 0;
}
//总结 前置递增返回引用,后置递增返回值
#include <iostream>
using namespace std;
//赋值运算符重载
class Person
{
public:
Person(int age)
{
m_age = new int(age);
}
~Person()
{
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
cout << "析构函数" << endl;
}
//重载 赋值运算符
Person& operator=(Person& p)
{ //编译器是提供浅拷贝
//应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
//深拷贝
m_age = new int(*p.m_age);
//返回对象本身
return *this;
}
int *m_age;
};
void test1()
{
Person p1(10);
Person p2(20);
Person p3(30);
p3 = p2 = p1; //赋值操作
cout <<"p1年龄为" <<*p1.m_age << endl;
cout << "p2年龄为" << *p2.m_age << endl;
cout << "p3年龄为" << *p3.m_age << endl;
}
int main()
{
test1();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
//赋值运算符重载
class Person
{
public:
Person(string name, int age)
{
m_name = name;
m_age = age;
}
//重载运算符==
bool operator==(Person& p)
{
if (this->m_name == p.m_name && this->m_age == p.m_age)
{
return true;
}
else
{
return false;
}
}
//重载运算符!=
bool operator!=(Person& p)
{
if (this->m_name == p.m_name && this->m_age == p.m_age)
{
return false;
}
else
{
return true;
}
}
string m_name;
int m_age;
};
void test1()
{
Person p1("tom", 18);
Person p2("tom", 18);
if (p1 == p2)
{
cout << "==符号:相等" << endl;
}
else
{
cout << "==符号:不相等" << endl;
}
if (p1 != p2)
{
cout << "!=符号:不相等" << endl;
}
else
{
cout << "!=符号:相等" << endl;
}
}
int main()
{
test1();
system("pause");
return 0;
}
评论列表 (0 条评论)