C++05

DAY05 总结

01数组类封装

1
2
3
4
5
6
* 1 提供int 类数组
* 2 属性
* 2.1 int *paddreass堆区数组指针
* 2.2 int m_capacity 数组容量
* 3 数组类封装中将对象,当数组名对待,可以用【】重载
* int& operator[](int index)这样可以用来当左值

02加号运算符重载

加号重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*加号运算符重载
* 对于内置数据类型,编译器知道该如何进行运算
* 但是对于自定义数据类型,编译器不知道如何运算
* 利用运算符重载,。可以解决问题
* 分别用成员函数和全局函数来实现
* //成员函数的本质
//Person p3 = p1.operator+(p2)
//全局函数的本质
//Person p3 = operator+(p1,p2)
* 都可以简化为p3 = p1+p2;
* */
#include<bits/stdc++.h>
using namespace std;
class Person{
public:
Person(){};
Person(int a,int b):m_A(a),m_B(b){}
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 a)
{
Person temp;
temp.m_A = p1.m_A+a;
temp.m_B = p1.m_B+a;
return temp;
}
void test01()
{
Person p1(10,19);
Person p2(12,23);
Person p3 = p2+p1;
//成员函数的本质
//Person p3 = p1.operator+(p2)
//全局函数的本质
//Person p3 = operator+(p1,p2)
cout<<"p3的A::"<<p3.m_A<<endl;
cout<<"P3的B::"<<p3.m_B<<endl;
Person p4 = p1+100;
cout<<"p4的A::"<<p4.m_A<<endl;
cout<<"P4的B::"<<p4.m_B<<endl;
}

03左移运算符重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*左移运算符重载
* 运算符重载是一种函数的调用的方式
* 重载运算符的定义就是一种函数,函数名为operator@
* @就是想要重载的运算符
* 全局函数(一元是一个参数,二元两个参数)
* 成员函数(一元没有参数,二元一个参数)
* 重载左移
* 利用成员函数:失败,原因是不能让cout在左侧
* 利用全局函数:ostream &operator<<(ostream &cout,Person&p)
* 如果Person属性是私有的,配合友元使用
* */
#include<bits/stdc++.h>
using namespace std;
class Person{
//将全局函数左移运算符重载变为Person 的友元函数
friend ostream &operator <<(ostream &cout, Person &p);
public:
Person();
Person(int a,int b)
{
this->m_A = a;
this->m_B = b;
}
// 试图运用成员函数实现<<运算符重载
// void operator<<(ostream&cout)
// {
// 不方便。
// }
private:
int m_A;
int m_B;
};
//利用全局函数实现<<运算符重载
ostream &operator <<(ostream &cout, Person &p)
{
cout<<"m_A = "<<p.m_A<<" m_B="<<p.m_B;
return cout;
}
void test01()
{
Person p1(10,10);
// cout<<p1<<endl;//在没有重载之前,这是错误的写法
cout<<p1<<endl;
}

04递增运算符重载

递增运算符重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*递增运算符重载
*对于++和--来说,后置形式先返回,然后对象++或者--,返回的是对象的原值
* 代码调用的时候,要优先使用前缀形式,除非确实需要后缀形式。前缀的效率会高一点
* 前置++ 返回引用 Myint&operator++()
* 后置++ 返回值 Myint operator++(int)
* */
#include<bits/stdc++.h>
using namespace std;
class MyInter{
friend ostream &operator <<(ostream &cout, MyInter &myInt);
public:
MyInter()
{
this->m_number = 0;
}
//重载前置
MyInter& operator++()//效率高,直接返回自己
{
//先++
m_number++;
//后返回
return *this;
}
//重载后置 要把参数放进去 返回值也不一样
MyInter operator++(int)//效率低,需要拷贝值
{
MyInter temp = *this;//当时的状态
this->m_number++;
return temp;
}
private:
int m_number;
};
ostream &operator <<(ostream &cout, MyInter &myInt)
{
cout<<myInt.m_number;
return cout;
}

