定義一個日期類
#include <iostream>
#include <assert.h>
using namespace std;class Date
{
public:void Display();
private:int _year;int _month;int _day;
};
注意:
在定義一個類的時候往往會將其成員變量定義為私有,成員函數定義為公有.這是為了達到軟件工程上的高內聚低耦合的要求
this指針
每一個函數都有自己的形式指針,它的名字是固定的,稱為this,同時this指針是隱式的.
編譯器會對成員函數進行優化,在對象調用成員函數的時候,此時會將該對象的地址傳遞給成員函數的第一個參數
同時this指針是成員函數隱含指針參數,我們程序員不能自己將其加到成員函數的形式參數列表中,也不能在調用的時候顯示的將對象的地址傳給this指針
注意:
this指針是形式參數,既然是形式參數那么它就會在棧上,但是在Linux下,this指針是被存放在寄存器中.同時誰來調用this成員函數那么this指針就指向誰
構造函數
構造函數可以達到原子性,即對象被定義的時候就會被初始化.在定義類的時候我們呢看到通常會將成員函數定義為私有但是私有成員變量在類外是不能被訪問的.為了對成員函數進行初始化,此時就需要構造函數來對私有成員變量進行初始化.同時構造函數只在對象被定義的時候僅僅被執行一次.下面來說一下構造函數的特點
1.函數名和類名相同
2.無返回值
3.對象構造時系統自己調用對應的構造函數
4.構造函數可以重載(函數名相同, 參數不同)
5.構造函數可以在類里面定義也可以在類外面定義
6.如果類中沒有定義一個構造函數,系統會默認生成一個缺省的構造函數,但是當我們定義了構造函數,此時系統就不會生成默認的缺省構造函數,而會定義我們自己定義的構造函數,編譯器在對其進行初始化的時候,內置的類型會被初始化,但是自定義的不會進行初始化
7.無參的構造函數和全缺的構造函數都認為是缺省構造函數.并且缺省的構造函數只能有一個
幾種構造函數
class Date
{
public:// 1.無參的構造函數// Date()// {// }// 2.缺省構造函數Date(int year = 1900, int month = 1, int day = 1){if(year < 1900 || month < 0 || month > 12 || day < 0 || day > 31){assert(0);}_year = year;_month = month;_day = day;}//3. 帶參的構造函數// Date (int year, int month, int day)// {// _year = year;// _month = month;// _day = day;// }// 4. 拷貝構造函數,創建一個對象Date(Date& d){_year = d._year;_month = d._month;_day = d._day;}void Display();
private:int _year;int _month;int _day;
};
注意:
拷貝構造其實就可以理解為一種特殊的構造函數,它是構造函數的重載,同時注意拷貝構造是對象的創建,而我們后面將所說的賦值是兩個對象都已經存在
同時拷貝構造必須傳引用,因為如果傳值的話那么就會出現無窮遞歸(拷貝構造就要傳參,傳參就要拷貝構造)
3.如果自己沒有顯示進行定義,那么系統就會自己生成默認缺省構造函數.缺省的時候拷貝構造函數就會依次拷貝類成員進行初始化
析構函數
1.和類的名字前面加上一個~
2.無返回值
3.一個類有且只有一個析構函數(因為沒有參數,不能構成重載),如果我們自己不寫編譯器會自動生成
4.對象生命周期結束的時候,系統調用析構函數
5.析構函數不能刪除對象,只是對對象進行清理
注意:
如果需要清理成員變量(動態開辟空間),則自己寫析構函數,對象在定義的時候一定調用了構造,生命周期結束的時候就一定調用了析構函數
運算符重載
Date& operator = (const Date& d){this -> _year = d._year;this -> _month = d._month;this -> _day = d._day;return *this;}// d2 == d3// d2.operator(*this, d3)bool operator == (const Date& d) {if(this -> _year == d._year && this -> _month == d._month && this -> _day == _day){return true;}return false;}bool operator != (const Date d){if(!(*this == d)){return true;}return false;}//d1 > d2//d1.operator > (this, d2)bool operator > (const Date& d) {if(this -> _year > d._year){return true;}if(this -> _year == d._year){if(this -> _month > d._month){return true;}}if(this -> _month == d._month){if(this -> _day > d._day){return true;}}return false;}//d2 < d3//d2.operator(this, d3)bool operator < (const Date& d) {if(*this == d || *this > d){return false;}return true;}// d2 >= d3bool operator >= (const Date& d){if(*this > d || *this == d){return true;}return false;}//d2 <= d3bool operator <= (const Date& d){if(*this < d || *this == d){return true;}return false;}//d2++Date& operator ++ () // 前置 {++( this -> _day );if(this -> _day > GetMonthDay(this -> _year, this -> _month)){this -> _day = 1;++(this -> _month);if((this -> _month) > 12){++(this -> _year);this -> _month = 1;}}return *this;}//d2++Date operator ++ (int) // 后置 {Date ret = (*this);++( *this );return ret;}//2018-1-1Date& operator -- (){--(this -> _day);if(this -> _day == 0){(this -> _month)--;if(this -> _month == 0){this -> _year -= 1;this -> _month = 12;}(this -> _day) += GetMonthDay(this -> _year, this -> _month);}return *this;}Date operator -- (int)//后置{Date ret = *this;--(*this);return ret;}Date& operator += (int day){this -> _day = this -> _day + day;while(this -> _day > GetMonthDay(this -> _year, this -> _month)){this -> _day = this -> _day - GetMonthDay(this -> _year, this -> _month);this -> _month ++;if(this -> _month > 12){this -> _month = 1;this -> _year ++;}}return *this;}//d1 + 30//d1.operator(this, day)Date operator + (int day) {Date ret = *this;ret += day;return ret;}Date& operator -= (int day){this -> _day -= day;while(this -> _day <= 0){this -> _month --;if(this -> _month < 1){this -> _month = 12;this -> _year --;}this -> _day += GetMonthDay(this -> _year, this -> _month);}return *this;}Date operator - (int day){Date ret = *this;ret -= day;return ret;}// 2018-1-31 - 2018-1-1// d1.operator(this, d2)int operator - (const Date& d){int count = 0;//記錄天數while(*this > d){--(*this);( *this ).Display();++count;}return count;}void Display()//展示日期類{cout<<_year<<"-"<<_month<<"-"<<_day<<endl;}int GetMonthDay(int year, int month){int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};if(( year %4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 )){day[month] = day[2] + 1;}return day[month];}
輸入探索構造函數
類的成員函數有兩種初始化方式
1.初始化列表
以一個冒號開始, 接著一個逗號分隔數據列表, 每個數據成員都在括號里進行初始化. 盡量使用初始化列表, 因為初始化列表更加高效.
2.構造函數體內進行賦值
初始化列表為什么更加高效
1.初始化列表不寫, 編譯器會自動走一次(自定義成員變量)
2.初始化列表可以認為是成員變量定義的地方
3.構造函數體內賦值會產生臨時變量, 但是初始化列表不會產生臨時變量
那些成員變量必須放在初始化列表中
1.常量成員變量(常量創建時必須初始化)
2.引用類型成員變量(引用成員變量創建時必須初始化)
3.沒有缺省構造函數的自定義類型
4.成員變量初始化的時候按照聲明順序依次初始化,而非初始化列表出現的順序