C++ 資料型別



在任何語言中編寫程式時,您都需要使用各種變數來儲存各種資訊。變數只不過是保留的記憶體位置以儲存值。這意味著當您建立變數時,您會在記憶體中保留一些空間。

您可能希望儲存各種資料型別的資訊,例如字元、寬字元、整數、浮點數、雙精度浮點數、布林值等。根據變數的資料型別,作業系統分配記憶體並決定在保留的記憶體中可以儲存什麼。

原始內建型別

C++ 為程式設計師提供了豐富的內建和使用者定義資料型別。下表列出了七種基本的 C++ 資料型別:

型別 關鍵字
布林型 bool
字元型 char
整型 int
浮點型 float
雙精度浮點型 double
無值 void
寬字元型 wchar_t

可以使用以下一個或多個型別修飾符修改幾種基本型別:

  • 帶符號
  • 無符號
  • 短整型
  • 長整型

下表顯示了變數型別、儲存值所需的記憶體量以及此類變數中可以儲存的最大值和最小值。

型別 典型位寬 典型範圍
char 1 位元組 -127 到 127 或 0 到 255
無符號字元型 1 位元組 0 到 255
帶符號字元型 1 位元組 -127 到 127
int 4 位元組 -2147483648 到 2147483647
無符號整型 4 位元組 0 到 4294967295
帶符號整型 4 位元組 -2147483648 到 2147483647
短整型 2 位元組 -32768 到 32767
無符號短整型 2 位元組 0 到 65,535
帶符號短整型 2 位元組 -32768 到 32767
長整型 8 位元組 -9223372036854775808 到 9223372036854775807
帶符號長整型 8 位元組 與長整型相同
無符號長整型 8 位元組 0 到 18446744073709551615
長長整型 8 位元組 -(2^63) 到 (2^63)-1
無符號長長整型 8 位元組 0 到 18,446,744,073,709,551,615
float 4 位元組
double 8 位元組
長雙精度浮點型 12 位元組
wchar_t 2 或 4 位元組 1 寬字元

變數的大小可能與上表中顯示的不同,具體取決於您使用的編譯器和計算機。

示例

以下示例將生成您計算機上各種資料型別的正確大小。

#include <iostream>
using namespace std;

int main() {
   cout << "Size of char : " << sizeof(char) << endl;
   cout << "Size of int : " << sizeof(int) << endl;
   cout << "Size of short int : " << sizeof(short int) << endl;
   cout << "Size of long int : " << sizeof(long int) << endl;
   cout << "Size of float : " << sizeof(float) << endl;
   cout << "Size of double : " << sizeof(double) << endl;
   cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
   
   return 0;
}

此示例使用endl,它在每一行之後插入一個換行符,並使用<<運算子將多個值輸出到螢幕。我們還使用sizeof()運算子獲取各種資料型別的大小。

當以上程式碼編譯並執行時,會產生以下結果,該結果可能因機器而異:

Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4

示例

以下是另一個示例

#include <iostream>
#include <limits>
using namespace std;

int main() {

    std::cout << "Int Min " << std::numeric_limits<int>::min() << endl;
    std::cout << "Int Max " << std::numeric_limits<int>::max() << endl;
    std::cout << "Unsigned Int  Min " << std::numeric_limits<unsigned int>::min() << endl;
    std::cout << "Unsigned Int Max " << std::numeric_limits<unsigned int>::max() << endl;
    std::cout << "Long Int Min " << std::numeric_limits<long int>::min() << endl;
    std::cout << "Long Int Max " << std::numeric_limits<long int>::max() << endl;

    std::cout << "Unsigned Long Int Min " << std::numeric_limits<unsigned  long int>::min() <<endl;
    std::cout << "Unsigned Long Int Max " << std::numeric_limits<unsigned  long int>::max() << endl;

}

派生資料型別

從 C++ 中預定義的資料型別獲得的資料型別稱為派生資料型別。這些可以分為四類,即:

1. 函式

一個函式是最簡單的使用者定義資料型別形式。它包括一個返回型別、一個函式名和輸入引數。

語法

return_type function_name(input_param1, input_param2…){
   <function_body>
}

示例

#include <iostream>
using namespace std;

string func(int n){
   //returns if n is odd or even
   if(n%2) return "Given number is Odd !";
   else return "Given number is Even !";
}
int main(){
   int a;
   //enter a number
   cin>>a;
   cout<<func(a);
   //a simple function to check if
   //number is odd or even
   return 0;
}
輸出
Given number is Even !

2. 陣列

一個陣列是一系列相同資料型別的元素。陣列的元素儲存在儲存器中的連續記憶體位置。

語法

data_type array_name[array_size];

示例

#include <iostream>
using namespace std;

int main(){
   int arr[5]={1,2,3,2,1};
   //define an integer array of size 5

   for(auto it:arr)
      cout<<it<<" ";
   //print the elements of array

   return 0;
}
輸出
1 2 3 2 1 

3. 指標

一個指標是對先前定義的元素的引用。指標的值返回與其關聯的元素的地址位置。

語法

