12.11 C++程序設計教程 面向對象程序設計

C++與C不同,它是一個面向對象的編程語言。是一種靜態類型的、編譯式的、通用的、大小寫敏感的、不規則的編程語言,支持過程化編程、面向對象編程和泛型編程。C++ 被認為是一種中級語言,它綜合了高級語言和低級語言的特點。

C++程序設計教程 面向對象程序設計


C++ 完全支持面向對象的程序設計,包括面向對象開發的四大特性:

封裝

抽象

繼承

多態

標準的 C++ 由三個重要部分組成:

核心語言,提供了所有構件塊,包括變量、數據類型和常量,等等。

C++ 標準庫,提供了大量的函數,用於操作文件、字符串等。

標準模板庫(STL),提供了大量的方法,用於操作數據結構等。

C++ 程序可以定義為對象的集合,這些對象通過調用彼此的方法進行交互。現在讓我們簡要地看一下什麼是類、對象,方法、即時變量。

對象 - 對象具有狀態和行為。例如:一隻狗的狀態 - 顏色、名稱、品種,行為 - 搖動、叫喚、吃。對象是類的實例。

類 - 類可以定義為描述對象行為/狀態的模板/藍圖。

方法 - 從基本上說,一個方法表示一種行為。一個類可以包含多個方法。可以在方法中寫入邏輯、操作數據以及執行所有的動作。

即時變量 - 每個對象都有其獨特的即時變量。對象的狀態是由這些即時變量的值創建的。

數據類型

基本的內置類型

C++ 為程序員提供了種類豐富的內置數據類型和用戶自定義的數據類型。下表列出了七種基本的 C++ 數據類型:

類型關鍵字

布爾型bool

字符型char

整型int

浮點型float

雙浮點型double

無類型void

寬字符型

wchar_t

其實 wchar_t 是這樣來的:

typedef wchar_t short int;

所以 wchar_t 實際上的空間是和 short int 一樣。

一些基本類型可以使用一個或多個類型修飾符進行修飾:

signed

unsigned

short

long

下表顯示了各種變量類型在內存中存儲值時需要佔用的內存,以及該類型的變量所能存儲的最大值和最小值。

注意:不同系統會有所差異。

類型位範圍

char1 個字節-128 到 127 或者 0 到 255

unsigned char1 個字節0 到 255

signed char1 個字節-128 到 127

int4 個字節-2147483648 到 2147483647

unsigned int4 個字節0 到 4294967295

signed int4 個字節-2147483648 到 2147483647

short int2 個字節-32768 到 32767

unsigned short int2 個字節0 到 65,535

signed short int2 個字節-32768 到 32767

long int8 個字節-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807

signed long int8 個字節-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807

unsigned long int8 個字節0 to 18,446,744,073,709,551,615

float4 個字節+/- 3.4e +/- 38 (~7 個數字)

double8 個字節+/- 1.7e +/- 308 (~15 個數字)

long double16 個字節+/- 1.7e +/- 308 (~15 個數字)

wchar_t2 或 4 個字節1 個寬字符

從上表可得知,變量的大小會根據編譯器和所使用的電腦而有所不同。

下面實例會輸出您電腦上各種數據類型的大小。

實例

#include<iostream> /<iostream>

#include<string> /<string>

#include <limits> /<limits>

using namespace std;

int main()

{

cout << "type: \\t\\t" << "************size**************"<< endl;

cout << "bool: \\t\\t" << "所佔字節數:" << sizeof(bool);

cout << "\\t最大值:" << (numeric_limits<bool>::max)(); /<bool>

cout << "\\t\\t最小值:" << (numeric_limits<bool>::min)() << endl; /<bool>

cout << "char: \\t\\t" << "所佔字節數:" << sizeof(char);

cout << "\\t最大值:" << (numeric_limits<char>::max)(); /<char>

cout << "\\t\\t最小值:" << (numeric_limits<char>::min)() << endl; /<char>

cout << "signed char: \\t" << "所佔字節數:" << sizeof(signed char);

cout << "\\t最大值:" << (numeric_limits<signed>::max)(); /<signed>

cout << "\\t\\t最小值:" << (numeric_limits<signed>::min)() << endl; /<signed>

cout << "unsigned char: \\t" << "所佔字節數:" << sizeof(unsigned char);

cout << "\\t最大值:" << (numeric_limits<unsigned>::max)(); /<unsigned>

cout << "\\t\\t最小值:" << (numeric_limits<unsigned>::min)() << endl; /<unsigned>

cout << "wchar_t: \\t" << "所佔字節數:" << sizeof(wchar_t);

cout << "\\t最大值:" << (numeric_limits<wchar>::max)(); /<wchar>

cout << "\\t\\t最小值:" << (numeric_limits<wchar>::min)() << endl; /<wchar>

cout << "short: \\t\\t" << "所佔字節數:" << sizeof(short);

cout << "\\t最大值:" << (numeric_limits<short>::max)(); /<short>

cout << "\\t\\t最小值:" << (numeric_limits<short>::min)() << endl; /<short>

cout << "int: \\t\\t" << "所佔字節數:" << sizeof(int);

cout << "\\t最大值:" << (numeric_limits::max)();

cout << "\\t最小值:" << (numeric_limits

::min)() << endl;

cout << "unsigned: \\t" << "所佔字節數:" << sizeof(unsigned);

cout << "\\t最大值:" << (numeric_limits<unsigned>::max)(); /<unsigned>

cout << "\\t最小值:" << (numeric_limits<unsigned>::min)() << endl; /<unsigned>

cout << "long: \\t\\t" << "所佔字節數:" << sizeof(long);

cout << "\\t最大值:" << (numeric_limits<long>::max)(); /<long>

cout << "\\t最小值:" << (numeric_limits<long>::min)() << endl; /<long>

cout << "unsigned long: \\t" << "所佔字節數:" << sizeof(unsigned long);

cout << "\\t最大值:" << (numeric_limits<unsigned>::max)(); /<unsigned>

cout << "\\t最小值:" << (numeric_limits<unsigned>::min)() << endl; /<unsigned>

cout << "double: \\t" << "所佔字節數:" << sizeof(double);

cout << "\\t最大值:" << (numeric_limits<double>::max)(); /<double>

cout << "\\t最小值:" << (numeric_limits<double>::min)() << endl; /<double>

cout << "long double: \\t" << "所佔字節數:" << sizeof(long double);

cout << "\\t最大值:" << (numeric_limits<long>::max)(); /<long>

cout << "\\t最小值:" << (numeric_limits<long>::min)() << endl; /<long>

cout << "float: \\t\\t" << "所佔字節數:" << sizeof(float);

cout << "\\t最大值:" << (numeric_limits<float>::max)(); /<float>

cout << "\\t最小值:" << (numeric_limits<float>::min)() << endl; /<float>

cout << "size_t: \\t" << "所佔字節數:" << sizeof(size_t);

cout << "\\t最大值:" << (numeric_limits<size>::max)(); /<size>

cout << "\\t最小值:" << (numeric_limits<size>::min)() << endl; /<size>

cout << "string: \\t" << "所佔字節數:" << sizeof(string) << endl;

// << "\\t最大值:" << (numeric_limits"


分享到:


相關文章: