C++类的六个默认成员函数——思维导图总结

这里写图片描述

相关的代码:


无参构造函数 & 有参构造函数
#include <iostream>
using namespace std;

class Date
{
public:
    Date()
    {
        cout<<"无参构造函数"<<endl;
    }
    Date(int year,int month,int day)
        :_year(year)
        ,_month(month)
        ,_day(day)
    {
        cout<<"有参构造函数"<<endl;
    }
private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1;                  // 无参调用方式
    Date d2(2018,8,8);        // 有参调用方式
    return 0;
}
半缺省参数构造函数

#include <iostream>
using namespace std;


class Date
{
public:
    Date(int year,int month = 1,int day = 30)
        :_year(year)
        ,_month(month)
        ,_day(day)
    {
        cout<<"半缺省参数构造函数"<<endl;
    }

    void Display()
    {
        cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
    }
private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d2(2018); //如果有初始化,则输出初始化的值,没有的话默认缺省值
    d2.Display();  // 结果:2018-1-30


    Date d3(2018,8); //如果有初始化,则输出初始化的值,没有的话默认缺省值
    d3.Display();  // 结果:2018-8-30

    return 0;
}
全缺省参数构造函数

#include <iostream>
using namespace std;


class Date
{
public:
    Date(int year = 1997,int month = 1,int day = 30)
        :_year(year)
        ,_month(month)
        ,_day(day)
    {
        cout<<"全缺省参数构造函数"<<endl;
    }

    void Display()
    {
        cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
    }
private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d1; // 调用全缺省的方法——和无参调用方法一样,所以二者不能一起出现在一个类中
    d1.Display();  // 结果:1997-1-30

    Date d2(2018,8,8); //如果有初始化,则输出初始化的值,没有的话默认缺省值
    d2.Display();  // 结果:2018-8-8


    Date d3(2018,8); //如果有初始化,则输出初始化的值,没有的话默认缺省值
    d3.Display();  // 结果:2018-8-30

    return 0;
}
拷贝构造函数—是构造函数的重载

#include <iostream>
using namespace std;


class Date
{
public:
    Date(int year,int month = 1,int day = 30)
        :_year(year)
        ,_month(month)
        ,_day(day)
    {
        cout<<"半缺省参数构造函数"<<endl;
    }

    Date( const Date& d )
    {
        cout<<"拷贝构造函数"<<endl;
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }
    void Display()
    {
        cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
    }
private:
    int _year;
    int _month;
    int _day;
};

int main()
{
    Date d2(2018); //如果有初始化,则输出初始化的值,没有的话默认缺省值
    d2.Display();  // 结果:2018-1-30


    Date d3(d2);
    d3.Display();  // 结果:2018-8-30

    return 0;
}

在类外定义构造函数


#include <iostream>
using namespace std;


class Date
{
public:
    Date(int year = 1997,int month = 1,int day = 30);

    Date( Date &d )
    {
        cout<<"拷贝构造函数"<<endl;
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

    // 赋值运算符重载
    Date& operator= (const Date &d) // 有一个隐藏的this指针
    {
        if( this != &d )
        {
            _year = d._year;
            _month = d._month;
            _day = d._day;
        }
        return *this;
    }
    void Display()
    {
        cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
    }
    ~Date()
    {}
private:
    int _year;
    int _month;
    int _day;
};

Date::Date(int year ,int month,int day ) //缺省参数不能同时在函数声明和函数定义中出现,只能任选其一(最好放声明),
        :_year(year)
        ,_month(month)
        ,_day(day)
    {
        cout<<"半缺省参数构造函数"<<endl;
    }
int main()
{
    Date d2; 
    d2.Display(); 

    Date d3;
    d3 = d2;
    d3.Display();  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值