国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【C++精華鋪】5.C++類和對象(中)類的六個(gè)默認(rèn)成員函數(shù)

這篇具有很好參考價(jià)值的文章主要介紹了【C++精華鋪】5.C++類和對象(中)類的六個(gè)默認(rèn)成員函數(shù)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

目錄

1. 六個(gè)默認(rèn)成員函數(shù)

2. 構(gòu)造函數(shù)

2.1 概念

2.2 默認(rèn)構(gòu)造

2.2.1 系統(tǒng)生成的默認(rèn)構(gòu)造

2.2.2 自定義默認(rèn)構(gòu)造函數(shù)

?2.3 構(gòu)造函數(shù)的重載

3. 析構(gòu)函數(shù)

3.1 概念

?3.2 系統(tǒng)生成的析構(gòu)函數(shù)

?3.3 自定義析構(gòu)函數(shù)

4. 拷貝構(gòu)造

4.1 概念

?4.2 默認(rèn)生成的拷貝構(gòu)造(淺拷貝)

?4.3 自定義拷貝構(gòu)造(深拷貝)

?5. 賦值運(yùn)算符重載

5.1 運(yùn)算符重載

5.2 賦值運(yùn)算符重載

6. 取地址及const取地址操作符重載

7. 附:完整日期類(文章中的代碼不取自這里的代碼,都是為了講解知識點(diǎn)臨時(shí)敲的,這里的代碼是完整的日期類(取自比特科技),可以借鑒學(xué)習(xí))


1. 六個(gè)默認(rèn)成員函數(shù)

? ? ? ? 當(dāng)我們想到空類的時(shí)候肯定想到的是里面什么都沒有的類稱之為空類,但是事實(shí)卻并非如此。當(dāng)一個(gè)類里面什么都不寫的時(shí)候編譯器會(huì)默認(rèn)生成六個(gè)默認(rèn)成員函數(shù)來完成一個(gè)類的基本功能。

  1. 構(gòu)造函數(shù):對象初始化工作
  2. 析構(gòu)函數(shù):空間清理工作
  3. 拷貝構(gòu)造和賦值運(yùn)算符重載:對象的拷貝復(fù)制工作
  4. 取地址和const取地址重載:一般很少自己實(shí)現(xiàn),除非需要給用戶返回指定的特殊的地址。

2. 構(gòu)造函數(shù)

2.1 概念

? ? ? ? 構(gòu)造函數(shù)是一個(gè)特殊的成員函數(shù),他會(huì)在對象創(chuàng)建時(shí)由編譯器自動(dòng)調(diào)用完成對象的初始化工作,構(gòu)造函數(shù)沒有返回值,并且函數(shù)名與類名相同。

? ? ? ? 構(gòu)造函數(shù)特征如下:

  1. 函數(shù)名與類名相同。
  2. 無返回值。
  3. 對象實(shí)例化時(shí)編譯器自動(dòng)調(diào)用對應(yīng)的構(gòu)造函數(shù)。
  4. 構(gòu)造函數(shù)可以重載?
class Date
{
public:
	Date()
	{
		//構(gòu)造函數(shù)
	}
private:

};

2.2 默認(rèn)構(gòu)造

? ? ? ? 什么是默認(rèn)構(gòu)造?無參的構(gòu)造函數(shù)和全缺省的構(gòu)造函數(shù)都稱為默認(rèn)構(gòu)造函數(shù),并且默認(rèn)構(gòu)造函數(shù)只能有一個(gè)。一般來說我們所實(shí)現(xiàn)的自定義類型都必須具有一個(gè)默認(rèn)構(gòu)造函數(shù),原因會(huì)在下面進(jìn)行敘述。注意:無參構(gòu)造函數(shù)、全缺省構(gòu)造函數(shù)、我們沒寫編譯器默認(rèn)生成的構(gòu)造函數(shù),都可以認(rèn)為 是默認(rèn)構(gòu)造函數(shù)。

2.2.1 系統(tǒng)生成的默認(rèn)構(gòu)造

