C++類(lèi)中const修飾的成員函數(shù)及日期類(lèi)小練習(xí)

一.const修飾類(lèi)的成員函數(shù)
1.問(wèn)題引出:
給出一段簡(jiǎn)單的代碼
代碼段:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
class Date1
{
public:
Date1(int year = 2000) 類(lèi)的全缺省構(gòu)造函數(shù)(可無(wú)參調(diào)用)
{
_year = year;
}
void Prin()
{
cout << "Print Date:" << _year << endl;
}
private:
int _year;
};
int main()
{
const Date1 a; 定義一個(gè)const修飾的對(duì)象a(該對(duì)象只可讀,不可被寫(xiě)入)
a.Prin();
return 0;
}該段程序會(huì)編譯報(bào)錯(cuò):

2.問(wèn)題分析
上述代碼段出錯(cuò)的原因要從類(lèi)的成員函數(shù)的隱含參數(shù)this指針出發(fā)進(jìn)行分析:

注意:
- 由于a是const修飾的對(duì)象,因此&a 取出的是 const Date *類(lèi)型的指針,該指針只可對(duì)a對(duì)象的內(nèi)存空間進(jìn)行讀取操作而不可進(jìn)行寫(xiě)入操作(該指針的權(quán)限為只可讀取不可寫(xiě)入)。
- Prin函數(shù)的形參是Date * const this指針,該類(lèi)型指針同時(shí)具有讀取和寫(xiě)入內(nèi)存空間的權(quán)限。
- 將&a賦給Prin的形參this,就會(huì)使指針的讀寫(xiě)權(quán)限被放大,因此編譯無(wú)法通過(guò)(指針是靈活而危險(xiǎn)的存在,編譯器只允許其讀寫(xiě)權(quán)限被縮小而不允許其權(quán)限被放大)
3.const修飾類(lèi)的成員函數(shù)
我們知道類(lèi)的每個(gè)成員函數(shù)都有一個(gè)隱含的this指針形參(類(lèi)型為:類(lèi)名*const this)。
為了使被const修飾的對(duì)象(比如是上面代碼段中的a)可以調(diào)用其成員對(duì)象,C++規(guī)定可以用const來(lái)修飾類(lèi)的成員函數(shù)。
類(lèi)中被const修飾的“成員函數(shù)”稱(chēng)為const成員函數(shù),const修飾類(lèi)成員函數(shù),本質(zhì)上修飾該成員函數(shù)隱含的this指針,表明在該成員函數(shù)中不能對(duì)類(lèi)的任何成員變量進(jìn)行修改。(修飾后成員函數(shù)的this指針形參類(lèi)型變?yōu)椋?u>const 類(lèi)名* const this)
比如:


const修飾的對(duì)象不可以調(diào)用非const修飾的成員函數(shù)(類(lèi)指針傳參給this指針時(shí)讀寫(xiě)權(quán)限被放大):

非const修飾的對(duì)象可以調(diào)用const修飾的成員函數(shù)(類(lèi)指針傳參給this指針時(shí)讀寫(xiě)權(quán)限被縮小):

const修飾的成員函數(shù)內(nèi)不可以調(diào)用其它的非const修飾的成員函數(shù)(this指針之間傳參時(shí)讀寫(xiě)權(quán)限被放大):

非const修飾的成員函數(shù)內(nèi)可以調(diào)用其它的const修飾的成員函數(shù)(this指針之間傳參時(shí)讀寫(xiě)權(quán)限被縮小):

當(dāng)類(lèi)的成員函數(shù)中沒(méi)有對(duì)類(lèi)的成員變量進(jìn)行任何形式的修改操作時(shí),該成員函數(shù)最好都用const來(lái)修飾(這樣安全同時(shí)又使得const修飾的對(duì)象可以調(diào)用該成員函數(shù))以保證代碼的健壯性。
二. 類(lèi)的兩個(gè)默認(rèn)的&運(yùn)算符重載

編譯器會(huì)默認(rèn)生成兩個(gè)類(lèi)的&(取地址)重載用于類(lèi)的取地址操作(如果我們自定義了類(lèi)的取地址重載則編譯器便不會(huì)再生成默認(rèn)的)
C++中,內(nèi)置運(yùn)算符若要直接作用于類(lèi)對(duì)象則必須經(jīng)過(guò)重載。
若想取到類(lèi)對(duì)象的地址,我們可以對(duì)&運(yùn)算符進(jìn)行重載,比如:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
class Date1
{
public:
Date1(int year = 2000)
{
_year = year;
}
Date1* operator& () 對(duì)&進(jìn)行重載用于非const修飾的對(duì)象的取地址
{
return this;
}
const Date1* operator&() const 對(duì)&進(jìn)行重載用于const修飾的對(duì)象的取地址
{
return this;
}
private:
int _year;
};
int main()
{
const Date1 a; 定義一個(gè)const修飾的對(duì)象a(該對(duì)象只可讀,不可被寫(xiě)入)
Date1 b;
cout << &a << endl;
cout << &b << endl;
return 0;
}

