C++ plus6th 1-3章

1.關(guān)于函數(shù)定義時(shí)的省略情形

  • 在函數(shù)定義時(shí)抹腿,如果省略函數(shù)前面的返回值類型:在C標(biāo)準(zhǔn)中默認(rèn)返回int值尸闸,但在C++語(yǔ)言中,并沒(méi)有該規(guī)定韵吨,其必須有返回值類型迹冤。

  • 在函數(shù)定義時(shí)讽营,如果省略參數(shù)列表:在C++中,默認(rèn)該函數(shù)無(wú)參數(shù)(void)泡徙,但在C語(yǔ)言中橱鹏,并無(wú)該規(guī)定,必須明確表示有無(wú)參數(shù)列表堪藐;

  • main函數(shù)必須有返回值莉兰,所以即使你定義了main函數(shù)的返回值類型為void,編譯器同樣會(huì)在程序結(jié)束前隱式添加return 0礁竞;語(yǔ)句糖荒。

  • 所以,在函數(shù)定義時(shí)模捂,為了減少錯(cuò)誤的發(fā)生捶朵,無(wú)論C或是C++,我們都要明確所定義函數(shù)的返回值類型和參數(shù)列表狂男。

2.關(guān)于注釋符

  • C風(fēng)格的注釋符為/* */,它可以位于單獨(dú)行或跨行综看,但起始和終止符必須配對(duì)使用

  • C++風(fēng)格的注釋符為//,它僅位于單獨(dú)行(也可位于代碼行后面)岖食,以//為起始红碑,行尾為終止符。

  • C++兼容C的注釋符泡垃,C99以后版本也兼容C++的注釋符

3.關(guān)于文件擴(kuò)展名

  • C++源文件的擴(kuò)展名有:.cpp析珊,.C.cxx兔毙,取決于你的C++系統(tǒng)唾琼。

4. 關(guān)于頭文件

  • 在使用C++的輸入輸出函數(shù)(cout和cin)時(shí)兄春,在編輯代碼前必須包含兩句:

#include <iostream.h> 適用于舊版本C++編譯器

或最新版本編譯器

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n29" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> #include <iostream>
using namespace std;</pre>

關(guān)于頭文件的使用澎剥,C++舊版本是和C一樣的,使用.h作為擴(kuò)展名(例如:#include <iostream.h>),但后來(lái)為了區(qū)分是C還是C++的頭文件哑姚,新版C++規(guī)定頭文件不再添加.h的擴(kuò)展(例如:#include <iostream>)祭饭,但有些共同的頭文件,C++又以在原文件名前添加c的形式表示叙量,例如C中的math.h倡蝙,在C++以cmath表示。

image-20210221160232333

5. 關(guān)于命名空間

  • 在使用C++的輸入輸出函數(shù)(cout和cin)時(shí)绞佩,在編輯代碼前必須包含兩句:

#include <iostream.h> (適用于舊版本C++編譯器)

或最新版本編譯器:(所有函數(shù)均默認(rèn)使用std命名空間)

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n38" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> #include <iostream>
using namespace std;//其作用范圍取決于它的位置
...
cout<<"hello!"<<endl;</pre>

或者僅將需要用到的函數(shù)在編碼前明確使用std命名空間)

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n40" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> #include <iostream>
using std::cout; //或在使用時(shí)直接全路徑調(diào)用
using std::cin;
using std::endl;
...
cout<<"hello!"<<endl;</pre>

6. 關(guān)于變量聲明和賦值語(yǔ)句

  • 在早期的C語(yǔ)言中寺鸥,為了方便管理,一般將一段程序中的所有變量聲明集中放在程序開(kāi)頭位置處品山,但后來(lái)的C++及C99以后胆建,規(guī)定變量聲明應(yīng)該緊挨著變量第一次使用前的位置.

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n46" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> ...
int var;
var = 10;
...</pre>

  • 賦值操作符=可以串聯(lián)使用

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c" cid="n50" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> int Var1,Var2,Var3;
Var1 = Var2 = Var3 = 10;</pre>

  • 賦值操作符=可以自引用

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c" cid="n54" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> Var = Var - 1;</pre>

