C++Day5——运算符重载
- 运算符重载:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
加号运算符重载
#include <iostream>
using namespace std;
class Person
{
public:
Person(){}
Person(int a, int b) :m_A(a), m_B(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;
}*/
int m_A;
int m_B;
};
//利用全局函数实现加号运算符重载
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;
}
void test01()
{
Person p1(10, 10);
Person p2(20, 20);
Person p3 = p1 + p2;
//全局函数本质:Person p3=operator+(p1,p2);
//成员函数本质:Person 拍=p1.operator+(p2);
cout << "p3.m_A=" << p3.m_A << endl;
cout << "p3.m_B=" << p3.m_B << endl;
}
int main()
{
test01();
return EXIT_SUCCESS;
}
左移运算符重载
#include <iostream>
using namespace std;
class Person
{
public:
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
//试图利用成员函数做<<重载
/*void operator<<(Person& p)//p.operator<<(cout) p<<cout无法将cout放在左边
{
}*/
int m_A;
int m_B;
};
//利用全局函数实现左移运算符重载
ostream& operator<<(ostream &cout,Person & p1)
{
cout << "m_A=" << p1.m_A << "m_B=" << p1.m_B;
return cout;
}
void test01()
{
Person p1(10, 10);
cout << p1 << endl;
}
int main()
{
test01();
return EXIT_SUCCESS;
}
前置后置递增运算符重载
#include <iostream>
using namespace std;
class MyInter
{
friend ostream& operator<<(ostream& cout, MyInter& myInt);
public:
MyInter()
{
m_Num = 0;
}
//前置++重载
MyInter& operator++()
{
this->m_Num++;
return *this;
}
private:
int m_Num;
};
ostream& operator<<(ostream& cout, MyInter& myInt)
{
cout << myInt.m_Num;
return cout;
}
void test01()
{
MyInter myInt;
cout << ++myInt << endl;
}
int main()
{
test01();
return EXIT_SUCCESS;
}
指针运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
cout << "Person的有参构造调用" << endl;
this->m_Age = age;
}
void showAge()
{
cout << "年龄为: "<< this->m_Age << endl;
}
~Person()
{
cout << "Person的析构调用" << endl;
}
int m_Age;
};
class SmartPoint
{
public:
SmartPoint(Person * person)
{
this->m_person = person;
}
//重载->运算符
Person * operator->()
{
return this->m_person;
}
//重载 * 运算符
Person& operator*()
{
return *m_person;
}
~SmartPoint()
{
if (this->m_person)
{
delete this->m_person;
this->m_person = NULL;
}
}
private:
Person * m_person;
};
void test01()
{
//Person * p = new Person(18);
//(*p).showAge();
//p->showAge();
//delete p;
//利用智能指针 管理 new出来的person的释放操作
SmartPoint sp(new Person(18));
sp->showAge(); // 本质sp->->showAge(); 编译器简化为 sp->showAge();
(*sp).showAge();
}
int main(){
test01();
system("pause");
return EXIT_SUCCESS;
}
赋值运算符重载
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//编译器 默认给一个类4个函数 默认构造 析构 拷贝构造 (值拷贝) operator= (值拷贝)
class Person
{
public:
Person(char * name, int age)
{
this->m_Name = new char[strlen(name) + 1];
strcpy(this->m_Name, name);
this->m_Age = age;
}
//重载 =
Person& operator=( const Person &p)
{
//先判断原来堆区释放有内容,如果有先释放
if (this->m_Name != NULL)
{
delete [] this->m_Name;
this->m_Name = NULL;
}
this->m_Name = new char[strlen(p.m_Name) + 1];
strcpy(this->m_Name, p.m_Name);
this->m_Age = p.m_Age;
return *this;
}
//拷贝构造
Person(const Person & p)
{
this->m_Name = new char[strlen(p.m_Name) + 1];
strcpy(this->m_Name, p.m_Name);
this->m_Age = p.m_Age;
}
~Person()
{
if (this->m_Name!=NULL)
{
delete [] this->m_Name;
this->m_Name = NULL;
}
}
char * m_Name;
int m_Age;
};
void test01()
{
Person p1("Tom",10);
Person p2("Jerry",19);
p2 = p1;
Person p3("", 0);
p3 = p2 = p1;
Person p4 = p3;
cout << "p1姓名: "<< p1.m_Name << " p1年龄: " << p1.m_Age << endl;
cout << "p2姓名: "<< p2.m_Name << " p2年龄: " << p2.m_Age << endl;
cout << "p3姓名: " << p3.m_Name << " p3年龄: " << p3.m_Age << endl;
}
int main(){
test01();
/*int a = 10;
int b = 20;
int c;
c = a = b;
cout << "a = " << a << " b = " << b << " c = " << c << endl;*/
system("pause");
return EXIT_SUCCESS;
}