提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
提示:这里可以添加本文要记录的大概内容:
例如:C++运算符重载
提示:以下是本篇文章正文内容,下面案例可供参考
一、加号运算符
完成自定义类型加号运算符
使用成员函数和全局函数都可以完成
#include<iostream>
using namespace std;
#include<string>
/*函数运算符重载*/
class Operate
{
private:
/* data */
public:
Operate(/* args */);
~Operate();
Operate(int a, int b) {
this->m_numa = a;
this->m_numb = b;
}
//成员函数方式
// Operate operator+(Operate &temp1) {
// Operate temp;
// temp.m_numa = this->m_numa+temp1.m_numa;
// temp.m_numb = this->m_numb+temp1.m_numb;
// return temp;
// }
int m_numa;
int m_numb;
};
Operate::Operate(/* args */)
{
}
Operate::~Operate()
{
}
//全员函数方式
Operate operator+(Operate &temp1,Operate &temp2) {
Operate temp;
temp.m_numa = temp1.m_numa + temp2.m_numa;
temp.m_numb = temp1.m_numb + temp2.m_numb;
return temp;
}
//运算符 函数重载
Operate operator+(Operate &temp1, int b) {
temp1.m_numb = temp1.m_numb + b;
return temp1;
}
int main()
{
Operate p1(10, 10);
cout << "numa = "<< p1.m_numa << endl;
cout << "numb = "<< p1.m_numb << endl;
Operate p2(10, 10);
cout << "numa = "<< p2.m_numa << endl;
cout << "numb = "<< p2.m_numb << endl;
Operate p3 = p1 + p2;
cout << "numa = "<< p3.m_numa << endl;
cout << "numb = "<< p3.m_numb << endl;
}
二、左移运算符
目的:为了可以直接实现cout输出一个对象。
只能使用全局函数实现
class Operate
{
friend ostream & operator<<(ostream &cout,Operate &temp);
private:
/* data */
public:
Operate(/* args */);
~Operate();
Operate(int a, int b) {
this->m_numa = a;
this->m_numb = b;
}
//成员函数方式
// Operate operator+(Operate &temp1) {
// Operate temp;
// temp.m_numa = this->m_numa+temp1.m_numa;
// temp.m_numb = this->m_numb+temp1.m_numb;
// return temp;
// }
int m_numa;
int m_numb;
};
ostream & operator<<(ostream &cout,Operate &temp) {
cout <<"numa = "<<temp.m_numa << " numb = " << temp.m_numb ;
return cout;
}
三.递增或者递减运算符
代码如下(示例):
class Myprint
{
private:
/* data */
int M_a;
public:
Myprint(/* args */);
Myprint(int a){
this->M_a = a;
}
~Myprint();
//前置++返回引用,为了操作是同一个对象
Myprint& operator++() {
this->M_a++;
return *this;
}
//后置++返回值,int 是占位符,函数重载的条件
Myprint operator++(int) {
Myprint temp = *this;
this->M_a++;
return temp;
}
//前置--返回引用,为了操作是同一个对象
Myprint& operator--() {
this->M_a--;
return *this;
}
//后置--返回值,int 是占位符,函数重载的条件
Myprint operator--(int) {
Myprint temp = *this;
this->M_a--;
return temp;
}
friend ostream & operator<<(ostream &cout, Myprint temp);
};
Myprint::Myprint(/* args */)
{
}
Myprint::~Myprint()
{
}
ostream & operator<<(ostream &cout, Myprint temp) {
cout << temp.M_a;
return cout;
}
int main()
{
Myprint a(10);
cout << a << endl;
//++ test
cout << ++a << endl;
cout << a << endl;
cout << a++ << endl;
cout << a << endl;
//-- test
cout << --a << endl;
cout << a << endl;
cout << a-- << endl;
cout << a << endl;
}
四.赋值运算符重载
c++编译器至少给一个类添加4个函数
1.默认构造函数
2.默认析构函数
3.默认拷贝构造函数
4.默认运算符operator=,对属性进行值拷贝
问题:如果在堆区申请内存,使用自带的赋值函数(浅拷贝),会导致同一块内存被重复释放,使用深拷贝方案解决
class NumTest
{
private:
/* data */
public:
NumTest(/* args */int age);
~NumTest();
NumTest& operator=(NumTest&temp) {
if (!M_data) {
delete M_data;
M_data = NULL;
}
//编译器提供的代码是浅拷贝
// M_data = temp.M_data;
//提供深拷贝构造函数
M_data = new int(*temp.M_data);
// 返回自身
return *this;
}
int *M_data;
};
NumTest::NumTest(/* args */int data)
{
this->M_data = new int(data);
}
NumTest::~NumTest()
{
if (!this->M_data) {
delete this->M_data;
this->M_data = NULL;
}
}
int main()
{
NumTest p1(10);
NumTest p2(20);
NumTest p3(30);
p2 = p1;
p3 = p2 = p1;
cout << "p1.data = " << *p1.M_data << endl;
cout << "p2.data = " << *p2.M_data << endl;
cout << "p3.data = " << *p3.M_data << endl;
}
五、关系运算符
class NumTest
{
private:
/* data */
public:
NumTest(/* args */int age);
~NumTest();
NumTest& operator=(NumTest&temp) {
if (!M_data) {
delete M_data;
M_data = NULL;
}
//编译器提供的代码是浅拷贝
// M_data = temp.M_data;
//提供深拷贝构造函数
M_data = new int(*temp.M_data);
// 返回自身
return *this;
}
bool operator==(NumTest&temp) {
if (*this->M_data == *temp.M_data)
{
return true;
}
return false;
}
bool operator!=(NumTest&temp) {
if (*this->M_data != *temp.M_data)
{
return true;
}
return false;
}
int *M_data;
int M_num;
};
NumTest::NumTest(/* args */int data)
{
this->M_data = new int(data);
}
NumTest::~NumTest()
{
if (!this->M_data) {
delete this->M_data;
this->M_data = NULL;
}
}
int main()
{
NumTest m1(10);
NumTest m2(10);
if(m1 == m2) {
cout << "m1 == m2 = " << *m1.M_data << endl;
} else {
cout << "m1 != m2 " << "m1 = " << *m1.M_data << "m2 = " << *m2.M_data << endl;
}
}
六、仿函数
重载的()被称作仿函数
class NumTest
{
private:
/* data */
public:
NumTest(/* args */int age);
~NumTest();
int operator()(int num1, int num2) {
return num1 + num2 ;
}
int *M_data;
int M_num;
};
NumTest::NumTest(/* args */int data)
{
this->M_data = new int(data);
}
NumTest::~NumTest()
{
if (!this->M_data) {
delete this->M_data;
this->M_data = NULL;
}
}
int main()
{
NumTest m1(10);
//仿函数
cout << m1(20, 20) << endl;
}
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。