7. cout和printf()

  • 在C語(yǔ)言中,利用printf()函數(shù)輸出一個(gè)數(shù)字時(shí)肘交,需要用%d明確告知笆载,但cout會(huì)自動(dòng)判別并自動(dòng)輸出

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c" cid="n59" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> printf("Printing a string: %s", "25");
printf("Printing a string: %d", 25);</pre>

C++中:

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n61" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> int Var = 25;
cout << Var;</pre>

此處,之所以cout可以識(shí)別Var,是因?yàn)檎巫兞繉?duì)<<操作符進(jìn)行了符號(hào)重載,其完成了2件事:一是取得變量Var的二進(jìn)制數(shù)值大小 ;二是將該數(shù)值轉(zhuǎn)化成相應(yīng)的數(shù)字字符串并輸出.cin同理.

  • 另外通過(guò)hex,oct關(guān)鍵字涯呻,可以改變cout輸出數(shù)字的進(jìn)制(默認(rèn)輸出10進(jìn)制dec)凉驻。例如:

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n66" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> #include <iostream>
using namespace std;
int main()
{
using namespace std;
int chest = 42;
int waist = 42;
int inseam = 42;
cout << “Monsieur cuts a striking figure!” << endl;
cout << “chest = “ << chest << “ (decimal)” << endl;
cout << hex; // manipulator for changing number base
cout << “waist = “ << waist << “ hexadecimal” << endl;
cout << oct; // manipulator for changing number base
cout << “inseam = “ << inseam << “ (octal)” << endl;
return 0;
}

輸出:
Monsieur cuts a striking figure!
chest = 42 (decimal)
waist = 2a hexadecimal
inseam = 52 (octal)</pre>

However, if you omitted the using directive and instead used std::cout, std::endl, std::hex, and std::oct, you could still use plain hex as the name for a variable.

8. 關(guān)于變量名

C++中:

  • 變量名僅能由字母、數(shù)字和下劃線組成复罐,且數(shù)字不能開(kāi)頭涝登。

  • 以雙下劃線開(kāi)頭或單下劃線加大寫(xiě)字母開(kāi)頭的變量是系統(tǒng)保留的,用戶不得使用.

  • 以單下劃線開(kāi)頭命名的變量也是系統(tǒng)保留的,一般視為全局變量市栗,用戶不得使用.

  • 在C語(yǔ)言中缀拭,變量名只有前63個(gè)字符是有意義的。也就是說(shuō)兩個(gè)變量名填帽,如果他們的前63個(gè)字符是相同的蛛淋,則C編譯器認(rèn)為該兩個(gè)變量就是同一個(gè)。而在C++中篡腌,變量名長(zhǎng)度沒(méi)有限制褐荷,所有字符均有意義。

9.關(guān)于整形

image-20210226114436919

在任一系統(tǒng)中嘹悼,short\int\long必須滿足以下條件

  • short至少16位寬

  • int位寬不得小于short

  • long至少32位叛甫,且不得小于int

  • long long至少64位,且不得小于long

整型變量類型的選擇:

  • int是最常選擇的杨伙,因?yàn)橛?jì)算機(jī)對(duì)它的處理效率最高

  • 如果你能確定變量的值不會(huì)取負(fù)其监,那就選擇unsigned類型的。

  • 如果變量的值比較大(大于16位數(shù)所能表示)限匣,那就選long型

  • 如果需要考慮到內(nèi)存資源消耗抖苦,特別是需要定義一個(gè)很大的整型數(shù)組時(shí),那就選short

  • 如果你僅僅使用單字節(jié),那就選char锌历。

來(lái)自limits.h文件的符號(hào)常量:

