C++基础(一)

0x01 基本语法

简介

对象:对象具有的状态与行为
类:描述对象状态、行为的模板
方法:一个方法标识一个行为,一个类可以包含多个方法,能够在方法中写入逻辑、操作数据及所有执行的动作
限时变量:每个对象都有其独有的即时变量,对象的状态是由这些即时变量创建的

基本语法

#include <iostream>
using namespace std;  //使用std命名空间
/* 和 */ 多行注释
// main() 是程序开始执行的地方
 
int main() //主函数
{  //语句块
   cout << "Hello World"; // 输出 Hello World
   return 0;  //终止madin函数并向调用进程返回值0
}


x = y; //分号为每行的结束
y = y+1;
add(x, y);
//等同于
x = y; y = y+1; add(x, y);

标识符

C++ 标识符是用来标识变量、函数、类、模块,或任何其他用户自定义项目的名称。一个标识符以字母 A-Z 或 a-z 或下划线 _ 开始,后跟零个或多个字母、下划线和数字(0-9)。

C++ 标识符内不允许出现标点字符,比如 @、& 和 %。C++ 是区分大小写的编程语言。因此,在 C++ 中,Manpower 和 manpower 是两个不同的标识符。

可用
mohd       zara    abc   move_name  a_123
myname50   _temp   j     a23b9      retVal

关键字

https://www.runoob.com/w3cnote/cpp-keyword-intro.html
不可做命名使用

数据类型

中文 英文
布尔型 bool
字符型 char
整型 int
浮点型 float
双浮点型 double
无类型 void
宽字符型 wchar_t
#include<iostream>  
#include <limits>

using namespace std;

int main()
{
    cout << "type: 		" << "************size**************" << endl;
    cout << "bool: 		" << "所占字节数:" << sizeof(bool);
    cout << "	最大值:" << (numeric_limits<bool>::max)();
    cout << "		最小值:" << (numeric_limits<bool>::min)() << endl;
    cout << "char: 		" << "所占字节数:" << sizeof(char);
    cout << "	最大值:" << (numeric_limits<char>::max)();
    cout << "		最小值:" << (numeric_limits<char>::min)() << endl;
    cout << "signed char: 	" << "所占字节数:" << sizeof(signed char);
    cout << "	最大值:" << (numeric_limits<signed char>::max)();
    cout << "		最小值:" << (numeric_limits<signed char>::min)() << endl;
    cout << "unsigned char: 	" << "所占字节数:" << sizeof(unsigned char);
    cout << "	最大值:" << (numeric_limits<unsigned char>::max)();
    cout << "		最小值:" << (numeric_limits<unsigned char>::min)() << endl;
    cout << "wchar_t: 	" << "所占字节数:" << sizeof(wchar_t);
    cout << "	最大值:" << (numeric_limits<wchar_t>::max)();
    cout << "		最小值:" << (numeric_limits<wchar_t>::min)() << endl;
    cout << "short: 		" << "所占字节数:" << sizeof(short);
    cout << "	最大值:" << (numeric_limits<short>::max)();
    cout << "		最小值:" << (numeric_limits<short>::min)() << endl;
    cout << "int: 		" << "所占字节数:" << sizeof(int);
    cout << "	最大值:" << (numeric_limits<int>::max)();
    cout << "	最小值:" << (numeric_limits<int>::min)() << endl;
    cout << "unsigned: 	" << "所占字节数:" << sizeof(unsigned);
    cout << "	最大值:" << (numeric_limits<unsigned>::max)();
    cout << "	最小值:" << (numeric_limits<unsigned>::min)() << endl;
    cout << "long: 		" << "所占字节数:" << sizeof(long);
    cout << "	最大值:" << (numeric_limits<long>::max)();
    cout << "	最小值:" << (numeric_limits<long>::min)() << endl;
    cout << "unsigned long: 	" << "所占字节数:" << sizeof(unsigned long);
    cout << "	最大值:" << (numeric_limits<unsigned long>::max)();
    cout << "	最小值:" << (numeric_limits<unsigned long>::min)() << endl;
    cout << "double: 	" << "所占字节数:" << sizeof(double);
    cout << "	最大值:" << (numeric_limits<double>::max)();
    cout << "	最小值:" << (numeric_limits<double>::min)() << endl;
    cout << "long double: 	" << "所占字节数:" << sizeof(long double);
    cout << "	最大值:" << (numeric_limits<long double>::max)();
    cout << "	最小值:" << (numeric_limits<long double>::min)() << endl;
    cout << "float: 		" << "所占字节数:" << sizeof(float);
    cout << "	最大值:" << (numeric_limits<float>::max)();
    cout << "	最小值:" << (numeric_limits<float>::min)() << endl;
    cout << "size_t: 	" << "所占字节数:" << sizeof(size_t);
    cout << "	最大值:" << (numeric_limits<size_t>::max)();
    cout << "	最小值:" << (numeric_limits<size_t>::min)() << endl;
    cout << "string: 	" << "所占字节数:" << sizeof(string) << endl;
    // << "	最大值:" << (numeric_limits<string>::max)() << "	最小值:" << (numeric_limits<string>::min)() << endl;  
    cout << "type: 		" << "************size**************" << endl;
    return 0;
}

