C++核心(面向对象)
类和对象
运算符重载
运算符重载的概念:对已有的运算符重新进行定义,赋于其另一种功能,以适应不同的数据类型。
1.加号运算符重载
- 作用:实现两个自定义数据类型相加运算
- 分为两种情况:一种是通过成员函数重载,另一种是通过全局函数重载
- 运算符重载也可以发生函数重载
#include <iostream>
using namespace std;
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;
// }
};
//通过全局函数重载加号运算符
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;
}
//运算符重载也可以发生函数重载
Person operator+(Person &p1, int num) //函数名相同,参数个数相同,类型不同
{
Person temp; //中间变量
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}
int main()
{
Person p1, p2, p3, p4;
p1.m_A = 10;
p1.m_B = 10;
p2.m_A = 20;
p2.m_B = 20;
//p3 = p1.operator+(p2);
//p3 = p1 + p2;
//p3 = operator+(p1, p2);
p3 = p1 + p2; //不进行加号运算符重载,不可以得到p3
cout<<"p3的属性值为:"<<p3.m_A<<" "<<p3.m_B<<endl;
p4 = operator+(p1, 100);
//p4 = p1 + 100;
cout<<"p4的属性值为:"<<p4.m_A<<" "<<p4.m_B<<endl;
return 0;
}
总结:对于内置的数据类型的表达式的运算符不可能改变;不要滥用运算符重载。
2.左移运算符重载
- 作用:可以输出自定义数据类型
- 不能通过成员函数重载左移运算符,只能用全局函数重载左移运算符
- 如果对象属性为私有,如何使用全局函数访问,此时通过友元技术
#include <iostream>
using namespace std;
class Person {
friend ostream & operator<<(ostream &cout, Person &p);
public:
int m_A;
int m_B;
private:
int m_C = 100;
};
//通过全局函数重载左移运算符
ostream & operator<<(ostream &cout, Person &p)
{
cout << p.m_A << " " << p.m_B << endl;
//全局函数访问对象的私有属性,利用友元技术
cout<<p.m_A<<" "<<p.m_B<<" "<<p.m_C; //利用友元技术来访问私有属性
return cout;
}
int main()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
cout <<"对象属性值为:"<< p1<< endl;
system("pause");
return 0;
}
注:以上左移运算符重载的代码是在Visual Studio中完成,非DEV C++。
3.递增运算符重载
- 作用:通过重载递增运算符,实现自己的整型数据
- 递增运算符重载分为前置++运算符和后置++运算符,前置++运算符先++后保存,后置++运算符先保存后++,注意两者区别
- 同理还有递减运算符重载
- C++语言并不要求递增和递减运算符必须是类的成员,但是因为它们改变的正好是所操作对象的状态,所以将其设定为成员函数(参考《C++ Primer 第五版》)
#include <iostream>
using namespace std;
class MyInteger {
friend ostream & operator<<(ostream &cout, MyInteger &m);
public:
MyInteger() //无参构造函数,初始化类的私有成员
{
m_Num = 10;
}
//前置++运算符的重载
MyInteger & operator++()
{
//先++
m_Num++;
//再返回
return *this;
}
//后置++运算符的重载
MyInteger operator++(int) //占位符防止函数名相同
{
//先返回
MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但返回以前的值
//后++
m_Num++;
return temp; //实现先返回后++
}
private:
int m_Num;
};
ostream & operator<<(ostream &cout, MyInteger &m)
{
cout << m.m_Num;
return cout;
}
int main()
{
MyInteger myint;
cout << myint << endl; //左移运算符重载,涉及友元技术
cout << ++myint << endl; //11
cout << myint++ << endl; //11
cout << myint << endl; //12
system("pause");
return 0;
}
总结:前置递增返回引用(目的是一直对一个数据进行递增操作),后置递增返回值。
注:以上递增运算符重载的代码是在Visual Studio中完成,非DEV C++。
4.赋值运算符重载
C++编译器至少给一个类添加4个函数:
- 默认构造函数(无参,函数体为空)
- 默认析构函数(无参,函数体为空)
- 默认拷贝构造函数,对属性进行值拷贝
- 赋值运算符operator=,对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深拷贝和浅拷贝带来的问题(堆区内存的重复释放)
#include <iostream>
using namespace std;
class Person {
friend void test01();
public:
Person(int age)
{
m_Age = new int(age);
}
~Person()
{
if(m_Age != NULL)
{
delete m_Age; //堆区内存手动释放
m_Age = NULL;
}
}
//不使用编译器提供的默认赋值运算符,自己定义重载赋值运算符
//默认
//void operator=(Person &p)
//{
// m_Age = p.m_Age;
//}
Person & operator=(Person &p)
{
//应该先判断是否有属性在堆区,如果有先释放干净,再深拷贝
if (m_Age != NULL)
{
delete m_Age;
m_Age = NULL;
}
//深拷贝
m_Age = new int(*p.m_Age);
return *this;
}
private:
int * m_Age;
};
void test01()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1; //连续赋值,需要将赋值运算符改为返回自身
cout << *p1.m_Age << endl; //18
cout << *p2.m_Age << endl; //18
cout << *p3.m_Age << endl; //18
}
int main()
{
test01();
system("pause");
return 0;
}
总结:在赋值运算符重载时,如果出现连续赋值操作,此时返回值为对象本身;赋值运算符必须为类的成员函数。
注:以上递增运算符重载的代码是在Visual Studio中完成,非DEV C++。
5.关系运算符重载
- 作用:重载关系运算符,可以让两个自定义类型对象进行对比操作
- 包括>大于 <小于 相等== 不相等!=
- 比较运算符可以是成员函数也可以是全局函数,一般为全局函数
#include <iostream>
using namespace std;
#include <string>
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;
}
}
string m_Name;
int m_Age;
};
void test01()
{
Person p1("jack",18);
Person p2("mike",20);
if (p1 == p2)
{
cout << "p1与p2是相等的" << endl;
}
else
{
cout << "p1与p2不相等" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
注:以上递增运算符重载的代码是在Visual Studio中完成,非DEV C++。
6.函数调用运算符重载
- 函数调用运算符()也可以重载
- 由于重载后使用的方式非常像函数的调用,所以称为仿函数
- 仿函数没有固定写法,非常灵活
- 函数调用运算符重载必须是成员函数
#include <iostream>
using namespace std;
#include <string>
class MyPrint{
public:
void operator()(string text) //重载函数调用,仿函数
{
cout<<text<<endl;
}
};
void test01()
{
MyPrint myprint;
myprint("hello world!");
}
int main()
{
test01(); //hello world
return 0;
}
补充:匿名对象的函数调用
#include <iostream>
using namespace std;
#include <string>
class MyADD{
public:
int operator()(int v1, int v2) //重载函数调用,仿函数
{
return v1+v2;
}
};
void test01()
{
MyADD myadd;
int ret = myadd(10, 10);
cout<<ret<<endl;
//匿名函数对象
cout<<MyADD()(100, 100)<<endl; //MyADD()匿名函数对象的创建,执行完毕后系统立即回收匿名对象
}
int main()
{
test01();
return 0;
}
注:匿名函数对象的创建,执行完毕后系统立即回收匿名对象 。