最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

1.問題引出:

給出一段簡單的代碼

代碼段:

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

該段程序會編譯報錯:

2.問題分析

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

注意:

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

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

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

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

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

比如:

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

非const修飾的對象可以調用const修飾的成員函數(shù)(類指針傳參給this指針時讀寫權限被縮小):

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

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

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

二. 類的兩個默認的&運算符重載

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

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

若想取到類對象的地址,我們可以對&運算符進行重載,比如:

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
 
 
 
class Date1
{
public:
    Date1(int year = 2000)
	{
		_year = year;
	}
 
 
	Date1* operator& ()               對&進行重載用于非const修飾的對象的取地址
	{
		return this;
	}
 
	const Date1* operator&() const    對&進行重載用于const修飾的對象的取地址
	{
		return this;
	}
 
private:
	int _year;
};
 
 
int main()
{
	const Date1 a;                     定義一個const修飾的對象a(該對象只可讀,不可被寫入)
    Date1 b;
 
	cout << &a << endl;
	cout << &b << endl;
 
	return 0;
}

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

三. 日期類小練習 

日期類頭文件:

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

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

日期類的成員函數(shù)的實現(xiàn):

#include "Date.h"
 
//Date的構造函數(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);
	}
	
}
//獲取相應月份天數(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;
}
//類對象的日期打印函數(shù)
void Date::Print()const
{
	cout << _year << ' ' << _month << ' ' << _day << ' ' << endl;
}
//判斷某個日期是星期幾,并打印出來
//注意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)]);
}
 
//將 > 運算符進行重載
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;
}
//將 =運算符進行重載
bool Date:: operator==(const Date& date)const
{
	if (date._day == _day && date._month == _month && date._year == _year)
	{
		return true;
	}
	return false;
}
//在邏輯上我們只需定義出大于(或小于)和等于的判斷函數(shù),剩余的判斷函數(shù)我們就可以通過復用的方式簡化代碼書寫
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++()             //實現(xiàn)日期類的前置++
{
	(*this) += 1;
	return (*this);
}
Date Date ::operator++(int)           //實現(xiàn)日期類的后置++
{
	Date tem(*this);
	(*this) += 1;
	return tem;
}
Date& Date:: operator--()             //實現(xiàn)日期類的前置--
{
	(*this) -= 1;
	return (*this);
}
Date Date:: operator--(int)           //實現(xiàn)日期類的后置--
{
	Date tem(*this);
	(*this) -= 1;
	return tem;
}
 
//實現(xiàn)時期相減的操作符重載
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;
	}
}

總結

到此這篇關于C++類中const修飾的成員函數(shù)及日期類小練習的文章就介紹到這了,更多相關C++類const修飾的成員函數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 詳細解析命令行的getopt_long()函數(shù)

    詳細解析命令行的getopt_long()函數(shù)

    getopt_long支持長選項的命令行解析,函數(shù)中的參數(shù)argc和argv通常直接從main()的兩個參數(shù)傳遞而來
    2013-09-09
  • C++ Boost Archive超詳細講解

    C++ Boost Archive超詳細講解

    Boost是為C++語言標準庫提供擴展的一些C++程序庫的總稱。Boost庫是一個可移植、提供源代碼的C++庫,作為標準庫的后備,是C++標準化進程的開發(fā)引擎之一,是為C++語言標準庫提供擴展的一些C++程序庫的總稱
    2022-12-12
  • C語言數(shù)據(jù)結構之鏈隊列的基本操作

    C語言數(shù)據(jù)結構之鏈隊列的基本操作

    這篇文章主要為大家介紹了C語言之鏈隊列的基本操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • CMake的簡單應用

    CMake的簡單應用

    這篇文章主要介紹了CMake的簡單應用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • C語言中結構體封裝全局變量用法說明

    C語言中結構體封裝全局變量用法說明

    這篇文章主要介紹了C語言中結構體封裝全局變量用法說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • C語言實現(xiàn)掃雷小游戲詳解

    C語言實現(xiàn)掃雷小游戲詳解

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • C/C++實現(xiàn)獲取硬盤序列號的示例代碼

    C/C++實現(xiàn)獲取硬盤序列號的示例代碼

    獲取硬盤的序列號、型號和固件版本號,此類功能通常用于做硬盤綁定或硬件驗證操作,下面我們就來學習一下如何使用C/C++實現(xiàn)獲取硬盤序列號吧
    2023-11-11
  • C++?auto關鍵字的小知識點分享

    C++?auto關鍵字的小知識點分享

    這篇文章主要是來和大家介紹一些C++中的小知識點,本文將從auto關鍵字開始講起,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-05-05
  • vs2017智能感知錯誤解決代碼標紅但編譯通過問題

    vs2017智能感知錯誤解決代碼標紅但編譯通過問題

    這篇文章主要介紹了vs2017智能感知錯誤代碼標紅但編譯通過問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • 用C語言實現(xiàn)五子棋游戲

    用C語言實現(xiàn)五子棋游戲

    這篇文章主要為大家詳細介紹了用C語言實現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06

最新評論

乌拉特后旗| 太仓市| 道真| 海城市| 木里| 铜鼓县| 黑山县| 建平县| 翁源县| 邵东县| 玉树县| 井冈山市| 石台县| 潼南县| 株洲县| 浠水县| 南平市| 九台市| 巴林右旗| 永新县| 亚东县| 邢台市| 江安县| 神木县| 晋宁县| 乐昌市| 巴里| 班戈县| 汝州市| 连南| 康定县| 三门县| 崇州市| 泰州市| 同仁县| 崇明县| 旺苍县| 科技| 长丰县| 濮阳市| 唐河县|