C++與C的主要差異

簡單性和與C的高度兼容性是C++語言的重要設計原則,它繼續維持C作爲其一個子集,在C++語言中定義新的類型是其最基本的程序設計活動,並且極力避免了那些即使不用也會帶來時間或空間額外開銷的特徵,支持利用傳統的編譯和運行時環境,具有類型檢查和數據隱藏特徵。C++特別強調程序的結構,強調模塊化、強類型接口和靈活性,支持大型程序的開發。

任何一種程序設計語言都要服務於兩個目的,一是提供一種需要描述所執行動作的載體,而是爲程序員提供描述問題的概念。前者要求這個程序設計語言儘可能接近機器,後者要求該程序設計語言儘可能接近要解決的問題。與C語言的兼容性保證了前者的要求,類的引入使C++語言達到後者的要求。

C++C的主要差別

C++C的差異的根源主要來源於C++提供的類型檢查和支持面向對象和模板等程序設計模式的需要,差異主要有如下幾點:

  • C中字符常量和枚舉符的大小都是sizeof(int),而在C++裏,對一個字符常量使用sizeof的結果等於sizeof(char),而且sizeof應用於枚舉符的大小會根據隨着枚舉類型的不同而不同,因爲C++實現會爲枚舉選擇合適的類型,在g++實現中每個枚舉都佔sizeof(int)個字節,沒有根據其值範圍選擇不同的int類型。
/***************************************
 * sizeof_char_enum.c                  *
 *                                     *
 * C語言中的字符常量和枚舉符大小是     *
 * sizeof(int)                         *
 ***************************************/

#include <stdio.h>

enum EnumType
{
  Test1,
  Test2
};

int main()
{
  printf("int的字節數爲%u\n", (unsigned)sizeof(int));
  printf("字符常量的字節數爲%u\n", (unsigned)sizeof('c'));
  printf("枚舉類型的字節數爲%u\n", (unsigned)sizeof(enum EnumType));

  return 0;
}
/***************************************
 * sizeof_char_enum.c.pp               *
 *                                     *
 * C++語言中的字符常量是sizeof(int),枚*
 * 舉的字節數根據實際情況而定          *
 *                                     *
 ***************************************/

#include <iostream>

enum EnumType
{
  Test1 = 0,
  Test2 = 1
};

enum EnumType2
{
  Test12,
  Test22 = 512
};

int main()
{
  std::cout<<"int的字節數爲"<<sizeof(int)<<"\n";
  std::cout<<"short int的字節數爲"<<sizeof(short)<<"\n";
  std::cout<<"字符常量的字節數爲"<<sizeof('c')<<"\n";
  std::cout<<"枚舉類型EnumType的字節數爲"<<sizeof(EnumType)<<"\n";
  std::cout<<"枚舉類型EnumType2的字節數爲"<<sizeof(EnumType2)<<"\n";

  return 0;
}

枚舉和字符常量字節

  • C++提供了單行註釋//C89標準中沒有,C99標準添加了單行註釋,但許多C實現都支持了單行註釋,例如gcc
/*************************************
 * single_annotation.c               *
 *                                   *
 * C語言中的單行註釋//               *
 ************************************/

#include <stdio.h>

int main()
{
  /*單行註釋*/
  int a = 8;

  //單行註釋
  printf("a = %d\n", a);

  return 0;
}

單行註釋

  • C++程序中在內層作用域中聲明的結構名稱將屏蔽外層作用域的對象、函數、枚舉或者類型的名稱。
/***************************************
 * inner_structure.c                   *
 *                                     *
 * 內層作用域中的結構體                *
 ***************************************/

#include <stdio.h>

int x[99];

int main()
{
  struct x {int a;};
  printf("x的大小爲%u\n", (unsigned)sizeof(x));
  return 0;
}
/***************************************
 * inner_structure.cpp                   *
 *                                     *
 * 內層作用域中的結構體                *
 ***************************************/