符號(hào)常量 含義
CHAR_BIT char類型的位數(shù)
CHAR_MAX Maximum char value
CHAR_MIN Minimum char value
SCHAR_MAX Maximum signed char value
SCHAR_MIN Minimum signed char value
UCHAR -MAX Maximum unsigned char value
SHRT_MAX Maximum short value
SHRT_MIN Minimum short value
USHRT_MAX Maximum unsigned short value
INT_MAX Maximum int value
INT_MIN Minimum int value
UINT_MAX Maximum unsigned int value
LONG_MAX Maximum long value
LONG_MIN Minimum long value
ULONG_MAX Maximum unsigned long value

9.1 關(guān)于賦值

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n155" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> int owls = 101; // traditional C initialization
int wrens(432); // alternative C++ syntax, set wrens to 432</pre>

9.2 關(guān)于自定義符號(hào)常量

在C語(yǔ)言中贮庞,我們一般會(huì)利用#define語(yǔ)句來(lái)定義自己的符號(hào)常量,但在C++中究西,我們一般會(huì)使用const關(guān)鍵字來(lái)定義一個(gè)符號(hào)常量(名字一般采用全大寫(xiě)字母)窗慎,并在定義的同時(shí)進(jìn)行賦值。因?yàn)楹笳呖梢灾付ǔW兞康臄?shù)據(jù)類型卤材,可以控制作用范圍遮斥,可以修飾更多復(fù)雜數(shù)據(jù)類型(數(shù)組、結(jié)構(gòu)體)扇丛,所以比define宏更具優(yōu)勢(shì)伏伐。

當(dāng)然在C語(yǔ)言中也可以使用const關(guān)鍵字,但其與C++中的最主要的區(qū)別:一是在于作用范圍晕拆;二是在C++中(不在C中)藐翎,可以使用const值來(lái)定義一個(gè)數(shù)組的大小。

2.0版C++之前实幕,和C語(yǔ)言一樣吝镣,用int型來(lái)存儲(chǔ)字符常量的值(但字符變量仍用char型存儲(chǔ)),但在之后C++就用char型來(lái)存儲(chǔ)字符常量的值了昆庇。

字符常量的表示方法:

  • 單引號(hào)加字符:例如 ‘a(chǎn)’

  • 反斜杠加字母表示特殊字符:\a(響鈴)末贾,\n(換行),\r(回車),\b(回退)

  • 反斜杠加八進(jìn)制或十六進(jìn)制數(shù)字:\032\x1a表示CTRL+Z

  • \u加8個(gè)16進(jìn)制數(shù) 或 \U加16個(gè)16進(jìn)制數(shù)(這些數(shù)字代表ISO 10646字符集編碼):\u00E2 代表 a

9.3 關(guān)于數(shù)字常量的默認(rèn)適配類型

  • 對(duì)于單純的整型數(shù)字來(lái)說(shuō),如果不加任何修飾整吆,則一般認(rèn)為其為十進(jìn)制整數(shù)拱撵,如2000;

  • 如果在數(shù)字后添加lL則代表此數(shù)字為long型表蝙,添加uU則代表unsigned

    • 因?yàn)樾?xiě)的l容易與1混淆拴测,一般使用LULLU都代表unsigned long意思
  • 在C++中府蛇,默認(rèn)會(huì)選擇最小適配的整數(shù)類型存儲(chǔ)給定的整型常量

    • 對(duì)于十進(jìn)制整數(shù):依次匹配int集索、longunsigned long汇跨;

    • 對(duì)于八進(jìn)制或十六進(jìn)制:依次匹配int务荆、unsigned intlong穷遂、unsigned long函匕;

9.4 關(guān)于char

不同關(guān)于int,long型蚪黑,char既不明確代表signed char盅惜,也不明確代表 unsigned char吸耿,不同的系統(tǒng)選擇也不同,所以為了程序的兼容性酷窥,開(kāi)發(fā)者可以自己明確。