這兩個(gè)默認(rèn)成員函數(shù)一般不用重新自定義 ,編譯器默認(rèn)會(huì)生成,編譯其默認(rèn)生成的&重載和上面我們自定義的成員函數(shù)應(yīng)該沒(méi)有什么區(qū)別(至少功能上沒(méi)區(qū)別)。
三. 日期類(lèi)小練習(xí)
日期類(lèi)頭文件:
為了提高代碼的可維護(hù)性和可讀性,將日期類(lèi)的成員函數(shù)的聲明和定義分開(kāi)寫(xiě)。
#pragma once
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
//記錄日期的類(lèi)
class Date
{
public:
//Date的構(gòu)造函數(shù)
Date(int day=1, int month=1, int year=1);
//獲取月份天數(shù)的方法
int GetMonthday(const int month) const;
//類(lèi)對(duì)象的日期打印函數(shù)
void Print() const;
//判斷某個(gè)日期是星期幾,并打印出來(lái)
void GetWeekDay() const ;
//一組比較運(yùn)算符的重載
bool operator> (const Date& date)const;
bool operator==(const Date& date)const;
//在邏輯上我們只需定義出大于(或小于)和等于的判斷函數(shù),剩余的判斷函數(shù)我們就可以通過(guò)復(fù)用的方
式簡(jiǎn)化代碼書(shū)寫(xiě)
bool operator<(const Date& date)const;
bool operator>=(const Date& date)const;
bool operator<=(const Date& date)const;
bool operator!=(const Date& date)const;
//一組日期+(-)整數(shù)的操作和+=(-=)整數(shù)的操作
Date operator+(const int day)const;
Date& operator+=(const int day);
Date operator-(const int day)const;
Date& operator-=(const int day);
Date& operator=(const Date& date);
//一組前置++(--)和后置++(--)的重載
Date& operator++(); //實(shí)現(xiàn)日期類(lèi)的前置++
Date operator++(int); //實(shí)現(xiàn)日期類(lèi)的后置++
Date& operator--(); //實(shí)現(xiàn)日期類(lèi)的前置--
Date operator--(int); //實(shí)現(xiàn)日期類(lèi)的后置--
//實(shí)現(xiàn)時(shí)期相減的操作符重載
int operator-(const Date& date)const;
private:
int _day;
int _month;
int _year;
};日期類(lèi)的成員函數(shù)的實(shí)現(xiàn):
#include "Date.h"
//Date的構(gòu)造函數(shù)
Date ::Date(int day, int month, int year)
{
_day = day;
_month = month;
_year = year;
if (_year <= 0 || _month <= 0 || _month > 12 || _day <= 0 || _day > GetMonthday(_month))
{
cout << "date invalued please exit the app" << endl;
exit(0);
}
}
//獲取相應(yīng)月份天數(shù)的方法
int Date::GetMonthday(const int month)const
{
static const int arr[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int ret = arr[month - 1];
if (((0 == _year % 4 && 0 != _year % 100) || (0 == _year % 400)) && 2 == month)
{
ret++;
}
return ret;
}
//類(lèi)對(duì)象的日期打印函數(shù)
void Date::Print()const
{
cout << _year << ' ' << _month << ' ' << _day << ' ' << endl;
}
//判斷某個(gè)日期是星期幾,并打印出來(lái)
//注意this指針不能由用戶(hù)去傳
void Date::GetWeekDay()const
{
const char* arr[7] = { "星期一","星期二","星期三","星期四","星期五","星期六","星期日" };
const Date tem(1, 1, 1900);
const int subret = (*this)-tem;
printf("%s\n", arr[(subret % 7)]);
}
//將 > 運(yùn)算符進(jìn)行重載
bool Date ::operator> (const Date& date)const
{
if (_year > date._year)
{
return true;
}
else if (_year == date._year && _month > date._month)
{
return true;
}
else if (_year == date._year && _month == date._month && _day > date._day)
{
return true;
}
return false;
}
//將 =運(yùn)算符進(jìn)行重載
bool Date:: operator==(const Date& date)const
{
if (date._day == _day && date._month == _month && date._year == _year)
{
return true;
}
return false;
}
//在邏輯上我們只需定義出大于(或小于)和等于的判斷函數(shù),剩余的判斷函數(shù)我們就可以通過(guò)復(fù)用的方式簡(jiǎn)化代碼書(shū)寫(xiě)
bool Date :: operator>= (const Date& date)const
{
if ((*this) > date || (*this) == date)
{
return true;
}
return false;
}
bool Date :: operator < (const Date& date)const
{
if ((*this) >= date)
{
return false;
}
return true;
}
bool Date :: operator<=(const Date& date)const
{
if ((*this) > date)
{
return false;
}
return true;
}
bool Date:: operator!= (const Date& date)const
{
if ((*this) == date)
{
return false;
}
return true;
}
//一組日期+(-)整數(shù)的操作和+=(-=)整數(shù)的操作
Date& Date::operator+=(const int day)
{
if (day < 0)
{
(*this) -= (-day);
return (*this);
}
_day += day;
while (_day > GetMonthday(_month))
{
if (_month < 12)
{
_day -= GetMonthday(_month);
_month++;
}
else
{
_day -= GetMonthday(_month);
_year++;
_month = 1;
}
}
return (*this);
}
Date Date::operator+(const int day)const
{
Date tem(*this);
tem += day;
return tem;
}
Date& Date::operator-=(const int day)
{
if (day < 0)
{
(*this) += (-day);
return (*this);
}
_day -= day;
while (_day <= 0 )
{
if (_month > 1)
{
_month--;
_day += GetMonthday(_month);
}
else
{
_year--;
_month = 12;
_day += GetMonthday(_month);
}
}
if (_year <= 0)
{
cout << "operation invalued" << endl;
exit(0);
}
return (*this);
}
Date Date::operator-(int day)const
{
Date tem(*this);
tem -= (day);
return tem;
}
Date& Date ::operator=(const Date& date)
{
if (this != &date)
{
_day = date._day;
_month = date._month;
_year = date._year;
}
return (*this);
}
//一組前置++(--)和后置++(--)的重載
Date& Date ::operator++() //實(shí)現(xiàn)日期類(lèi)的前置++
{
(*this) += 1;
return (*this);
}
Date Date ::operator++(int) //實(shí)現(xiàn)日期類(lèi)的后置++
{
Date tem(*this);
(*this) += 1;
return tem;
}
Date& Date:: operator--() //實(shí)現(xiàn)日期類(lèi)的前置--
{
(*this) -= 1;
return (*this);
}
Date Date:: operator--(int) //實(shí)現(xiàn)日期類(lèi)的后置--
{
Date tem(*this);
(*this) -= 1;
return tem;
}
//實(shí)現(xiàn)時(shí)期相減的操作符重載
int Date::operator-(const Date& date)const
{
int count = 0;
Date min;
if ((*this) < date)
{
min = (*this);
while (min != date)
{
min++;
count++;
}
return -count;
}
else
{
min = date;
while (min != (*this))
{
min++;
count++;
}
return count;
}
}總結(jié)
到此這篇關(guān)于C++類(lèi)中const修飾的成員函數(shù)及日期類(lèi)小練習(xí)的文章就介紹到這了,更多相關(guān)C++類(lèi)const修飾的成員函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++ DFS算法實(shí)現(xiàn)走迷宮自動(dòng)尋路
這篇文章主要為大家詳細(xì)介紹了C++ DFS算法實(shí)現(xiàn)走迷宮自動(dòng)尋路,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
C語(yǔ)言遞歸函數(shù)與漢諾塔問(wèn)題簡(jiǎn)明理解
遞歸(recursive)函數(shù)是“自己調(diào)用自己”的函數(shù),無(wú)論是采用直接或間接調(diào)用方式。間接遞歸意味著函數(shù)調(diào)用另一個(gè)函數(shù)(然后可能又調(diào)用第三個(gè)函數(shù)等),最后又調(diào)用第一個(gè)函數(shù)。因?yàn)楹瘮?shù)不可以一直不停地調(diào)用自己,所以遞歸函數(shù)一定具備結(jié)束條件2022-07-07
C++聚合體初始化aggregate initialization詳細(xì)介紹
這篇文章主要介紹了C++聚合體初始化aggregate initialization,C++有很多初始化對(duì)象的方法。其中之一叫做 聚合體初始化(aggregate initialization) ,這是聚合體專(zhuān)有的一種初始化方法2023-02-02
OpenCV實(shí)現(xiàn)無(wú)縫克隆算法的步驟詳解
借助無(wú)縫克隆算法,您可以從一張圖像中復(fù)制一個(gè)對(duì)象,然后將其粘貼到另一張圖像中,從而形成一個(gè)看起來(lái)無(wú)縫且自然的構(gòu)圖。本文將詳解OpenCV實(shí)現(xiàn)無(wú)縫克隆算法的步驟,需要的可以參考一下2022-06-06

