目录
- class Stack
- {
- public:
- Stack(int capacity=4 )
- {
- _a = (int*)malloc(sizeof(int)*capacity);
- if (_a == nullptr)
- {
- perror("malloc fail");
- exit(-1);
- }
- _top = 0;
- _capacity = capacity;
- cout << "Stack构造函数()" << endl;
- }
- Stack(const Stack&st)
- {
- _a=(int*)malloc(sizeof(int)*st._capacity);
- if (_a == nullptr)
- {
- perror("malloc fail");
- exit(-1);
- }
- memcpy(_a, st._a, sizeof(int)*st._top);
- _top = st._top;
- _capacity = st._capacity;
- }
- void Push(int x)
- {
- _a[_top++] = x;
- }
- ~Stack()
- {
- free(_a);
- _a = nullptr;
- _top = _capacity = 0;
- cout << "Stack析构函数()" << endl;
- }
- private:
- int*_a;
- int _capacity;
- int _top;
- };
- class MyQueue
- {
- public:
- void push(int x)
- {
-
- }
- private:
- Stack _pushSt;
- Stack _popSt;
- size_t size ;
- };
- int main()
- {
- MyQueue m1;
- MyQueue q(m1);
- return 0;
- }
MyQueue这个类需要自定义拷贝构造函数吗?
答:不需要,对于内置类完成值拷贝,对于自定义类型调用其默认拷贝构造。
Stack有默认的拷贝构造。

完成了深拷贝。

运算符重载使类函数调用可读更强。
我们先写一个日期类比较大小。

我们要想实现符号重载,需要自主实现一个>函数。
我们可以先实现一个简单的判断相等函数:
- bool operator==(const Date&d1, const Date&d2)
- {
- return d1._year == d2.__year
- &&d1._month == d2._month
- &&d1._day == d2._day;
- }
但是我们的函数写在类外面,无法访问类里面的成员变量,我们的方法是把函数定义在类里面:

但是注意:==函数有两个操作数,所以只有两个参数:
类成员函数都有一个默认参数:隐含的 this指针,所以:
- bool operator==(const Date&d1)
- {
- return _year == d1._year
- &&_month == d1._month
- &&_day == d1._day;
- }

<<优先级要大于==,所以我们要加上()。

假如我们必须在类外面进行运算符重载呢?
我们无法提取到成员变量,我们可以设计一个成员函数,提取到对应对象的成员变量:
- int GetYear()
- {
- return _year;
- }
- int GetMonth()
- {
- return _month;
- }
- int GetDay()
- {
- return _day;
- }
- bool operator>(const Date&d1)
- {
- if (_year > d1._year)
- {
- return true;
- }
- else if (_year ==d1._year&&_month >d1._month)
- {
- return true;
- }
- else if (_year == d1._year&&_month == d1._month&&_day >d1._day)
- {
- return true;
- }
- else
- return false;
- }
- int main()
- {
- Date d1(2022, 9, 22);
- Date d2(2022, 9, 21);
- /*d1.operator==(d2);*/
- cout << (d1>d2) << endl;
- }

有了大于,我们就可以写出大于等于了。
- bool operator>=(const Date&d1)
- {
- return *this > d1||*this == d1;
- }
- int main()
- {
- Date d1(2022, 9, 22);
- Date d2(2022, 9, 21);
- /*d1.operator==(d2);*/
- cout << (d1>=d2) << endl;
- }

- int GetMonthDay(int year, int month)
- {
- static int monthDayArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
- {
- return 29;
- }
- else
- {
- return monthDayArray[month];
- }
- }
闰年:年数与4的余数等于0且年数与100的余数不等于0和年数与400的余数等于0.
这里为什么要创建静态数组?
答:静态数组是在代码段(静态区),如果我们创建普通数组,每一个对象都会多存储13个整型的字节,消耗空间。
- Date& operator+=(int day)
- {
- _day += day;
- while (_day > GetMonthDay(_year, _month))
- {
- _day -= GetMonthDay(_year, _month);
- _month++;
- if (_month == 13)
- {
- ++_year;
- _month = 1;
- }
- }
- return *this;
- }
分析:

case1:当日期的天数加上对应天数小于对应月(9)的天数(30),我们直接加日期就可以。
case2:当日期的天数加上对应天数大于对应月的天数,我们让日期加天数-该月的天数,月++。
case3:同case2,当月++大于13时,把月置为1,年++。
最后返回*this,日期类指针的解引用。
- Date&operator+=(int day)
- {
- _day += day;
- while (_day > GetMonthDay(_year, _month))
- {
- _day -= GetMonthDay(_year, _month);
- _month++;
- if (_month == 13)
- {
- _year += 1;
- _month == 1;
- }
- }
- return*this;
- }

我们返回的也是一个日期类,并且这个日期类是this指针指向日期加天数之后的结果,调用完毕之后这个日期依旧存在,所以要用传引用返回。

加等表示d1的值也发生了改变。
- Date operator+(int day)
- {
- Date ret = *this;
- ret += day;
- return ret;
- }
我们创建一个临时变量,让临时变量代替*this+=天数,然后返回临时变量,不能传引用返回,因为临时变量在函数调用时自动销毁。
- int main()
- {
- Date d1(2022, 9, 22);
- Date d2;
- /*d1.operator==(d2);*/
- d2 = d1 + 30;
- return 0;
- }

d1+30之后,d1本身没有发生改变。