? ? ? ? ?當(dāng)我們沒有去手動(dòng)定義構(gòu)造函數(shù)的時(shí)候系統(tǒng)會(huì)默認(rèn)生成一個(gè)無參的構(gòu)造函數(shù),這個(gè)無參的構(gòu)造函數(shù)會(huì)對類里面的成員變量進(jìn)行初始化,其中包括對自定義類型調(diào)用它的默認(rèn)構(gòu)造函數(shù)(這也是是為什么每個(gè)類必須要有一個(gè)默認(rèn)構(gòu)造的原因),對內(nèi)置類型不做處理,對,你沒聽錯(cuò),系統(tǒng)生成的默認(rèn)構(gòu)造不會(huì)對內(nèi)置類型進(jìn)行處理,這是一個(gè)非常bug的點(diǎn)。比如下面的案例:

class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;
		_hour = 0;
		_minute = 0;

		_second = 0;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
public:
	// 基本類型(內(nèi)置類型)
	int _year;
	int _month;
	int _day;
private:
	// 自定義類型
	Time _t;
};
int main()
{
	Date d;
	cout << d._year << d._month << d._day << endl;
	return 0;
}

輸出:

Time()
-858993460-858993460-858993460?

????????由此可見,默認(rèn)生成的構(gòu)造函數(shù)不會(huì)對內(nèi)置類型進(jìn)行處理,對自定義類型會(huì)調(diào)用它的默認(rèn)構(gòu)造。 由于這一點(diǎn),讓系統(tǒng)生成的默認(rèn)構(gòu)造毫無用武之地,所以在C++11中給這個(gè)漏洞打了一個(gè)補(bǔ)?。?strong>C++11 中針對內(nèi)置類型成員不初始化的缺陷,打了一個(gè)補(bǔ)丁,即:內(nèi)置類型成員變量在 類中聲明時(shí)可以給默認(rèn)值。(以給缺省值的方式給內(nèi)置類型一個(gè)默認(rèn)值)如下:

class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;
		_hour = 0;
		_minute = 0;

		_second = 0;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
public:
	// 基本類型(內(nèi)置類型)
	int _year = 2023;
	int _month = 8;
	int _day = 9;
private:
	// 自定義類型
	Time _t;
};
int main()
{
	Date d;
	cout << d._year << ' ' << d._month << ' '  << d._day << endl;
	return 0;
}

輸出:

Time()
2023 8 9

2.2.2 自定義默認(rèn)構(gòu)造函數(shù)

? ? ? ? 自定義的默認(rèn)構(gòu)造函數(shù)只有倆種,無參的和全缺省的構(gòu)造函數(shù),并且這倆種構(gòu)造只能存在一個(gè)。但是有人可能就要問了,這倆種構(gòu)造函數(shù)不是構(gòu)成重載了嗎,為什么不能同時(shí)存在呢?首先就是語法上規(guī)定了默認(rèn)構(gòu)造只能存在一個(gè),另外就是這倆個(gè)函數(shù)雖然符合函數(shù)重載的語法,但是在調(diào)用的時(shí)候會(huì)出現(xiàn)歧義,要知道,任何語法上的規(guī)定都是有原因的,如下:

class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;
		_hour = 0;
		_minute = 0;

		_second = 0;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
public:
	Date()
	{
		cout << "Date()" << endl;
	}

	Date(int a = 1, int b = 2)
	{
		cout << "Date(int a = 1, int b = 2)" << endl;
	}
private:
	int _year = 2023;
	int _month = 8;
	int _day = 9;
	Time _t;
};
int main()
{
	Date d;  //這里出現(xiàn)了歧義,
			   //不知道調(diào)用的是全缺省的構(gòu)造還是無參的構(gòu)造
			   //因?yàn)檫@倆種函數(shù)都可以用 Date() 的形式來調(diào)用就出現(xiàn)了歧義
	return 0;
}

?錯(cuò)誤:
C2668?? ?“Date::Date”: 對重載函數(shù)的調(diào)用不明確。

