C++学习笔记-day11
1、运算符重载加号
注意事项:(1)对于内置的数据类型表达式的运算符是不可能改变的。(2)不要滥用运算符重载。
#include<iostream>
using namespace std;
//加号运算符重载
//1、成员函数重载加号
class Person
{
public:
int m_A;
int m_B;
//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;
//}
};
//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;
}
//3、运算符重载也可以进行函数重载
Person operator+(Person p1, int a)
{
Person temp;
temp.m_A = p1.m_A + a;
temp.m_B = p1.m_B + a;
return temp;
}
void test01()
{
Person p1;
Person p2;
p1.m_A = 10;
p2.m_A = 10;
p1.m_B = 10;
p2.m_B = 10;
Person p3 = p1 + p2;
Person p4 = p1 + 30;
cout << p3.m_A << p3.m_B << endl;
cout << p4.m_A << p4.m_B << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
2、运算符重载 左移运算符
重载左移运算符配合友元可以实现输出自定义数据类型
#include<iostream> using namespace std; //左移运算符重载 class Person1 { friend ostream& operator<<(ostream& cout, Person1& p); friend void test02(); private: int m_A; int m_B; //不会利用成员函数重载<<运算符,因为无法实现cout在左侧 只能运用全局函数 }; //只能利用全局函数重载<<运算符 ostream& operator<<(ostream& cout, Person1& p) //本质 operator <<(cout,p) 简化cout<<p { cout << "m_A" << p.m_A << "m_B" << p.m_B; return cout; } void test02() { Person1 p1; p1.m_A = 10; p1.m_B = 10; cout << p1 <<"helloworld"<<endl; } int main() { test02(); system("pause"); return 0; }
3、递增运算符重载
#include<iostream> using namespace std; //重载递增运算符 //自定义整型 class MyInteger { friend ostream& operator<<(ostream& cout, MyInteger myint); public: MyInteger() { m_Num = 10; } //重载前置递增运算符 //重载后置递增运算符 MyInteger& operator++() { //先进行++运算,再将自身进行返回 m_Num++; return *this; } MyInteger operator++(int) //int占位参数 用于区分前置和后置递增 { //先记录当时的结果 MyInteger temp = *this; //后递增 m_Num++; //最后将记录的结果进行返回 return temp; } private: int m_Num; }; //重载左移运算符 ostream& operator<<(ostream& cout, MyInteger myint) { cout << myint.m_Num; return cout; } void test3() { MyInteger myint; cout << ++myint << endl; cout << ++myint << endl; cout << myint++ << endl; cout << myint << endl; } int main() { test3(); system("pause"); return 0; }
4、赋值运算符重载
#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; } } //重载赋值运算符 Person& operator=(Person& p) { //编译器是提供浅拷贝 //m_Age=p.m_Age //应该先判断是否有属性在堆区,如果有应该先释放干净,然后再深拷贝 if (m_Age != NULL) { delete m_Age; m_Age = NULL; } m_Age=new int(*p.m_Age); return *this; } int *m_Age; //返回对象本身 }; void test32() { Person p1(18); Person p2(20); Person p3(30); p2 = p1; p3 = p2 = p1; cout << "p1的年龄是" << *p1.m_Age << endl; cout << "p2的年龄是" << *p2.m_Age << endl; cout << "p3的年龄是" << *p3.m_Age << endl; } int main() { int a = 1, b = 2, c = 3; a = b = c; cout << a << b << c<<endl; test32(); system("pause"); return 0; }
5、关系运算符重载
#include<iostream> using namespace std; //重载关系运算符 class Person { public: Person(string name,int age) { m_Name = name; m_Age = age; } string m_Name; int m_Age; //重载关系运算符 bool operator==(Person& p) { if (m_Name == p.m_Name && m_Age == p.m_Age) { return true; } else { return false; } } bool operator!=(Person& p) { if (m_Name != p.m_Name || m_Age != p.m_Age) { return true; } else { return false; } } }; void test1() { Person p1("To2m", 18); Person p2("To2m", 18); if (p1 == p2) { cout << "p1和p2是相等的"<<endl; } else { cout << "p1和p2是不相等的" << endl; } if (p1 != p2) { cout << "p1和p2是不相等的" << endl; } else { cout << "p1和p2是相等的" << endl; } } int main() { test1(); system("pause"); return 0; }
6、函数调用运算符重载
#include<iostream> using namespace std; //函数调用运算符重载 //打印输出类 class Myprint { public: void operator()(string test) { cout << test << endl; } }; class Myadd { public: int operator()(int num1, int num2) { return num1+num2; } }; void Myprint02(string test) { cout << test << endl; } void test1() { Myprint m; //由于使用起来十分类似函数调用,因此称为仿函数,仿函数非常灵活,没有固定的写法 m("helloworld"); Myprint02("he"); Myadd add; int ret=add(100, 200); cout << ret << endl; //匿名函数对象 cout << Myadd()(1, 4) << endl; } int main() { test1(); system("pause"); return 0; }
作者:lq0710
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!