類的默認成員函數

類的六個默認成員函數:構造函數、拷貝構造函數、析構函數、賦值操作符重載、取地址的操作符重載、const修飾的取地址操作符重載。

1.【構造函數】

成員變量爲私有的,要對它們進行初始化,必須用一個公有成員函數來進行。同時這個函數應該有且僅在定義對象時自動執行一次,這時

調用的函數稱爲構造函數(constructor) 。

構造函數是特殊的成員函數,其特徵如下:

1. 函數名與類名相同。

2. 無返回值。

3. 對象構造(對象實例化)時系統自動調用對應的構造函數。

4. 構造函數可以重載。

5. 構造函數可以在類中定義,也可以在類外定義。

6. 如果類定義中沒有給出構造函數,則C++編譯器自動產生一個缺省的構造函數,但只要我們定義了一個構造函數,系統就不會自動

生成缺省的構造函數。

7. 無參的構造函數和全缺省值的構造函數都認爲是缺省構造函數,並且缺省的構造函數只能有一個。

class Date
{
  public:
    // 1.無參構造函數
    Date ()
    {}
    // 2.帶參構造函數
    Date (int year, int month , int day )
    {
        _year = year ;
        _month = month ;
        _day = day ;
    }
 private:
        int _year ;
        int _month ;
        int _day ;
};
    void TestDate1 ()
    {
    Date d1 ; // 調用無參構造函數
    Date d2 (2015, 1, 1); // 調用帶參的構造函數
    }

2.【拷貝構造函數】

創建對象時使用同類對象來進行初始化,這時所用的構造函數稱爲拷貝構造函數(Copy Constructor),拷貝構造函數是特殊的構造函

數。

特徵:

1. 拷貝構造函數其實是一個構造函數的重載。

2. 拷貝構造函數的參數必須使用引用傳參,使用傳值方式會引發無窮遞歸調用。

3. 若未顯示定義,系統會默認缺省的拷貝構造函數。缺省的拷貝構造函數會,依次拷貝類成員進行初始化。

class Date
{
public :
    Date()
    {}
// 拷貝構造函數
    Date (const Date& d)
    {
        _year = d ._year;
        _month = d ._month;
        _day = d ._day;
    }
private :
    int _year ;
    int _month ;
    int _day ;
};
void TestDate1 ()
{
    Date d1
    Date d2 (d1); // 調用拷貝構造函數
 }

3.【析構函數】

當一個對象的生命週期結束時,C++編譯系統會自動調用一個成員函數,這個特殊的成員函數即析構函數(destructor)

構造函數是特殊的成員函數,其特徵如下:

1. 析構函數在類名加上字符~。

2. 析構函數無參數無返回值。

3. 一個類有且只有一個析構函數。若未顯示定義,系統會自動生成缺省的析構函數。

4. 對象生命週期結束時,C++編譯系統系統自動調用析構函數。

5. 注意析構函數體內並不是刪除對象,而是做一些清理工作。

class Date
{
public :
// 析構函數
    ~Date()
    {}
private :
    int _year ;
    int _month ;
    int _day ;
};

4.【賦值運算符重載】

1、拷貝構造函數是創建的對象,使用一個已有對象來初始化這個準備創建的對象。

2、賦值運算符的重載是對一個已存在的對象進行拷貝賦值。

class Date
{
public :
    Date()
    {}
    // 拷貝構造函數
    Date (const Date& d)
        : _year(d._year)
        , _month(d._month)
        , _day(d._day)
       {}
// 賦值操作符的重載
Date& operator = (const Date& d)
{
    if (this != &d)
    {
    this->_year = d. _year;
    this->_month = d. _month;
    this->_day = d. _day;
}
    return *this ;
}
private:
    int _year ;
    int _month ;
    int _day ;
};
void Test ()
{
    Date d1 ;
    Date d2 = d1; // 調用拷貝構造函數
    Date d3 ;
    d3 = d1 ; // 調用賦值運算符的重載
 }

操作符重載實現的加減乘除

	Date operator= (const Date &g)
	{
		if (this != &g)
		{
			_year=g._year;
			_month=g._month;
			_day=g._day;
		}
		return *this;
	}
	Date operator+ (const Date g)
	{
		Date d;
		d._year = _year + g._year;
		d._month = _month + g._month;
		d._day = _day + g._day;
		return d;
	}
	Date operator+= (const Date g)
	{
		_year += g._year;
		_month += g._month;
		_day += g._day;
		return *this;
	}
	Date operator-(const Date g)
	{
		Date d;
		d._year=_year-g._year;
		d._month=_month-g._month;
		d._day=_day-g._day;
		return d;
	}

在C++中比較常用的就是以上四個默認成員函數,我們在熟悉了以上四個默認函數的特點和用法之後,我們可以練習實現一個複數類。

class Complex
{
public:
	Complex()
	{}//無參構造函數
	Complex(int real,int empty)
	{
		_real=real;
		_empty=empty;
	}//帶參構造函數
	Complex (const Complex& g)
	{
		_real=g._real;
		_empty=g._empty;
	}//拷貝構造函數
	void Display()
	{
		cout<<"real:"<< _real<<"empty:"<< _empty<<endl;
	}
	~Complex()
	{
		cout<<"~Complex()"<<endl;
	}//析構函數
	Complex operator+ (const Complex g)
	{
		Complex d;
		d._real=_real+g._real;
		d._empty=_empty+g._empty;
		return d;
	}//賦值操作符重載函數
private:
	int _real;
	int _empty;
};
int main()
{
	Complex c(10,5);
	Complex e(c);
	e.Display();
	c.Display();
	Complex g;
	g=c+e;
	g.Display();
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章