欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2023年01月28日 16:07:20   作者:擺爛小青菜  
將const修飾的“成員函數(shù)”稱之為const成員函數(shù),const修飾類成員函數(shù),表明在該成員函數(shù)中不能對(duì)類的任何成員進(jìn)行修改,下面這篇文章主要給大家介紹了關(guān)于C++類中const修飾的成員函數(shù)及日期類小練習(xí)?的相關(guān)資料,需要的朋友可以參考下

一.const修飾類的成員函數(shù)

1.問題引出:

給出一段簡單的代碼

代碼段:

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
 
class Date1
{
public:
	Date1(int year = 2000)             類的全缺省構(gòu)造函數(shù)(可無參調(diào)用)
	{
		_year = year;
	}
 
	void Prin()
	{
		cout << "Print Date:" << _year << endl;
	}
 
private:
	int _year;
};
 
 
 
int main()
{
	const Date1 a;                       定義一個(gè)const修飾的對(duì)象a(該對(duì)象只可讀,不可被寫入)
	a.Prin();
 
	return 0;
}

該段程序會(huì)編譯報(bào)錯(cuò):

2.問題分析

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

注意:

  • 由于a是const修飾的對(duì)象,因此&a 取出的是 const Date *類型的指針,該指針只可對(duì)a對(duì)象的內(nèi)存空間進(jìn)行讀取操作而不可進(jìn)行寫入操作(該指針的權(quán)限為只可讀取不可寫入)。
  • Prin函數(shù)的形參是Date * const this指針,該類型指針同時(shí)具有讀取和寫入內(nèi)存空間的權(quán)限。
  • 將&a賦給Prin的形參this,就會(huì)使指針的讀寫權(quán)限被放大,因此編譯無法通過(指針是靈活而危險(xiǎn)的存在,編譯器只允許其讀寫權(quán)限被縮小而不允許其權(quán)限被放大)

3.const修飾類的成員函數(shù) 

我們知道類的每個(gè)成員函數(shù)都有一個(gè)隱含的this指針形參(類型為:類名*const this)。

為了使被const修飾的對(duì)象(比如是上面代碼段中的a)可以調(diào)用其成員對(duì)象,C++規(guī)定可以用const來修飾類的成員函數(shù)。

類中被const修飾的“成員函數(shù)”稱為const成員函數(shù),const修飾類成員函數(shù),本質(zhì)上修飾該成員函數(shù)隱含的this指針,表明在該成員函數(shù)中不能對(duì)類的任何成員變量進(jìn)行修改。(修飾后成員函數(shù)的this指針形參類型變?yōu)椋?u>const 類名* const this)

比如:

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

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

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

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

當(dāng)類的成員函數(shù)中沒有對(duì)類的成員變量進(jìn)行任何形式的修改操作時(shí),該成員函數(shù)最好都用const來修飾(這樣安全同時(shí)又使得const修飾的對(duì)象可以調(diào)用該成員函數(shù))以保證代碼的健壯性。

二. 類的兩個(gè)默認(rèn)的&運(yùn)算符重載

編譯器會(huì)默認(rèn)生成兩個(gè)類的&(取地址)重載用于類的取地址操作(如果我們自定義了類的取地址重載則編譯器便不會(huì)再生成默認(rèn)的)

C++中,內(nèi)置運(yùn)算符若要直接作用于類對(duì)象則必須經(jīng)過重載。

若想取到類對(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ì)象只可讀,不可被寫入)
    Date1 b;
 
	cout << &a << endl;
	cout << &b << endl;
 
	return 0;
}

這兩個(gè)默認(rèn)成員函數(shù)一般不用重新自定義 ,編譯器默認(rèn)會(huì)生成,編譯其默認(rèn)生成的&重載和上面我們自定義的成員函數(shù)應(yīng)該沒有什么區(qū)別(至少功能上沒區(qū)別)。

三. 日期類小練習(xí) 

日期類頭文件:

為了提高代碼的可維護(hù)性和可讀性,將日期類的成員函數(shù)的聲明和定義分開寫。

#pragma once
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
 
//記錄日期的類
class Date        
{
public:
 
	//Date的構(gòu)造函數(shù)
	Date(int day=1, int month=1, int year=1);		
	//獲取月份天數(shù)的方法
	int GetMonthday(const int month) const;
	//類對(duì)象的日期打印函數(shù)
	void Print() const;
	//判斷某個(gè)日期是星期幾,并打印出來
	void GetWeekDay() const ;
 
 	//一組比較運(yùn)算符的重載
	bool operator> (const Date& date)const;				
	bool operator==(const Date& date)const;
	//在邏輯上我們只需定義出大于(或小于)和等于的判斷函數(shù),剩余的判斷函數(shù)我們就可以通過復(fù)用的方    
    式簡化代碼書寫
	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)日期類的前置++
	Date operator++(int);							 //實(shí)現(xiàn)日期類的后置++
	Date& operator--();                              //實(shí)現(xiàn)日期類的前置--
	Date operator--(int);                            //實(shí)現(xiàn)日期類的后置--
 
	//實(shí)現(xiàn)時(shí)期相減的操作符重載
	int operator-(const Date& date)const;
	