E0339?? ?類 "Date" 包含多個(gè)默認(rèn)構(gòu)造函數(shù)。

? ? ? ? 但是我們?nèi)绻约簜鲗?shí)參給構(gòu)造函數(shù)我們發(fā)現(xiàn)就可以調(diào)用了,所以語法上規(guī)定只能存在一個(gè)默認(rèn)構(gòu)造本質(zhì)就是因?yàn)樯鲜銮闆r會(huì)出現(xiàn)歧義,而在我們平時(shí)實(shí)例化的時(shí)候傳個(gè)實(shí)參就可以避免這種歧義,這個(gè)時(shí)候即使定義了倆個(gè)默認(rèn)構(gòu)造也依然不會(huì)報(bào)錯(cuò),但是我們不建議這樣去寫,因?yàn)檫@樣在項(xiàng)目中的風(fēng)險(xiǎn)是巨大的,比如下面:

class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;
		_hour = 0;
		_minute = 0;

		_second = 0;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
public:
	Date()
	{
		cout << "Date()" << endl;
	}

	Date(int a = 1, int b = 2)
	{
		cout << "Date(int a = 1, int b = 2)" << endl;
	}
private:
	int _year = 2023;
	int _month = 8;
	int _day = 9;
	Time _t;
};
int main()
{
	Date d(1,2);  //不建議,不能因?yàn)楸苊鈭?bào)錯(cuò)就去特殊處理初始化方式。
                  //不能同時(shí)定義倆個(gè)默認(rèn)構(gòu)造,即使編譯器沒有報(bào)錯(cuò)
	return 0;
}

?2.3 構(gòu)造函數(shù)的重載

? ? ? ? 構(gòu)造函數(shù)是支持重載的,可以讓我們應(yīng)對不同的初始化場景(這里在強(qiáng)調(diào)一遍:無參構(gòu)造和全缺省構(gòu)造不能同時(shí)定義,會(huì)出現(xiàn)歧義)。

public:
	Date()
	{
		cout << "Date()" << endl;
	}

	Date(int a, int b = 2)
	{
		cout << "Date(int a = 1, int b = 2)" << endl;
	}
	Date(int year, int month, int day)
	{
		cout << "Date(int year, int month, int day)" << endl;
	}
private:
	int _year = 2023;
	int _month = 8;
	int _day = 9;
	Time _t;
};

3. 析構(gòu)函數(shù)

3.1 概念

? ? ? ? 析構(gòu)函數(shù)的性質(zhì)與構(gòu)造函數(shù)十分的類似,但是與構(gòu)造函數(shù)功能相反,析構(gòu)函數(shù)用于清理對象銷毀后的空間,但它不是完成對對象本身的銷毀,局部對象銷毀工作是由編譯器完成的。而對象在銷毀時(shí)會(huì)自動(dòng)調(diào)用析構(gòu)函數(shù),完成對象中資源的清理工作。

? ? ? ? 析構(gòu)函數(shù)和構(gòu)造函數(shù)一樣無參數(shù)無返回值,命名有所不同,并且析構(gòu)函數(shù)是在對象銷毀時(shí)由編譯器自動(dòng)調(diào)用的,這一點(diǎn)和構(gòu)造函數(shù)類似。特征如下:

  1. 析構(gòu)函數(shù)名是在類名前加上字符 ~。
  2. 無參數(shù)無返回值類型。
  3. ?一個(gè)類只能有一個(gè)析構(gòu)函數(shù)。若未顯式定義,系統(tǒng)會(huì)自動(dòng)生成默認(rèn)的析構(gòu)函數(shù)。注意:析構(gòu) 函數(shù)不能重載。
  4. 對象生命周期結(jié)束時(shí),C++編譯系統(tǒng)系統(tǒng)自動(dòng)調(diào)用析構(gòu)函數(shù)。
class Date
{
public:
	~Date()
	{
		//析構(gòu)函數(shù)
	}
private:
	int _year = 2023;
	int _month = 8;
	int _day = 9;
};

