日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

3_V1-类和对象 -- 默认成员函数

發(fā)布時間:2023/11/30 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 3_V1-类和对象 -- 默认成员函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

定義一個日期類

#include <iostream> #include <assert.h> using namespace std;class Date { public:void Display(); private:int _year;int _month;int _day; };

注意:

在定義一個類的時候往往會將其成員變量定義為私有,成員函數(shù)定義為公有.這是為了達到軟件工程上的高內聚低耦合的要求

this指針

每一個函數(shù)都有自己的形式指針,它的名字是固定的,稱為this,同時this指針是隱式的.
編譯器會對成員函數(shù)進行優(yōu)化,在對象調用成員函數(shù)的時候,此時會將該對象的地址傳遞給成員函數(shù)的第一個參數(shù)
同時this指針是成員函數(shù)隱含指針參數(shù),我們程序員不能自己將其加到成員函數(shù)的形式參數(shù)列表中,也不能在調用的時候顯示的將對象的地址傳給this指針

注意:

this指針是形式參數(shù),既然是形式參數(shù)那么它就會在棧上,但是在Linux下,this指針是被存放在寄存器中.同時誰來調用this成員函數(shù)那么this指針就指向誰

構造函數(shù)

構造函數(shù)可以達到原子性,即對象被定義的時候就會被初始化.在定義類的時候我們呢看到通常會將成員函數(shù)定義為私有但是私有成員變量在類外是不能被訪問的.為了對成員函數(shù)進行初始化,此時就需要構造函數(shù)來對私有成員變量進行初始化.同時構造函數(shù)只在對象被定義的時候僅僅被執(zhí)行一次.下面來說一下構造函數(shù)的特點
1.函數(shù)名和類名相同
2.無返回值
3.對象構造時系統(tǒng)自己調用對應的構造函數(shù)
4.構造函數(shù)可以重載(函數(shù)名相同, 參數(shù)不同)
5.構造函數(shù)可以在類里面定義也可以在類外面定義
6.如果類中沒有定義一個構造函數(shù),系統(tǒng)會默認生成一個缺省的構造函數(shù),但是當我們定義了構造函數(shù),此時系統(tǒng)就不會生成默認的缺省構造函數(shù),而會定義我們自己定義的構造函數(shù),編譯器在對其進行初始化的時候,內置的類型會被初始化,但是自定義的不會進行初始化
7.無參的構造函數(shù)和全缺的構造函數(shù)都認為是缺省構造函數(shù).并且缺省的構造函數(shù)只能有一個

幾種構造函數(shù)

class Date { public:// 1.無參的構造函數(shù)// Date()// {// }// 2.缺省構造函數(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. 帶參的構造函數(shù)// Date (int year, int month, int day)// {// _year = year;// _month = month;// _day = day;// }// 4. 拷貝構造函數(shù),創(chuàng)建一個對象Date(Date& d){_year = d._year;_month = d._month;_day = d._day;}void Display(); private:int _year;int _month;int _day; };

注意:

拷貝構造其實就可以理解為一種特殊的構造函數(shù),它是構造函數(shù)的重載,同時注意拷貝構造是對象的創(chuàng)建,而我們后面將所說的賦值是兩個對象都已經(jīng)存在
同時拷貝構造必須傳引用,因為如果傳值的話那么就會出現(xiàn)無窮遞歸(拷貝構造就要傳參,傳參就要拷貝構造)
3.如果自己沒有顯示進行定義,那么系統(tǒng)就會自己生成默認缺省構造函數(shù).缺省的時候拷貝構造函數(shù)就會依次拷貝類成員進行初始化

析構函數(shù)

1.和類的名字前面加上一個~
2.無返回值
3.一個類有且只有一個析構函數(shù)(因為沒有參數(shù),不能構成重載),如果我們自己不寫編譯器會自動生成
4.對象生命周期結束的時候,系統(tǒng)調用析構函數(shù)
5.析構函數(shù)不能刪除對象,只是對對象進行清理

注意:

如果需要清理成員變量(動態(tài)開辟空間),則自己寫析構函數(shù),對象在定義的時候一定調用了構造,生命周期結束的時候就一定調用了析構函數(shù)

運算符重載

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

輸入探索構造函數(shù)

類的成員函數(shù)有兩種初始化方式
1.初始化列表
以一個冒號開始, 接著一個逗號分隔數(shù)據(jù)列表, 每個數(shù)據(jù)成員都在括號里進行初始化. 盡量使用初始化列表, 因為初始化列表更加高效.
2.構造函數(shù)體內進行賦值

初始化列表為什么更加高效
1.初始化列表不寫, 編譯器會自動走一次(自定義成員變量)
2.初始化列表可以認為是成員變量定義的地方
3.構造函數(shù)體內賦值會產生臨時變量, 但是初始化列表不會產生臨時變量

那些成員變量必須放在初始化列表中

1.常量成員變量(常量創(chuàng)建時必須初始化)
2.引用類型成員變量(引用成員變量創(chuàng)建時必須初始化)
3.沒有缺省構造函數(shù)的自定義類型
4.成員變量初始化的時候按照聲明順序依次初始化,而非初始化列表出現(xiàn)的順序

總結

以上是生活随笔為你收集整理的3_V1-类和对象 -- 默认成员函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。