data_type * pointer_name=& variable_name;

示例

#include <iostream>
using namespace std;

int main() {
   int a=20;
   //declare variable a
   int *p= &a;
   //assign pointer to a
   cout<<"Address of variable a: "<<p<<endl;
   cout<<"Value of variable a: "<<*p<<endl;

   return 0;
}
輸出
Address of variable a: 0x7ffc49a8637c
Value of variable a: 20

4. 引用

一個引用變數用於建立具有相同引用的變數的副本。因此,對引用變數所做的更改也會反映在原始變數上。

語法

data_type & reference_name= variable_name;

示例

#include <iostream>
using namespace std;

int main(){
   int c=11;
   int& refer=c;

   cout<<"Initially value of integer is: "<<c<<endl;

   refer=121;
   cout<<"After changing value using refer variable :"<<c<<endl;

   return 0;
}
輸出
Initially value of integer is: 11
After changing value using refer variable :121

使用者定義的資料型別

使用者直觀地定義而不使用任何預定義資料型別的資料型別稱為使用者定義的資料型別。這些資料型別可以進一步分為五種型別,即:

1. 類

一個在面向物件程式設計中被定義為一種自定義資料型別,用於構造物件。它是物件的框架,可以包含建構函式、方法和麵向物件的概念,如多型、繼承等。

語法

class Class_name{
   <class body>

   class_name(parameters) {
      <constructor body>
   }

   return_type method_name(paremeters){
      <method body>
   }

}

示例

#include <iostream>
using namespace std;

class TP{
   public:
      string tp;
    
      void print(){
         cout<<tp<<endl;
      }
};
int main(){
   TP object;
   object.tp="I Love Tutorialspoint !!!";
   object.print();

   return 0;
}
輸出
I Love Tutorialspoint !!!

2. 結構體 (struct)

結構體資料型別中,使用者可以在結構體主體內部引入多個基本資料型別。

語法

struct struct_name{
   data_type1 var_name1;
   data_type2 var_name2;
   …
}

示例

#include <iostream>
using namespace std;

struct TP{
   string tp;
   int grade;
};
int main(){
   TP object;
   object.tp="I Love Tutorialspoint !!!";
   object.grade=10;

   cout<<object.tp<<endl;
   cout<<"How much would you rate it?"<<" : "<< object.grade;

   return 0;
}
輸出
I Love Tutorialspoint !!!
How much would you rate it? : 10

3. 聯合體

聯合體類似於結構體。在此,所有變數的記憶體位置相同,並且所有變數共享相同的引用。因此,一個值的更改會導致所有其他值發生更改。

語法

union union_name{
   data_type var_name1;
   data_type var_name2;
};

示例

#include <iostream>
using namespace std;

union TP{
   int tp1,tp2;
};
int main(){
   union TP t;
   t.tp1=2;
   cout<<"Value of tp1 initially: "<<t.tp1<<endl;

   t.tp2=4;
   cout<<"When we change tp2, value of tp1 is : "<<t.tp1<<endl;

   return 0;
}
輸出
Value of tp1 initially: 2
When we change tp2, value of tp1 is : 4

4. 列舉 (Enum)

列舉或簡稱 enum 是一種使用者定義的資料型別,用於在程式中為整數常量命名。這提高了程式的使用者可讀性。

語法

enum enum_name{
   var¬_name1 , var_name2, …
}

示例

#include <iostream>
using namespace std;

enum TP{ C, Java, Python, Ruby, Kotlin, Javascript, TypeScript, Others};

int main(){
   enum TP course;
   cout<<"Which course do you love the most?"<<endl;

   course=Kotlin;
   cout<<"I love the "<<course+1<<"th course !!!";

   return 0;
}

輸出

Which course do you love the most?
I love the 5th course !!!

typedef 宣告

您可以使用typedef為現有型別建立新名稱。以下是使用 typedef 定義新型別的簡單語法:

typedef type newname; 

例如,以下程式碼告訴編譯器 feet 是 int 的另一個名稱:

typedef int feet;

現在,以下宣告是完全合法的,並建立一個名為 distance 的整型變數:

feet distance;

列舉型別

列舉型別宣告一個可選的型別名稱和一組零個或多個識別符號,這些識別符號可用作該型別的值。每個列舉器都是一個常量,其型別為列舉。

建立列舉需要使用關鍵字enum。列舉型別的通用形式為:

enum enum-name { list of names } var-list; 

這裡,enum-name 是列舉的型別名稱。名稱列表以逗號分隔。

例如,以下程式碼定義了一個名為 colors 的顏色列舉和一個型別為 color 的變數 c。最後,c 被賦值為“blue”。

enum color { red, green, blue } c;
c = blue;

預設情況下,第一個名稱的值為 0,第二個名稱的值為 1,第三個名稱的值為 2,依此類推。但是,您可以透過新增初始化程式為名稱指定特定值。例如,在以下列舉中,green的值將為 5。

enum color { red, green = 5, blue };

這裡,blue的值將為 6,因為每個名稱都比其前面的名稱大 1。

廣告