阶段三:C++核心编程
Chapter7:类和对象-C++运算符重载
运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
7.1 加号运算符重载
- 作用:实现两个自定义数据类型相加的运算
总结1:对于内置的数据类型的表达式的的运算符是不可能改变的,比如int double等等 只有对自定义的可以通过重载改变 总结2:不要滥用运算符重载;比如看到的是两个数相加的重载 但是函数实现的内部是两个数相减,可以这样做也不会报错,但是这样不规范,也不便于维护代码
#include "iostream"
using namespace std;
class Person
{
public:
Person() {};
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
Person operator+(const Person& p) {
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
public:
int m_A;
int m_B;
};
Person operator+(const Person& p2, int val)
{
Person temp;
temp.m_A = p2.m_A + val;
temp.m_B = p2.m_B + val;
return temp;
}
void test()
{
Person p1(10, 10);
Person p2(20, 20);
Person p3 = p2 + p1;
cout << "mA:" << p3.m_A << " mB:" << p3.m_B << endl;
Person p4 = p3 + 10;
cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;
}
int main()
{
test();
system("pause");
return 0;
}
7.2 左移运算符重载
#include "iostream"
using namespace std;
class Person
{
friend ostream& operator<<(ostream& out, Person& p);
public:
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
private:
int m_A;
int m_B;
};
ostream& operator<<(ostream& out, Person& p)
{
out << "a:" << p.m_A << " b:" << p.m_B;
return out;
}
void test()
{
Person p1(10, 20);
cout << p1 << "hello world" << endl;
}
int main()
{
test();
system("pause");
return 0;
}
7.3 递增运算符重载
#include "iostream"
using namespace std;
class MyInteger
{
friend ostream& operator<<(ostream& out, MyInteger myint);
public:
MyInteger()
{
m_Num = 0;
}
MyInteger& operator++()
{
m_Num++;
return *this;
}
MyInteger operator++(int)
{
MyInteger temp = *this;
m_Num++;
return temp;
}
private:
int m_Num;
};
ostream& operator<<(ostream& out, MyInteger myint)
{
out << myint.m_Num;
return out;
}
void test01()
{
MyInteger myInt;
cout << ++myInt << endl;
cout << myInt << endl;
}
void test02()
{
MyInteger myInt;
cout << myInt++ << endl;
cout << myInt << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
7.4 赋值运算符重载
#include "iostream"
using namespace std;
class Person
{
public:
Person(int age)
{
m_Age = new int(age);
}
Person& operator=(Person& p)
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
m_Age = new int(*p.m_Age);
return *this;
}
~Person()
{
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
}
int* m_Age;
};
void test01()
{
Person p1(18);
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()
{
test01();
system("pause");
return 0;
}
7.5 关系运算符重载
#include "iostream"
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->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 test01()
{
Person a("孙悟空", 18);
Person b("孙悟空", 18);
if (a == b)
{
cout << "a和b相等" << endl;
}
else
{
cout << "a和b不相等" << endl;
}
if (a != b)
{
cout << "a和b不相等" << endl;
}
else
{
cout << "a和b相等" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
7.6 函数调用运算符重载
#include "iostream"
using namespace std;
class MyPrint
{
public:
void operator()(string text)
{
cout << text << endl;
}
};
void test01()
{
MyPrint myFunc;
myFunc("hello world");
}
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
void test02()
{
MyAdd add;
int ret = add(10, 10);
cout << "ret = " << ret << endl;
cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
|