因?yàn)閏har只有8位伴网,最多僅表示256個(gè)不同的字符蓬推,在系統(tǒng)使用的字符集超過(guò)這個(gè)數(shù)后,char型就不足以表示全部的字符了澡腾。為了解決該問(wèn)題沸伏,C++使用wchar_t類型來(lái)表示擴(kuò)展的字符集,它本質(zhì)上是16位二進(jìn)制整數(shù)动分,既可以是unsigned short毅糟,也可以是int類型。

  • C++采用在字符或字符串前綴 ‘L’ 表示寬字符澜公,例如:L’P’ 或 L”tall”

  • C++使用wcout 和 wcin來(lái)處理wchar_t類型的字符流

10. 關(guān)于浮點(diǎn)型

image-20210226114704117
  • C++中有3種浮點(diǎn)型數(shù)據(jù):float(32位)姆另、double(64位)、long double(80-128位)

  • 在C語(yǔ)言中的float.h或C++中的cfloat頭文件坟乾,規(guī)定了實(shí)數(shù)有效數(shù)字和尾數(shù)等的值:

image-20210225152037877
  • 在C++中有兩種表示方法:一是固定小數(shù)點(diǎn)的方式1212.3456迹辐,二是使用E或e的方式12.123456e2;使用cout命令輸出實(shí)數(shù)時(shí)需要設(shè)置輸出方式甚侣,為此:

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n206" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> cout.setf(ios_base::fixed, ios_base::floatfield) //fixed-point
cout.setf(ios::fixed, ios::floatfield) //fixed-point C++老版本 </pre>

10.1 關(guān)于浮點(diǎn)常數(shù)

  • 默認(rèn)浮點(diǎn)常數(shù)采用double型存儲(chǔ),若在常量后綴f或F殷费,則采用float型存儲(chǔ)印荔,若在常量后綴l或L則用long double型存儲(chǔ)。因?yàn)樾?xiě)的l很像數(shù)字1详羡,所以一般使用L仍律。

  • float最大只能保證6位有效數(shù)字的精度,超過(guò)6位有效數(shù)字時(shí)实柠,建議使用duoble型

10.2 關(guān)于浮點(diǎn)數(shù)的運(yùn)算

  • 除法(/):若2個(gè)操作數(shù)均為整數(shù)染苛,則結(jié)果為整數(shù);若至少有一個(gè)操作數(shù)為浮點(diǎn)數(shù)主到,則結(jié)果為浮點(diǎn)數(shù)茶行。

  • 取余(%):左右操作數(shù)必須為整數(shù),否則報(bào)錯(cuò)登钥!

10.3 關(guān)于類型轉(zhuǎn)換

類型轉(zhuǎn)換發(fā)生在賦值畔师、運(yùn)算表達(dá)式和參數(shù)傳遞時(shí):

  • 賦值時(shí)的轉(zhuǎn)換:整數(shù)向浮點(diǎn)數(shù)轉(zhuǎn)換時(shí)后面添加小數(shù)0;浮點(diǎn)數(shù)向整數(shù)轉(zhuǎn)換時(shí)截?cái)嗪竺娴男?shù)牧牢,但若超過(guò)整數(shù)范圍時(shí)無(wú)確定意義看锉;

  • 表達(dá)式中的轉(zhuǎn)換:C++將自動(dòng)將bool,char,unsigned char,signed char,short姿锭,unsigned short的值轉(zhuǎn)化為int型值再做計(jì)算(true一般轉(zhuǎn)為1,false轉(zhuǎn)化為0)伯铣。若short 和int的位數(shù)一樣呻此,則unsigned short轉(zhuǎn)化為unsigned int。類似的wchar_t類型將自動(dòng)轉(zhuǎn)化為能包含它大小的最小如下類型:int腔寡,unsigned int焚鲜,long,unsigned long放前。

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n226" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> short chickens = 20; //line 1
short ducks =35; //line 2
short fowl = chickens + ducks; //line 3</pre>

