C++ 基本语法
C++ 程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互。现在让我们简要地看一下什么是类、对象,方法、即时变量。
- 对象 - 对象具有状态和行为。例如:一只狗的状态 - 颜色、名称、品种,行为 - 摇动、叫唤、吃。对象是类的实例。
- 类 - 类可以定义为描述对象行为/状态的模板/蓝图。
- 方法 - 从基本上说,一个方法表示一种行为。一个类可以包含多个方法。可以在方法中写入逻辑、操作数据以及执行所有的动作。
- 即时变量 - 每个对象都有其独特的即时变量。对象的状态是由这些即时变量的值创建的。
C++ 标识符
C++ 标识符是用来标识变量、函数、类、模块,或任何其他用户自定义项目的名称。一个标识符以字母 A-Z 或 a-z 或下划线 _ 开始,后跟零个或多个字母、下划线和数字(0-9)。
C++ 标识符内不允许出现标点字符,比如 @、& 和 %。C++ 是区分大小写的编程语言。因此,在 C++ 中,Manpower 和 manpower 是两个不同的标识符。
C++ 关键字
下表列出了 C++ 中的保留字。这些保留字不能作为常量名、变量名或其他标识符名称。
asm |
else |
new |
this |
auto |
enum |
operator |
throw |
bool |
explicit |
private |
true |
break |
export |
protected |
try |
case |
extern |
public |
typedef |
catch |
false |
register |
typeid |
char |
float |
reinterpret_cast |
typename |
class |
for |
return |
union |
const |
friend |
short |
unsigned |
const_cast |
goto |
signed |
using |
continue |
if |
sizeof |
virtual |
default |
inline |
static |
void |
delete |
int |
static_cast |
volatile |
do |
long |
struct |
wchar_t |
double |
mutable |
switch |
while |
dynamic_cast |
namespace |
template |
C++ 注释
程序的注释是解释性语句,您可以在 C++ 代码中包含注释,这将提高源代码的可读性。所有的编程语言都允许某种形式的注释。
C++ 支持单行注释和多行注释。注释中的所有字符会被 C++ 编译器忽略。
C++ 注释一般有两种:
- //
- 一般用于单行注释。
- /
* ... */ - 一般用于多行注释。
C++ 数据类型
使用编程语言进行编程时,需要用到各种变量来存储各种信息。变量保留的是它所存储的值的内存位置。这意味着,当您创建一个变量时,就会在内存中保留一些空间。
您可能需要存储各种数据类型(比如字符型、宽字符型、整型、浮点型、双浮点型、布尔型等)的信息,操作系统会根据变量的数据类型,来分配内存和决定在保留内存中存储什么。
基本的内置类型
C++ 为程序员提供了种类丰富的内置数据类型和用户自定义的数据类型。下表列出了七种基本的 C++ 数据类型:
类型 |
关键字 |
布尔型 |
bool |
字符型 |
char |
整型 |
int |
浮点型 |
float |
双浮点型 |
double |
无类型 |
void |
宽字符型 |
wchar_t |
其实 wchar_t 是这样来的:
typedef short int wchar_t;
所以 wchar_t 实际上的空间是和 short int 一样。
一些基本类型可以使用一个或多个类型修饰符进行修饰:
- signed
- unsigned
- short
- long
下表显示了各种变量类型在内存中存储值时需要占用的内存,以及该类型的变量所能存储的最大值和最小值。
注意:不同系统会有所差异,一字节为 8 位。
注意:默认情况下,int、short、long都是带符号的,即 signed。
注意:long int 8 个字节,int 都是 4 个字节,早期的 C 编译器定义了 long int 占用 4 个字节,int 占用 2 个字节,新版的 C/C++ 标准兼容了早期的这一设定。
类型 |
位 |
范围 |
char |
1 个字节 |
-128 到 127 或者 0 到 255 |
unsigned char |
1 个字节 |
0 到 255 |
signed char |
1 个字节 |
-128 到 127 |
int |
4 个字节 |
-2147483648 到 2147483647 |
unsigned int |
4 个字节 |
0 到 4294967295 |
signed int |
4 个字节 |
-2147483648 到 2147483647 |
short int |
2 个字节 |
-32768 到 32767 |
unsigned short int |
2 个字节 |
0 到 65,535 |
signed short int |
2 个字节 |
-32768 到 32767 |
long int |
8 个字节 |
-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
signed long int |
8 个字节 |
-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
unsigned long int |
8 个字节 |
0 到 18,446,744,073,709,551,615 |
float |
4 个字节 |
精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字) |
double |
8 个字节 |
双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字) |
long double |
16 个字节 |
长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。 |
wchar_t |
2 或 4 个字节 |
1 个宽字符 |
从上表可得知,变量的大小会根据编译器和所使用的电脑而有所不同。
下面实例会输出您电脑上各种数据类型的大小。
#includeiostream>
#include limits>
using namespace std;
int main()
{
cout "type: \t\t" "************size**************"endl;
cout "bool: \t\t" "所占字节数:" sizeof(bool);
cout "\t最大值:" (numeric_limitsbool>::max)();
cout "\t\t最小值:" (numeric_limitsbool>::min)() endl;
cout "char: \t\t" "所占字节数:" sizeof(char);
cout "\t最大值:" (numeric_limitschar>::max)();
cout "\t\t最小值:" (numeric_limitschar>::min)() endl;
cout "signed char: \t" "所占字节数:" sizeof(signed char);
cout "\t最大值:" (numeric_limitssigned char>::max)();
cout "\t\t最小值:" (numeric_limitssigned char>::min)() endl;
cout "unsigned char: \t" "所占字节数:" sizeof(unsigned char);
cout "\t最大值:" (numeric_limitsunsigned char>::max)();
cout "\t\t最小值:" (numeric_limitsunsigned char>::min)() endl;
cout "wchar_t: \t" "所占字节数:" sizeof(wchar_t);
cout "\t最大值:" (numeric_limitswchar_t>::max)();
cout "\t\t最小值:" (numeric_limitswchar_t>::min)() endl;
cout "short: \t\t" "所占字节数:" sizeof(short);
cout "\t最大值:" (numeric_limitsshort>::max)();
cout "\t\t最小值:" (numeric_limitsshort>::min)() endl;
cout "int: \t\t" "所占字节数:" sizeof(int);
cout "\t最大值:" (numeric_limitsint>::max)();
cout "\t最小值:" (numeric_limitsint>::min)() endl;
cout "unsigned: \t" "所占字节数:" sizeof(unsigned);
cout "\t最大值:" (numeric_limitsunsigned>::max)();
cout "\t最小值:" (numeric_limitsunsigned>::min)() endl;
cout "long: \t\t" "所占字节数:" sizeof(long);
cout "\t最大值:" (numeric_limitslong>::max)();
cout "\t最小值:" (numeric_limitslong>::min)() endl;
cout "unsigned long: \t" "所占字节数:" sizeof(unsigned long);
cout "\t最大值:" (numeric_limitsunsigned long>::max)();
cout "\t最小值:" (numeric_limitsunsigned long>::min)() endl;
cout "double: \t" "所占字节数:" sizeof(double);
cout "\t最大值:" (numeric_limitsdouble>::max)();
cout "\t最小值:" (numeric_limitsdouble>::min)() endl;
cout "long double: \t" "所占字节数:" sizeof(long double);
cout "\t最大值:" (numeric_limitslong double>::max)();
cout "\t最小值:" (numeric_limitslong double>::min)() endl;
cout "float: \t\t" "所占字节数:" sizeof(float);
cout "\t最大值:" (numeric_limitsfloat>::max)();
cout "\t最小值:" (numeric_limitsfloat>::min)() endl;
cout "size_t: \t" "所占字节数:" sizeof(size_t);
cout "\t最大值:" (numeric_limitssize_t>::max)();
cout "\t最小值:" (numeric_limitssize_t>::min)() endl;
cout "string: \t" "所占字节数:" sizeof(string) endl;
// ::max)() ::min)()
cout "type: \t\t" "************size**************"endl;
return 0;
}
本实例使用了 endl,这将在每一行后插入一个换行符, 运算符用于向屏幕传多个值,sizeof() 运算符用来获取各种数据类型的大小。