变量类型

类型 描述
bool 存储值 true 或 false。
char 通常是一个八位字节(一个字节)。这是一个整数类型。
int 对机器而言,整数的最自然的大小。
float 单精度浮点值。
double 双精度浮点值。
void 表示类型的缺失。
wchar_t 宽字符类型。

赋值并输出

#include <iostream>
using namespace std;

// 变量声明
extern int a, b;
extern int c;
extern float f;

int main()
{
	// 变量定义
	int a, b;
	int c;
	float f;

	// 实际初始化
	a = 10;
	b = 20;
	c = a + b;

	cout << c << endl;
	cout << a << endl;
	f = 70.0 / 3.0;
	cout << f << endl;

	return 0;
}

0x02 变量与常量

作用域属于程序的一个区域,分三类
局部变量:函数或者代码块内部声明的变量
形式参数:函数参数的定义中声明的变量
全局变量:函数外部声明的变量

局部变量

#include <iostream>
using namespace std;

int main()
{
	int a = 10;
	int b = 20;
	int c = a + b;

	cout << c;
}

全局变量

#include <iostream>
using namespace std;
int d = 4;
int main()
{
	int a = 10;
	int b = 20;
	int c = a + b + d;

	cout << c;
}

局部全局变量名称可相同,但函数内局部会替代全局

#include <iostream>
using namespace std;
int a = 4;
int main()
{
	int a = 10;
	int b = 20;
	int c = a + b ;

	cout << c;
}

初始化

局部变量被定义时系统不会对其进行初始化,需人工自行进行初始化,相关初始化格式如下
数据类型 初始化默认值
int 0
char ''
float 0
double 0
pointer NULL

常量

常量是固定值,程序执行时候不会改变,又叫字面量
常量可以试任何的基本数据类型,可分为整形、浮点数字,字符。字符串,布尔值
常量是常规变量,不过常量的值在定义后不可以修改

整数常量

85         // 十进制
0213       // 八进制 
0x4b       // 十六进制 
30         // 整数 
30u        // 无符号整数 
30l        // 长整数 
30ul       // 无符号长整数

浮点常量

3.14159       // 合法的 
314159E-5L    // 合法的 
510E          // 非法的:不完整的指数
210f          // 非法的:没有小数或指数
.e55          // 非法的:缺少整数或分数

布尔常量

true 真
false 假

字符常量

转义序列 含义
字符
' ' 字符
" " 字符
? ? 字符
a 警报铃声
 退格键
f 换页符
换行符
回车
水平制表符
v 垂直制表符
ooo 一到三位的八进制数
xhh . . . 一个或多个数字的十六进制数

字符串常量

"hello, dear"
"hello, 
dear"
"hello, " "d" "ear"

定义常量

#define 预处理器

#include <iostream>
using namespace std;

#define changdu 12 //定义常量
int mianji;

int main()
{
	int kuandu = 1;
	mianji = changdu * kuandu;
	cout << mianji;
	return 0;
}

const 关键字

#include <iostream>
using namespace std;

#define changdu 12
int mianji;

int main()
{
	const int kuandu = 1; //const前缀进行声明
	mianji = changdu * kuandu;
	cout << mianji;
	return 0;
}

0x03 修饰符类型