在執(zhí)行第三條語(yǔ)句時(shí)忿磅,計(jì)算機(jī)傾向于先將chickens和ducks轉(zhuǎn)化為int型,求得結(jié)果后在轉(zhuǎn)化為short型賦值給fowl變量凭语。因?yàn)閷?duì)CPU而言葱她,int型是計(jì)算效率最快的一種數(shù)據(jù)類型。

簡(jiǎn)而言之似扔,當(dāng)表達(dá)式存在兩種不同類型的數(shù)據(jù)進(jìn)行運(yùn)算時(shí)吨些,數(shù)值范圍小的將自動(dòng)向數(shù)值范圍大的轉(zhuǎn)化:

image-20210226110207482
  • 傳參時(shí)的轉(zhuǎn)換:一般來(lái)說(shuō),參數(shù)的類型轉(zhuǎn)換由函數(shù)原型中聲明的進(jìn)行炒辉,但C++將自動(dòng)轉(zhuǎn)換char锤灿,short(包含signed和unsigned)型的參數(shù)為int型。同時(shí)為了兼容經(jīng)典C辆脸,自動(dòng)將float轉(zhuǎn)化成double但校。

10.4 強(qiáng)制類型轉(zhuǎn)換

在C++中有多種形式的強(qiáng)制類型轉(zhuǎn)換:(以將int型的thorn變量轉(zhuǎn)化為long型為例)

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="c++" cid="n235" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px 0px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> (long)thron //C語(yǔ)言形式
long(thron) //C++形式,使之看起來(lái)像函數(shù)調(diào)用
static_cast<long> (thron) //</pre>

強(qiáng)制類型轉(zhuǎn)換并不實(shí)際改變?cè)兞康念愋头惹猓皇巧梢粋€(gè)臨時(shí)數(shù)值状囱。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市倘是,隨后出現(xiàn)的幾起案子亭枷,更是在濱河造成了極大的恐慌,老刑警劉巖搀崭,帶你破解...
    沈念sama閱讀 218,941評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叨粘,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡瘤睹,警方通過(guò)查閱死者的電腦和手機(jī)升敲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)轰传,“玉大人驴党,你說(shuō)我怎么就攤上這事』癫纾” “怎么了港庄?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,345評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵倔既,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我鹏氧,道長(zhǎng)渤涌,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,851評(píng)論 1 295
  • 正文 為了忘掉前任把还,我火速辦了婚禮实蓬,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘笨篷。我一直安慰自己,他們只是感情好瓣履,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,868評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布率翅。 她就那樣靜靜地躺著,像睡著了一般袖迎。 火紅的嫁衣襯著肌膚如雪冕臭。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,688評(píng)論 1 305
  • 那天燕锥,我揣著相機(jī)與錄音辜贵,去河邊找鬼。 笑死归形,一個(gè)胖子當(dāng)著我的面吹牛托慨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播暇榴,決...
    沈念sama閱讀 40,414評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼厚棵,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了蔼紧?” 一聲冷哼從身側(cè)響起婆硬,我...
    開(kāi)封第一講書(shū)人閱讀 39,319評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎奸例,沒(méi)想到半個(gè)月后彬犯,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,775評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡查吊,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年谐区,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片逻卖。...
    茶點(diǎn)故事閱讀 40,096評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡卢佣,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出箭阶,到底是詐尸還是另有隱情虚茶,我是刑警寧澤戈鲁,帶...
    沈念sama閱讀 35,789評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站嘹叫,受9級(jí)特大地震影響婆殿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜罩扇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,437評(píng)論 3 331
  • 文/蒙蒙 一婆芦、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧喂饥,春花似錦消约、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,993評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至捞高,卻和暖如春氯材,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背硝岗。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,107評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工氢哮, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人型檀。 一個(gè)月前我還...
    沈念sama閱讀 48,308評(píng)論 3 372
  • 正文 我出身青樓冗尤,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親胀溺。 傳聞我的和親對(duì)象是個(gè)殘疾皇子生闲,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,037評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容