加号运算符重载
//本质就是函数,只是命名更规则更规范更具有可读性
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
int a;
int b;
Person()
{
}
// 有了有参构造函数,就没有无参构造函数了
Person(int a, int b)
{
this->a = a;
this->b = b;
}
//成员函数运算符重载+
//Person operator+(Person& p)
//{
// Person temp;
// temp.a = this->a + p.a;
// temp.b = this->b + p.b;
// return temp;
//}
};
//全局函数运算符重载
Person operator+(Person& p1,Person& p2)
{
Person temp;
temp.a = p1.a + p2.a;
temp.b = p1.b + p2.b;
return temp;
}
//全局函数运算符重载的函数重载
Person operator+(Person& p1, int num)
{
Person temp;
temp.a = p1.a + num;
temp.b = p1.b + num;
return temp;
}
void test01()
{
Person p1(10,20);
Person p2(30, 40);
Person p3;
//成员函数重载的本质调用
//p3 = p2.operator+(p1);
//简化调用
//p3 = p1 + p2;
//全局函数运算符重载的本质调用
//p3 = operator+(p1, p2);
//简化调用
//p3 = p1 + p2;
// 简化运用时,要确保只能有一个加号运算符重载函数
//cout << p3.a << endl;
//cout << p3.b << endl;
// 运算符重载也可以发生函数重载
p3 = p1 + 10;
cout << p3.a << endl;
cout << p3.b << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
左移运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
// 左移运算符重载
// 1.利用成员函数重载左移运算符
// 2.利用全局函数重载左移运算符
class Person
{
friend ostream& operator<<(ostream& out, Person p);
public:
Person()
{}
Person(int a, int b)
{
this->a = a;
this->b = b;
}
private:
int a;
int b;
// 因为cout时全局变量,只有一个,所以要用引用的调用方式
// 使用成员函数重载左移运算符
// 调用方式为 p.operator<<(cout)
// 简写为 p << cout
ostream& operator<<(ostream& cout)
{
cout << "a = " << this->a << endl
<< "b = " << this->b << endl;
return cout;
}
};
// 利用全局变量重载左移运算符
ostream& operator<<(ostream& out,Person p)
{
out << "a = " << p.a << endl
<< "b = " << p.b << endl;
return out;
}
//void test01()
//{
// Person p;
// p.a = 10;
// p.b = 20;
// cout << p << endl;
//}
//void test02()
//{
// Person p;
// p.a = 10;
// p.b = 20;
// p << cout << endl;
//}
void test03()
{
Person p(10, 20);
cout << p << endl;
}
int main()
{
//test01();
//test02();
test03();
system("pause");
return 0;
}
递增运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
// 递增运算符重载
class MyInt
{
friend ostream& operator<<(ostream& cout, MyInt myint);
friend MyInt& operator++(MyInt& myint);
// 额外参数应该在主要参数的后面
// friend MyInt operator++(int, MyInt& myint);
friend MyInt operator++(MyInt& myint, int);
public:
MyInt()
{
}
MyInt(int num)
{
this->num = num;
}
//MyInt& operator++()
//{
// num++;
// return *this;
//}
// 判断函数重载的条件不包含返回值
// int代表占位参数,可用来区分前置++还是后置++
// 后缀"operator++" 的额外参数必须时int类型
//MyInt operator++(int)
//{
// MyInt temp = *this;
// num++;
// return temp;
//}
private:
int num;
};
ostream& operator<<(ostream& cout, MyInt myint)
{
cout << myint.num;
return cout;
}
MyInt& operator++(MyInt& myint)
{
(myint.num)++;
return myint;
}
MyInt operator++(MyInt& myint,int)
{
MyInt temp = myint;
(myint.num)++;
return temp;
}
void test01()
{
MyInt myint(10);
cout << myint << endl;
// myint.operator++();
cout << ++myint << endl;
cout << myint << endl;
// myint.operator++(1);
cout << myint++ << endl;
cout << myint << endl;
}
void test02()
{
MyInt myint(10);
cout << myint << endl;
// operator++(myint);
cout << ++myint << endl;
cout << myint << endl;
// operator++(myint,1);
cout << myint++ << endl;
cout << myint << endl;
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
赋值运算符重载
涉及到深拷贝与浅拷贝的问题。--》 堆区内存重复释放与解决方法
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
// 赋值运算符重载
class Person
{
public:
Person(int age)
{
this->age = new int(age);
}
Person& operator=(Person p)
{
// 先确保本类中是否已经在堆区开辟空间
if (this->age != NULL)
{
delete this->age;
this->age = NULL;
}
// 编译器提供的代码时浅拷贝
// this->age = p.age;
this->age = new int(*p.age);
return *this;
}
~Person()
{
if (age != NULL)
{
delete age;
age = NULL;
}
}
int* age;
};
void test01()
{
Person p1(10);
Person p2(20);
Person p3(30);
//p3 = p2 = p1;
// 若形参不是Person& 而是Person
// 则调用函数时时简单的值拷贝,调用结束后,所拷贝的对象要释放
// 同时也要释放堆区空间,会涉及到浅拷贝的问题,
// 所拷贝的对象会把原对象的堆空间释放。
//p3 = p1; //
p3 = p2 = p1;
cout << *p1.age << endl;
cout << *p2.age << endl;//20
cout << *p3.age << endl;//10
// 拷贝构造函数,只是简单的值拷贝,浅拷贝,
// 会出现堆区空间重复释放的问题
// p2 = p1;
}
int main()
{
test01();
system("pause");
return 0;
}
关系运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
// 关系运算符重载
class Person
{
public:
Person(string name,int age)
{
this->name = name;
this->age = age;
}
string name;
int age;
bool operator==(Person& p)
{
if (this->name == p.name && this->age == p.age)
{
return true;
}
else
{
return false;
}
}
};
void test01()
{
Person p1("tom", 18);
Person p2("tom", 18);
if (p1 == p2)
{
cout << "==" << endl;
}
else
{
cout << "!=" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
函数调用运算符重载(仿函数)
匿名函数对象
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
// 函数调用运算符重载(仿函数)
// 仿函数非常的灵活,没有固定的写法(类似于函数)
class MyPrint
{
public:
void operator()(string test)
{
cout << test << endl;
}
};
class Add
{
public:
int operator()(int num1,int num2)
{
return num1 + num2;
}
};
void test01()
{
MyPrint p;
p("caixukun");
}
void test02()
{
Add p;
int c = p(10, 20);
cout << c << endl;
// 匿名函数对象,调用结束后立马被释放
cout << Add()(10, 30) << endl;
}
int main()
{
//test01();
test02();
return 0;
}