?3.2 系統(tǒng)生成的析構(gòu)函數(shù)

? ? ? ? 如果一個(gè)類里面沒有顯式的定義析構(gòu)函數(shù),系統(tǒng)會(huì)自動(dòng)生成一個(gè)默認(rèn)的析構(gòu)函數(shù),這個(gè)析構(gòu)函數(shù)會(huì)在對象生命周期結(jié)束自動(dòng)調(diào)用,默認(rèn)生成的析構(gòu)函數(shù)對內(nèi)置類型不做處理,對自定義類型會(huì)去調(diào)用它的析構(gòu)函數(shù)。如下:

class Time
{
public:
	~Time()
	{
		cout << "~Time()" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
public:
	~Date()
	{
		cout << "~Date()" << endl;
	}
private:
	int _year = 2023;
	int _month = 8;
	int _day = 9;
	Time _t;
};
int main()
{
	Date d;
	return 0;
}

輸出:

~Date()
~Time()

?3.3 自定義析構(gòu)函數(shù)

? ? ? ? 上面說過,析構(gòu)函數(shù)對內(nèi)置類型是不做處理的,但是我們在日常的使用過程中常常會(huì)涉及到內(nèi)存申請(比如malloc)然后用指針類型去存儲地址,諸如此類的空間我們就不能依賴系統(tǒng)生成的默認(rèn)析構(gòu)以免導(dǎo)致內(nèi)存泄漏,需要自己定義析構(gòu)函數(shù)去手動(dòng)釋放。?

class Date
{
public:
	Date()
	{
		p = (int*)malloc(10 * sizeof(int));
	}
	~Date()
	{
		free(p);
		cout << "~Date()" << endl;
	}
private:
	int _year = 2023;
	int _month = 8;
	int _day = 9;
	int* p = nullptr;
};

4. 拷貝構(gòu)造

4.1 概念

? ? ? ? 拷貝構(gòu)造是構(gòu)造函數(shù)的一種的重載形式,拷貝構(gòu)造只有一個(gè)形參,一般是這個(gè)類型的const引用(只能傳引用,不能傳值,傳值會(huì)造成無窮遞歸),在用已存在的類型對象創(chuàng)建新對象時(shí)由編譯器自動(dòng)調(diào)用。

? ? ? ? 傳引用(正確):

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d)  //拷貝構(gòu)造函數(shù)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
private:
	int _year = 2023;
	int _month = 8;
	int _day = 9;
	int* p = nullptr;
};

? ? ? ? 傳值(無窮遞歸):

【C++精華鋪】5.C++類和對象(中)類的六個(gè)默認(rèn)成員函數(shù),# C++,c++,開發(fā)語言

?4.2 默認(rèn)生成的拷貝構(gòu)造(淺拷貝)

? ? ? ? 在我們沒有顯式的定義拷貝構(gòu)造的時(shí)候會(huì)自動(dòng)生成一個(gè)默認(rèn)的拷貝構(gòu)造,默認(rèn)的拷貝構(gòu)造會(huì)對我們的成員變量按字節(jié)拷貝,也成為值拷貝或者淺拷貝。

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
		p = (int*)malloc(10 * sizeof(int));
	}
    ~Date()
	{
		free(p);
		cout << "~Date()" << endl;
	}

private:
	int _year;
	int _month;
	int _day;
	int* p = nullptr;
};

int main()
{
	Date d1(12, 12, 12);
	Date d2(d1);   //引發(fā)異常
}

【C++精華鋪】5.C++類和對象(中)類的六個(gè)默認(rèn)成員函數(shù),# C++,c++,開發(fā)語言

?4.3 自定義拷貝構(gòu)造(深拷貝)