void test01()
{
MyInter i1;
cout<<++(++i1)<<endl;
cout<<i1<<endl;
}
void test02()
{
MyInter i2;
cout<< i2++ <<endl;
cout<<i2<<endl;
}

05智能指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*智能指针
* 智能用途 用来托管堆区创建的对象的释放
* 如果想让sp对象当作一个指针去对待,需要重载
*
* */
#include<bits/stdc++.h>
using namespace std;
class Person{
public:
Person(int age)
{
cout<<"有参构造函数"<<endl;
m_Age = age;
}
void showAge()
{cout<<"AGE IS "<<this->m_Age<<endl;}
~Person()
{cout<<"析构函数的调用"<<endl;}
private:
int m_Age;
};
//智能指针 用来托管new出来的对象的释放
class SmartPointer{
public:
SmartPointer(Person *person1)
{cout<<"SMARTPOINTER构造"<<endl;
this->person = person1;
}
//重载指针的运算符
Person* operator->()
{
return this->person;
}
//重载*运算符
Person& operator*()
{
return *this->person;
}
~SmartPointer()
{
if(this->person !=NULL)
{
cout<<"SMARTPOINTER"<<endl;
delete this->person;
this->person =NULL;
}
}
private:
Person *person;
};
void test01()
{
// Person *p=new Person(10);
//
// delete(p);

SmartPointer sp = SmartPointer(new Person(18));
sp->showAge();//sp->->showAge 才是原来的,编译器简化为sp->showAge();
(*sp).showAge();
}

06赋值运算符的重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* 赋值运算符的重载
*统会默认给一个类创建至少3个函数 默认构造,析构,拷贝构造(简单值拷贝) operator=(值拷贝)
* 由于系统提供的operator= 会进行简单的值拷贝,属性中有堆区的数据,系统会崩
* 返回值 Person& operator=(const Person&p)
* */
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;
}
//系统会默认给一个类创建至少3个函数 默认构造,析构,拷贝构造(简单值拷贝) operator=(值拷贝)
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",19);
Person p2("JERRY",17);
// p1 =p2;
cout<<"NAME IS ::"<<p1.m_Name<<" AGE IS:"<<p1.m_Age<<endl;
cout<<"NAME IS ::"<<p2.m_Name<<" AGE IS:"<<p2.m_Age<<endl;
Person p3(" ",20);
p3=p2=p1;
cout<<"NAME IS ::"<<p2.m_Name<<" AGE IS:"<<p2.m_Age<<endl;
cout<<"NAME IS ::"<<p3.m_Name<<" AGE IS:"<<p3.m_Age<<endl;
Person p4(p3);
cout<<"NAME IS ::"<<p4.m_Name<<" AGE IS:"<<p4.m_Age<<endl;
}

07关系运算符重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Person{
public:
Person(string name,int age)
{
this->m_Name = name;
this->m_Age = age;
}

bool operator==(const Person&p)
{
return this->m_Name == p.m_Name && this->m_Age == p.m_Age;
}
bool operator!=(const Person&p)
{
return !(this->m_Name == p.m_Name && this->m_Age == p.m_Age);
}
private:
string m_Name;
int m_Age;
};
void test01()
{
Person p1("小红",19);
Person p2("小白",18);
if(p1==p2){cout<<"p1等于p2"<<endl;}
else cout<<"p1不等于p2"<<endl;
if(p1!=p2){cout<<"p1不等于p2"<<endl;}
else cout<<"p1等于p2"<<endl;
}

08函数调用运算符重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
* 实现两个()重载
* 进行打印输出文本
* 进行加法运算
*/
class MyFunc
{
public:
void operator()(string text)
{
cout<<text<<endl;
}
};
class MyAdd
{
public:
int operator()(int a,int b)
{ return a+b;}
};
void test01()
{
MyFunc func;//仿函数
func("Hello world");
}

不要重载&&和||

-------------本文结束,感谢您的阅读-------------