#include <iostream>

int x[99];

int main()
{
  struct x {int a;};
  std::cout<<"x的大小爲"<< (unsigned)sizeof(x)<<"\n";
  return 0;
}

內部定義結構體
- C中運行在沒有預先聲明的情況下調用大部分函數(這會出現問題,產生莫名其妙的錯誤,難以調試,使用函數原型),而C++不允許。
- C中可以用整數爲枚舉類型賦值,而C++不允許。
- C++提供了比C更多的關鍵字,如表1所示。C中,C++的某些關鍵字被作爲宏定義在頭文件裏
- C++中,同一個作用域內,不能有一個類名和typedef定義的類名相同。
- C中任意指針類型的變量可以自動轉換爲void *C++不能那麼做。C允許跳過void *類型的初始化,而C++不允許。

/**************************************
 * void_pointer.c                     *
 *                                    *
 * C語言中的void型指針                *
 **************************************/

#include <stdio.h>

int main()
{
  int a = 10;
  void *p = &a;
  int *p1 = p;
  printf("*p1 = %d\n", *p1);

  return 0;
}
/**************************************
 * void_pointer.cpp                   *
 *                                    *
 * C++語言中的void型指針              *
 **************************************/

#include <iostream>

int main()
{
  int a = 10;
  void *p = &a;
  int *p1 = p;
  std::cout<<"*p1 = "<< *p1<<std::endl;
  return 0;
}

void指針
- C中嵌套結構的名字和嵌套於其中的結構在同一個作用域。

/**************************************
 * struct_closure.c                   *
 *                                    *
 * C語言中嵌套的作用域定義            *
 **************************************/

#include <stdio.h>

int main()
{
  struct S
  {
    struct T{int x,y;} a;
  };

  struct T t = {10,20};

  printf("t.x = %d, t.y = %d\n", t.x, t.y);

  return 0;
}
/**************************************
 * struct_closure.cpp                 *
 *                                    *
 * C語言中嵌套的作用域定義            *
 **************************************/

#include <iostream>

int main()
{
  struct S
  {
    struct T{int x,y;} a;
  };

  struct T t = {10,20};

  std::cout<<"t.x = "<<t.x<<","<<"t.y = "<<t.y<<std::endl;
  return 0;
}

struct作用域
- C使用static指明函數或對象是某一個文件的局部的東西,C++不使用static,而是用匿名名字空間替代。
- C隱式地將字符串文字量轉換爲char *,C++將字符串常量類型設置爲const char*,不允許隱式轉換。
- C++支持使用static_castreinterpret_castconst_cast取代C風格的類型轉換。
- C++支持對static存儲中對象的非常量初始化
-C++支持在for語句初始化表達式和條件中的聲明,C99標準也支持
-C+支持結構名前無需前綴struct
- C++支持類型安全的鏈接
- C++支持用newdelete管理自由存儲
- C++支持布爾類型bool
- C++支持函數重載
- C++將變量聲明作爲一個語句
- C++支持爲函數提供默認參數
- C++支持類、成員函數、成員類、構造和析構函數、派生類、虛函數和抽象類、共用/保護/私有訪問控制、友元、執行成員的指針、static成員、mutable成員、運算符重載和引用等用以用戶定義類型
-C++使用模板、內聯函數、默認參數、函數重載、名字空間、顯示作用域限定、異常處理和運行時類型識別等組織程序。

表1 C++新增的關鍵字

and and_eq asm bitand bitor bool
catch class compl const_cast delete dynamic_cast
explicit export false friend inline mutable
namespace new not not_eq operator or
or_eq private protected public reinterpret_cast static_cast
template this throw true try typeid
typename using virtual wchar_t xor xor_eq

_cplusplus可以用以確定是C還是C++編譯器在處理當前程序。

參考文獻

  1. Bjarne Stroustrup著,裘宗燕譯. C++程序設計語言(特別版).機械工業出版社 2009.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章