c++构造函数,析构函数,拷贝函数代码即解析

c++构造函数,析构函数,拷贝构造函数有关代码,建议拷贝到编译器运行,会理解的更深刻

#include <stdlib.h>
#include <iostream>
#include <assert.h>
using namespace std;
class Stack
{
public:
    Stack()
    {
        cout << "Stack()" << endl;
        _a = nullptr;
        _size = _capacity = 0;
    }
    //不能再在成员函数中 int*_a,成员变量已经声明过了!!!
    Stack(int n)
    {
        cout << "Stack(int n)" << endl;
        _a = (int*)malloc(sizeof(int) * n);
        if (_a == nullptr)
        {
            perror("malloc fail");
            exit(-1);
        }
        _capacity = n;
        _size = 0;    
    }
    void init(int n = 4)
    {
        int* _a = (int*)malloc(sizeof(int) * n);
        if (_a == NULL)
        {
            perror("malloc fail");
            exit(-1);
        }
        _capacity = n;
        _size = 0;
    }
    void push(int x)
    {
        assert(_a);
        _a[_size] = x;
        _size++;
    }
    ~Stack()
    {
        cout << "~Stack" << endl;
        if (_a != nullptr)
        {
            free(_a);
            _a = nullptr;
            _size = _capacity = 0;
        }
    }
    void destroy()
    {
        if (_a != nullptr)
        {
            free(_a);
            _a = nullptr;
            _size = _capacity = 0;
        }
    }
private:
    int* _a=nullptr;
    int _size=1;
    int _capacity=1;
};
class Date
{
public:
    Date()
    {
        _year = 1;
        _month = 1;
        _day = 1;
    }
    Date(int year, int month = 1, int day = 1)//必须从右往左给缺省值
    {
        _year = year;
        _month = month;
        _day = day;
    }
    //拷贝构造必须得传引用传参 传值传参的时候会继续调用拷贝构造,无穷递归下去
    Date(const Date& d)//d是d1的别名,权限可以缩小!!! 而且赋值不会赋反
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
    void print()
    {
        cout << _year << "/" << _month << "/" << _day << endl;
    }
    //计算100天以后的年月日
    int GetMonthDay(int year,int month)
    {
        assert(month > 0 && month < 13);
        int monthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//第一个位置空出来不用
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
        {
            monthArray[2] = 29;
        }
        return monthArray[month];
    }
    //这里传引用返回 是可以的 返回的是*this
    //不传引用返回 如Add 是需要调用拷贝构造的
    //Date AddEqual(int x)
    /*Date& GetAfterXDay(int x)
    {
        _day += x;
        while(_day > GetMonthDay(_year,_month))
        {
            _day -= GetMonthDay(_year, _month);
            _month++;
            if (_month > 12)
            {
                _year++;
                _month = 1;
            }
        }
        return *this;
    }*/
    //Date Add(int x)
    Date GetAfterXDay(int x)
    {
        Date tmp(*this);//拷贝构造一个
        tmp._day += x;
        while (tmp._day > GetMonthDay(tmp._year,tmp. _month))
        {
            tmp._day -= GetMonthDay(tmp._year, tmp._month);
            tmp._month++;
            if (tmp._month > 12)
            {
                tmp._year++;
                tmp._month = 1;
            }
        }
        return tmp;
    }

private:
    int _year = 2023;
    int _month = 3;
    int _day = 15;
};
int main()
{
    /*Stack st(3);
    st.push(1);
    st.push(1);
    st.push(1);*/
    //构造函数和析构函数对自定义类型会初始化,内置类型不会(如整形,指针)
    // 打的补丁,在成员变量声明时给缺省值,没有明确给构造函数时,,会使用缺省值
    //Date d(2023);
    /*Date d;
    d.print();*/
    //错误方式 没有初始化 太麻烦了 所以就有了构造函数和析构函数
    /*st.push(1);
    st.push(2);*/

    //拷贝构造是构造函数的一种重载
    /*Date d1(2023,3,15);
    Date d2(d1);
    d2.print();
    Date d3 = d1;*/
    //Date d3(2023, 3, 15);
    //d3.GetAfterXDay(100);//d3改变再改变一次是往上叠加
    //d3.print();
    Date d4;
    Date d3(2023, 3, 15);
    d4 = d3.GetAfterXDay(100);
    d4.print();//2023.6.23
    d3.print();//2023.3.15
}

当实力撑不起野心时,请你努力

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值