? ? ? ? 當(dāng)涉及到內(nèi)存管理的時(shí)候編譯器默認(rèn)生成的拷貝構(gòu)造就不夠用了,這個(gè)時(shí)候就需要我們對其進(jìn)行深拷貝。什么是深拷貝呢:就是創(chuàng)建一個(gè)新的對象,將原對象的各項(xiàng)屬性的“值”(數(shù)組的所有元素)拷貝過來。如果是上面的情況我們就需要重新申請一塊空間去保存d.p指向空間里面的數(shù)據(jù)。

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
		p = (int*)malloc(10 * sizeof(int));
		if (p == NULL)
		{
			perror("malloc fail");
			exit(-1);
		}
		for (int i = 0; i < 10; i++)
		{
			p[i] = 10;
		}
	}
	~Date()
	{
		free(p);
		cout << "~Date()" << endl;
	}
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
		p = (int*)malloc(10 * sizeof(int));
		if (p == NULL)
		{
			perror("malloc fail");
			exit(-1);
		}
		for (int i = 0; i < 10; i++)
		{
			p[i] = d.p[i];
		}
	}
private:
	int _year;
	int _month;
	int _day;
	int* p = nullptr;
};
int main()
{
	Date d1(12, 12, 12);
	Date d2(d1);
}

【C++精華鋪】5.C++類和對象(中)類的六個(gè)默認(rèn)成員函數(shù),# C++,c++,開發(fā)語言

?輸出:

~Date()
~Date()

?5. 賦值運(yùn)算符重載

5.1 運(yùn)算符重載

????????C++為了增強(qiáng)代碼的可讀性引入了運(yùn)算符重載,運(yùn)算符重載是具有特殊函數(shù)名的函數(shù),也具有其返回值類型,函數(shù)名字以及參數(shù)列表,其返回值類型與參數(shù)列表與普通的函數(shù)類似。 函數(shù)名字為:關(guān)鍵字operator后面接需要重載的運(yùn)算符符號。 函數(shù)原型:返回值類型?operator操作符(參數(shù)列表)。

注意:

  1. 不能通過連接其他符號來創(chuàng)建新的操作符:比如operator@ 重載操作符必須有一個(gè)類類型參數(shù)
  2. 用于內(nèi)置類型的運(yùn)算符,其含義不能改變,例如:內(nèi)置的整型+,不 能改變其含義
  3. 作為類成員函數(shù)重載時(shí),其形參看起來比操作數(shù)數(shù)目少1,因?yàn)槌蓡T函數(shù)的第一個(gè)參數(shù)為隱藏的this
  4. .* :: sizeof ?: . 注意以上5個(gè)運(yùn)算符不能重載。這個(gè)經(jīng)常在筆試選擇題中出現(xiàn)。

? ? ? ? 運(yùn)算符重載我們有倆種定義方式,可以定義成全局的,也可以直接定義成函數(shù)成員,但是定義成函數(shù)成員我們所看到的形式參數(shù)就會(huì)少一個(gè)(由于this指針隱含參數(shù))?,下面以==為例:

//全局
bool operator==(const Date& d1, const Date& d2)
{
    return d1._year == d2._year
   && d1._month == d2._month
        && d1._day == d2._day;
}

//函數(shù)成員

class Date
{
public:
	bool operator==(const Date& d)
	{
		return (_year == d._year)
			&& (_month == d._month)
			&& (_day == d._day);
	}
private:
	int _year;
	int _month;
	int _day;
};

? ? ? ? 在運(yùn)算符重載里面需要注意的就是“++”和“--”了,因?yàn)檫@倆個(gè)運(yùn)算符有前置和后置的區(qū)別,所以在前置和后置上也作了區(qū)分如下(以++為例):

	Date& operator++();//前置
	Date operator++(int);//后置 多了一個(gè)int形參,僅作標(biāo)記,沒有實(shí)際含義

5.2 賦值運(yùn)算符重載

? ? ? ? 賦值運(yùn)算符重載函數(shù)如果沒有被顯示定義,編譯器會(huì)自動(dòng)生成一個(gè)默認(rèn)的賦值運(yùn)算符重載,拷貝的方式是淺拷貝。與其他運(yùn)算符重載函數(shù)不同的地方是賦值運(yùn)算符重載必須定義成成員函數(shù),不能定義成全局,如果定義成全局,類體里沒有賦值運(yùn)算符重載就會(huì)自動(dòng)生成,這會(huì)與我們在全局定義的發(fā)生沖突。

