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;
}

猜你喜欢

转载自blog.csdn.net/qq_37941471/article/details/81507253