C++ 允许在 char、int 和 double 数据类型前放置修饰符。修饰符用于改变基本类型的含义,所以它更能满足各种情境的需求。

数据类型的修饰符:
signed、ussigned、long、short

修饰符 signed、unsigned、long 和 short 可应用于整型,signed 和 unsigned 可应用于字符型,long 可应用于双精度型,修饰符 signed 和 unsigned 也可以作为 long 或 short 修饰符的前缀
#include <iostream>
using namespace std;
 
/* 
 * 这个程序演示了有符号整数和无符号整数之间的差别
*/
int main()
{
   short int i;           // 有符号短整数
   short unsigned int j;  // 无符号短整数

   j = 50000;

   i = j;
   cout << i << " " << j;

   return 0;
} 

//无符号短整数 50000 的位模式被解释为有符号短整数 -15,536

类型限定符

限定符 含义
const const 类型的对象在程序执行期间不能被修改改变。
volatile 修饰符 volatile 告诉编译器,变量的值可能以程序未明确指定的方式被改变。
restrict restrict 修饰的指针是唯一一种访问它所指向的对象的方式。只有 C99 增加了新的类型限定符 restrict。

0x04 存储类

存储类定义C++程序中的变量、函数的范围及可见性域生命周期,说明放在他们的锁修饰的类型之前

可用的存储类
auto,register,static,extern,mutable

auto

所有局部变量默认的存储类,auto只能用在函数内,即代表auto只能修饰局部变量
{
   int mount;
   auto int month;
}

register

用于定义存储在寄存器中而不是RAM中的局部变量,意味着变量的最大尺寸等于寄存器的大小,且不能对他应用的一元的&运算符,因为他没有内存位置
{
   register int  miles;
}
寄存器只用于需要快速访问的变量,比如计算器、定义register并不意味着变量将存储在寄存器中,他意味着可能存储在寄存器中,这取决于于硬件和现实的限制

static

指示编译器在程序的生命周期内保持局部变量的存在,不需要在每次它进入或离开作用域时进行创建和销毁,因此使用static修饰局部变量可以在函数调用之间保持局部变量的值,static也开业用于全局变量,当他修饰全局变量时,会使得变量的作用域限制在声明他的文件内
当static用于类数据成员上时,会导致仅有一个该成员的副本被类的所有对象共享

#include <iostream>
 
// 函数声明 
void func(void);
 
static int count = 10; /* 全局变量 */
 
int main()
{
    while(count--)
    {
       func();
    }
    return 0;
}
// 函数定义
void func( void )
{
    static int i = 5; // 局部静态变量
    i++;
    std::cout << "变量 i 为 " << i ;
    std::cout << " , 变量 count 为 " << count << std::endl;
}
/*
输出为
变量 i 为 6 , 变量 count 为 9
变量 i 为 7 , 变量 count 为 8
变量 i 为 8 , 变量 count 为 7
变量 i 为 9 , 变量 count 为 6
变量 i 为 10 , 变量 count 为 5
变量 i 为 11 , 变量 count 为 4
变量 i 为 12 , 变量 count 为 3
变量 i 为 13 , 变量 count 为 2
变量 i 为 14 , 变量 count 为 1
变量 i 为 15 , 变量 count 为 0
*/

extern

用于提供一个全局变量的引用,全局变量对所有的程序文件都是可见的。当您使用 'extern' 时,对于无法初始化的变量,会把变量名指向一个之前定义过的存储位置。

当您有多个文件且定义了一个可以在其他文件中使用的全局变量或函数时,可以在其他文件中使用 extern 来得到已定义的变量或函数的引用。可以这么理解,extern 是用来在另一个文件中声明一个全局变量或函数。

extern 修饰符通常用于当有两个或多个文件共享相同的全局变量或函数的时候


第一个文件:main.cpp
#include <iostream>
 
int count ;
extern void write_extern();
 
main()
{
   count = 5;
   write_extern();
}

第二个文件:support.cpp

#include <iostream>
 
extern int count;
 
void write_extern(void)
{
   std::cout << "Count is " << count << std::endl; 
} 

编译输出:
Count is 5

mutable

仅适用于类的对象,允许对象的成员替代常量。也就是说,mutable 成员可以通过 const 成员函数修改
原文地址:https://www.cnblogs.com/Yang34/p/14188271.html