class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	  
	Date& operator=(const Date& d) //引用傳參避免拷貝提高效率								
	{                              //引用返回因?yàn)橘x值運(yùn)算符支持連續(xù)賦值 d1 = d2 = d3;
		if (this != &d)
		{
			_year= d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}

private:
	int _year;
	int _month;
	int _day;
};

? ? ? ? 如果類型涉及到內(nèi)存管理就需要深拷貝,這里就與拷貝構(gòu)造完全相同,不理解的話可以往前看。

6. 取地址及const取地址操作符重載

? ? ? ? 這倆種運(yùn)算符重載編譯器也會(huì)默認(rèn)生成,絕大部分情況下都不需要我們自己去定義它,除非想讓別人獲取到指定的內(nèi)容!文章來源地址http://www.zghlxwxcb.cn/news/detail-637381.html

class Date
{
public:
	Date* operator&()
	{
		return this;
	}
	const Date* operator&()const
	{
		return this;
	}
private:
	int _year;
	int _month;
	int _day;
};

7. 附:完整日期類(文章中的代碼不取自這里的代碼,都是為了講解知識點(diǎn)臨時(shí)敲的,這里的代碼是完整的日期類(取自比特科技),可以借鑒學(xué)習(xí))

#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	Date(int year = 2023, int month = 8, int day = 10);
	void Print() const;
	int GetMonthDay(int year, int month) const;
	bool operator==(const Date& d) const;
	bool operator!=(const Date& d) const;
	bool operator<(const Date& d) const;
	bool operator<=(const Date& d) const;
	bool operator>(const Date& d) const;
	bool operator>=(const Date& d) const;
	Date& operator+=(int day);
	Date operator+(int day) const;
	Date& operator-=(int day);
	Date operator-(int day) const;
	int operator-(const Date& d) const;
	Date& operator++();
	Date operator++(int);
	Date& operator--();
	Date operator--(int);
private:
	int _year;
	int _month;
	int _day;
};
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}
#include"Date.h"
int Date::GetMonthDay(int year, int month) const
{
	assert(month > 0 && month < 13);

	int monthArray[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 monthArray[month];
	}
}
Date::Date(int year, int month, int day)
{
	if (month > 0 && month < 13
		&& (day > 0 && day <= GetMonthDay(year, month)))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "日期非法,初始化失敗" << endl;
	}
}
void Date::Print() const
{
	cout << _year << " " << _month << " " << _day << endl;
}
bool Date::operator==(const Date& d) const
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}
bool Date::operator<(const Date& d) const
{
	return _year < d._year
		|| (_year == d._year && _month < d._month)
		|| (_year == d._year && _month == d._month && _day < d._day);
}
bool Date::operator<=(const Date& d) const
{
	return *this < d || *this == d;
}
bool Date::operator>(const Date& d) const
{
	return !(*this <= d);
}
bool Date::operator>=(const Date& d) const
{
	return !(*this < d);
}
bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		*this -= -day;
		return *this;
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}
	return *this;
}
Date Date::operator+(int day) const
{
	Date tmp(*this);

	tmp += day;

	return tmp;
}
Date& Date::operator-=(int day) 
{
	if (day < 0)
	{
		*this += -day;
		return *this;
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}

		_day += GetMonthDay(_year, _month);
	}

	return *this;
}
Date Date::operator-(int day) const
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
Date Date::operator++(int)
{
	Date tmp(*this);

	*this += 1;

	return tmp;
}
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;

	return tmp;
}
int Date::operator-(const Date& d) const
{
	Date max = *this;
	Date min = d;
	int flag = 1;

	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return n*flag;
}

到了這里,關(guān)于【C++精華鋪】5.C++類和對象(中)類的六個(gè)默認(rèn)成員函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包