private:
	int _day;
	int _month;
	int _year;
 
};

日期類的成員函數(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;
}
//類對(duì)象的日期打印函數(shù)
void Date::Print()const
{
	cout << _year << ' ' << _month << ' ' << _day << ' ' << endl;
}
//判斷某個(gè)日期是星期幾,并打印出來
//注意this指針不能由用戶去傳
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ù)我們就可以通過復(fù)用的方式簡化代碼書寫
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)日期類的前置++
{
	(*this) += 1;
	return (*this);
}
Date Date ::operator++(int)           //實(shí)現(xiàn)日期類的后置++
{
	Date tem(*this);
	(*this) += 1;
	return tem;
}
Date& Date:: operator--()             //實(shí)現(xiàn)日期類的前置--
{
	(*this) -= 1;
	return (*this);
}
Date Date:: operator--(int)           //實(shí)現(xiàn)日期類的后置--
{
	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++類中const修飾的成員函數(shù)及日期類小練習(xí)的文章就介紹到這了,更多相關(guān)C++類const修飾的成員函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++簡明分析inline函數(shù)的使用

    C++簡明分析inline函數(shù)的使用

    inline是C++關(guān)鍵字,在函數(shù)聲明或定義中,函數(shù)返回類型前加上關(guān)鍵字inline,即可以把函數(shù)指定為內(nèi)聯(lián)函數(shù)。這樣可以解決一些頻繁調(diào)用的函數(shù)大量消耗??臻g(棧內(nèi)存)的問題
    2022-07-07
  • C語言操作符基礎(chǔ)知識(shí)圖文詳解

    C語言操作符基礎(chǔ)知識(shí)圖文詳解

    這篇文章主要以圖文結(jié)合的方式為大家詳細(xì)介紹了C語言位運(yùn)算基礎(chǔ)知識(shí),感興趣的小伙伴們可以參考一下,希望能給你帶來幫助
    2021-08-08
  • C++ DFS算法實(shí)現(xiàn)走迷宮自動(dòng)尋路

    C++ DFS算法實(shí)現(xiàn)走迷宮自動(dòng)尋路

    這篇文章主要為大家詳細(xì)介紹了C++ DFS算法實(shí)現(xiàn)走迷宮自動(dòng)尋路,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • do...while(0)的妙用詳細(xì)解析

    do...while(0)的妙用詳細(xì)解析

    do...while(0)消除goto語句;通常,如果在一個(gè)函數(shù)中開始要分配一些資源,然后在中途執(zhí)行過程中如果遇到錯(cuò)誤則退出函數(shù),當(dāng)然,退出前先釋放資源
    2013-09-09
  • C語言遞歸函數(shù)與漢諾塔問題簡明理解

    C語言遞歸函數(shù)與漢諾塔問題簡明理解

    遞歸(recursive)函數(shù)是“自己調(diào)用自己”的函數(shù),無論是采用直接或間接調(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詳細(xì)介紹

    這篇文章主要介紹了C++聚合體初始化aggregate initialization,C++有很多初始化對(duì)象的方法。其中之一叫做 聚合體初始化(aggregate initialization) ,這是聚合體專有的一種初始化方法
    2023-02-02
  • DLL加載設(shè)置相對(duì)路徑的方法

    DLL加載設(shè)置相對(duì)路徑的方法

    這篇文章給大家介紹了DLL加載設(shè)置相對(duì)路徑的方法,非常不錯(cuò),具有一定的參考借鑒加載,需要的朋友參考下吧
    2018-08-08
  • C語言中的指針 初階

    C語言中的指針 初階

    這篇文章主要介紹的是關(guān)于初級(jí)階段學(xué)習(xí)C語言中指針的一些內(nèi)容,那就是指針是什么?簡單的說,就是通過它能找到以它為地址的內(nèi)存單元。下面文章我們就來詳細(xì)介紹該內(nèi)容,需要的朋友可以參考一下
    2021-10-10
  • C語言實(shí)現(xiàn)出棧序列合法性判定

    C語言實(shí)現(xiàn)出棧序列合法性判定

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)出棧序列合法性判定,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • OpenCV實(shí)現(xiàn)無縫克隆算法的步驟詳解

    OpenCV實(shí)現(xiàn)無縫克隆算法的步驟詳解

    借助無縫克隆算法,您可以從一張圖像中復(fù)制一個(gè)對(duì)象,然后將其粘貼到另一張圖像中,從而形成一個(gè)看起來無縫且自然的構(gòu)圖。本文將詳解OpenCV實(shí)現(xiàn)無縫克隆算法的步驟,需要的可以參考一下
    2022-06-06

最新評(píng)論