3_V1-类和对象 -- 默认成员函数
定義一個(gè)日期類
#include <iostream> #include <assert.h> using namespace std;class Date { public:void Display(); private:int _year;int _month;int _day; };注意:
在定義一個(gè)類的時(shí)候往往會(huì)將其成員變量定義為私有,成員函數(shù)定義為公有.這是為了達(dá)到軟件工程上的高內(nèi)聚低耦合的要求
this指針
每一個(gè)函數(shù)都有自己的形式指針,它的名字是固定的,稱為this,同時(shí)this指針是隱式的.
編譯器會(huì)對(duì)成員函數(shù)進(jìn)行優(yōu)化,在對(duì)象調(diào)用成員函數(shù)的時(shí)候,此時(shí)會(huì)將該對(duì)象的地址傳遞給成員函數(shù)的第一個(gè)參數(shù)
同時(shí)this指針是成員函數(shù)隱含指針參數(shù),我們程序員不能自己將其加到成員函數(shù)的形式參數(shù)列表中,也不能在調(diào)用的時(shí)候顯示的將對(duì)象的地址傳給this指針
注意:
this指針是形式參數(shù),既然是形式參數(shù)那么它就會(huì)在棧上,但是在Linux下,this指針是被存放在寄存器中.同時(shí)誰來調(diào)用this成員函數(shù)那么this指針就指向誰
構(gòu)造函數(shù)
構(gòu)造函數(shù)可以達(dá)到原子性,即對(duì)象被定義的時(shí)候就會(huì)被初始化.在定義類的時(shí)候我們呢看到通常會(huì)將成員函數(shù)定義為私有但是私有成員變量在類外是不能被訪問的.為了對(duì)成員函數(shù)進(jìn)行初始化,此時(shí)就需要構(gòu)造函數(shù)來對(duì)私有成員變量進(jìn)行初始化.同時(shí)構(gòu)造函數(shù)只在對(duì)象被定義的時(shí)候僅僅被執(zhí)行一次.下面來說一下構(gòu)造函數(shù)的特點(diǎn)
1.函數(shù)名和類名相同
2.無返回值
3.對(duì)象構(gòu)造時(shí)系統(tǒng)自己調(diào)用對(duì)應(yīng)的構(gòu)造函數(shù)
4.構(gòu)造函數(shù)可以重載(函數(shù)名相同, 參數(shù)不同)
5.構(gòu)造函數(shù)可以在類里面定義也可以在類外面定義
6.如果類中沒有定義一個(gè)構(gòu)造函數(shù),系統(tǒng)會(huì)默認(rèn)生成一個(gè)缺省的構(gòu)造函數(shù),但是當(dāng)我們定義了構(gòu)造函數(shù),此時(shí)系統(tǒng)就不會(huì)生成默認(rèn)的缺省構(gòu)造函數(shù),而會(huì)定義我們自己定義的構(gòu)造函數(shù),編譯器在對(duì)其進(jìn)行初始化的時(shí)候,內(nèi)置的類型會(huì)被初始化,但是自定義的不會(huì)進(jìn)行初始化
7.無參的構(gòu)造函數(shù)和全缺的構(gòu)造函數(shù)都認(rèn)為是缺省構(gòu)造函數(shù).并且缺省的構(gòu)造函數(shù)只能有一個(gè)
幾種構(gòu)造函數(shù)
class Date { public:// 1.無參的構(gòu)造函數(shù)// Date()// {// }// 2.缺省構(gòu)造函數(shù)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. 帶參的構(gòu)造函數(shù)// Date (int year, int month, int day)// {// _year = year;// _month = month;// _day = day;// }// 4. 拷貝構(gòu)造函數(shù),創(chuàng)建一個(gè)對(duì)象Date(Date& d){_year = d._year;_month = d._month;_day = d._day;}void Display(); private:int _year;int _month;int _day; };注意:
拷貝構(gòu)造其實(shí)就可以理解為一種特殊的構(gòu)造函數(shù),它是構(gòu)造函數(shù)的重載,同時(shí)注意拷貝構(gòu)造是對(duì)象的創(chuàng)建,而我們后面將所說的賦值是兩個(gè)對(duì)象都已經(jīng)存在
同時(shí)拷貝構(gòu)造必須傳引用,因?yàn)槿绻麄髦档脑捘敲淳蜁?huì)出現(xiàn)無窮遞歸(拷貝構(gòu)造就要傳參,傳參就要拷貝構(gòu)造)
3.如果自己沒有顯示進(jìn)行定義,那么系統(tǒng)就會(huì)自己生成默認(rèn)缺省構(gòu)造函數(shù).缺省的時(shí)候拷貝構(gòu)造函數(shù)就會(huì)依次拷貝類成員進(jìn)行初始化
析構(gòu)函數(shù)
1.和類的名字前面加上一個(gè)~
2.無返回值
3.一個(gè)類有且只有一個(gè)析構(gòu)函數(shù)(因?yàn)闆]有參數(shù),不能構(gòu)成重載),如果我們自己不寫編譯器會(huì)自動(dòng)生成
4.對(duì)象生命周期結(jié)束的時(shí)候,系統(tǒng)調(diào)用析構(gòu)函數(shù)
5.析構(gòu)函數(shù)不能刪除對(duì)象,只是對(duì)對(duì)象進(jìn)行清理
注意:
如果需要清理成員變量(動(dòng)態(tài)開辟空間),則自己寫析構(gòu)函數(shù),對(duì)象在定義的時(shí)候一定調(diào)用了構(gòu)造,生命周期結(jié)束的時(shí)候就一定調(diào)用了析構(gòu)函數(shù)
運(yùn)算符重載
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;//記錄天數(shù)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];}輸入探索構(gòu)造函數(shù)
類的成員函數(shù)有兩種初始化方式
1.初始化列表
以一個(gè)冒號(hào)開始, 接著一個(gè)逗號(hào)分隔數(shù)據(jù)列表, 每個(gè)數(shù)據(jù)成員都在括號(hào)里進(jìn)行初始化. 盡量使用初始化列表, 因?yàn)槌跏蓟斜砀痈咝?
2.構(gòu)造函數(shù)體內(nèi)進(jìn)行賦值
初始化列表為什么更加高效
1.初始化列表不寫, 編譯器會(huì)自動(dòng)走一次(自定義成員變量)
2.初始化列表可以認(rèn)為是成員變量定義的地方
3.構(gòu)造函數(shù)體內(nèi)賦值會(huì)產(chǎn)生臨時(shí)變量, 但是初始化列表不會(huì)產(chǎn)生臨時(shí)變量
那些成員變量必須放在初始化列表中
1.常量成員變量(常量創(chuàng)建時(shí)必須初始化)
2.引用類型成員變量(引用成員變量創(chuàng)建時(shí)必須初始化)
3.沒有缺省構(gòu)造函數(shù)的自定義類型
4.成員變量初始化的時(shí)候按照聲明順序依次初始化,而非初始化列表出現(xiàn)的順序
總結(jié)
以上是生活随笔為你收集整理的3_V1-类和对象 -- 默认成员函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。