C++教程(最全)
1 C++简介
1.1 起源
-贝尔实验室20世纪80年代(1979)
1.2 应⽤范围
⽂字处理程序以及电⼦表格
编译器
操作系统
⼤型游戏等
1.3 C++和C
C语⾔是结构化和模块化的语⾔,⾯向过程。
C++保留了C语⾔原有的所有优点,增加了⾯向对象的机制,俗称“带类的C",1983年更名为C++
2开发⼯具
记事本(Notepad++)+命令⾏
Visual C++ 6.0:经典开发⼯具,与流⾏操作系统有冲突
VS 2015等:功能强⼤,体积也强⼤
Code::Blocks:开源免费开发⼯具,专业开发⼈员推荐使⽤
其他开发⼯具:DeV C++、CLion、C-Free、Xcode、C4droid
3 基本语法
对象-对象具有状态的⾏为。对象是类的实例。
类-类可以定义为对象⾏为、状态的模版。
⽅法-从基本上讲,⼀个⽅法表⽰⼀种⾏为,⼀个类可以包含多种⽅法。
变量
3.1 注释
//单⾏注释
/*
多⾏注释
多⾏注释
*/
3.2关键字
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
3.3标识符
标识符是⽤来标识变量、函数、类、模块,或任何其他⽤户⾃定义项⽬的名称。⼀个标识符以字母 A-Z 或 a-z 或下划线 _ 开始,后跟零个或多个字母、下划线和数字(0-9)。
标识符内不允许出现标点字符,⽐如 @、& 和 %。C++ 是区分⼤⼩写的编程语⾔。
4 数据类型
4.1基本数据类型
七种基本的C++数据类型:bool、char、int、float、double、void、wchar_t
类型修饰符:signed、unsigned、short、long
注:⼀些基本类型可以使⽤⼀个或多个类型修饰符进⾏修饰,⽐如:signed short int简写为short、signed long int 简写为long。
类型名占⽤字节数
数值范围
void 0bool 1{true.false}
wchar_t 2或4个字节
char(signed char)1-128~+127short(signed short)2-32768~+32767
int(signed int)4-2147483648~+2147483647long(signed long)4-2147483648~+2147483647
long long(signed long long)
8-9,223,372,036,854,775,808 ~9,223,372,036,854,775,807
float 4-.34*1038double 8-1.7*10308
unsigned char 10~255unsigned shrot 20~65525unsigned(unsigned int)
40~4294967295unsigned long 40~4294967295
unsigned long long
8
0 ~ 18,446,744,073,709,551,615
/
/x64处理器 64位window10 vs2015 #include <iostream>using namespace std ;int main (){ bool b ;
char c ;short s ; int i ; long l ; long long ll ; float f ; double d ; long double ld ;long float lf ;
unsigned char uc ; unsigned short us ; unsigned int ui ; unsigned long ul ; unsigned long long ull ; cout << sizeof (bool ) << endl ;
cout << sizeof (char )<<" " << sizeof (short )<<" "<< sizeof (signed int ) << " " << sizeof (long ) << " " << sizeof (signed long long ) << " " << sizeof (float ) << " " << si zeof (double ) << " " << sizeof (long float ) << " " << sizeof (long double ) << endl ;
cout <<sizeof (unsigned char )<<" "<< sizeof (unsigned short ) << " " << sizeof (unsigned int ) << " " << sizeof (unsigned long ) << " " << sizeof (unsigned long lon g ) << endl ;
cout << sizeof (unsigned ) << endl ;
cout << "hello World" <<endl ; system ("pause"); return 0;
}
4.2 数据类型在不同系统中所占空间⼤⼩令数组全部的值为0
这个与机器、操作系统、编译器有关。⽐如同样是在32bits的操作系统系,VC++的编译器下int类型为占4个字节;⽽tuborC下则是2个字节。原因:
38~3.4*10308~1.7*10
c/c++规定int字长和机器字长相同
操作系统字长和机器字长未必⼀致
编译器根据操作系统字长来定义int字长
类型16位操作系统32位操作系统64位操作系统
char111
char*248
short222
int244
long448 long long888
注:long类型在不同编译器中的占位不⼀样: 32位时,VC++和GCC都是4字节; 64位时,VC++是4字节,GCC是8字节。
4.3 typedef声明
//使⽤typedef为⼀个已有的类型取⼀个新的名字,语法如下:
typedef type newname
//eg:
typedef int feet
feet distance
4.4 枚举类型
C++中的⼀种派⽣数据类型,它是由⽤户定义的若⼲枚举常量的集合;枚举元素是⼀个整型,枚举型可以隐式的转换为int型,int型不能隐式的转换为枚举型。
//枚举类型的语法:
enum枚举名{
标识符[=整型常数],
标识符[=整型常数],
...
标识符[=整型常数]
}枚举变量;
如果枚举没有初始化, 即省掉"=整型常数"时, 则从第⼀个标识符开始;
默认情况下,第⼀个名称的值为 0,第⼆个名称的值为 1,第三个名称的值为 2,以此类推。但是,您也可以给名称赋予⼀个特殊的值,只需要添加⼀个初始值即可。
例如:
enum course {math,chinese,english,physics,chemistry}c;
c = english;
cout<<c<<endl;//2
//english为1 physics为2 chemistry为3,chinese仍为1,math仍为0
enum course {math,chinese,english=1,physics,chemistry};
5 变量
变量其实只不过是程序可操作的存储区的名称。C++ 中每个变量都有指定的类型,类型决定了变量存储的⼤⼩和布局,该范围内的值都可以存储在内存中,运算符可应⽤于变量上。
5.1 变量的声明和定义
变量声明向编译器保证变量以给定的类型和名称存在,这样编译器在不需要知道变量完整细节的情况下也能继续进⼀步的编译。
可以在 C++ 程序中多次声明⼀个变量,但变量只能在某个⽂件、函数或代码块中被定义⼀次。
多个变量赋同⼀个值时,需要分别赋值。
int x = y = z =66;//错误
int x =3,y =3,z =3;
int x, y ,z =3;
x = y = z;
变量的声明(不分配内存):extern 数据类型变量名;
变量的定义:数据类型变量名1,变量名2,...变量名n;
// 变量声明
extern int a, b;
int main ()
{
// 变量定义
int a, b;
// 初始化
a =23;
b =25;
return0;
}
5.2 变量的作⽤域
局部变量:在函数或⼀个代码块内部声明的变量,称为局部变量。它们只能被函数内部或者代码块内部的语句使⽤。
全局变量:在所有函数外部定义的变量(通常是在程序的头部),称为全局变量。全局变量的值在程序的整个⽣命周期内都是有效的。
局部变量和全局变量的名称可以相同,但是在函数内,局部变量的值会覆盖全局变量的值。
当局部变量被定义时,系统不会对其初始化;定义全局变量时,系统会⾃动初始化值:int float double 0,char ’\0‘,指针 NULL
int i =66;
int main ()
{
int i =88;
cout << i<<endl;//8
return0;
}
float f;
double d;
char c;
int*p;
int main()
{
cout << i << f << d << c << p << endl;//000 00000000
return0
}
6 运算符
算术运算符:+ - * / % ++ --
关系运算符:== != < > >= <=
逻辑运算符:&& || !
位运算符:& | ^ ~ << >>
赋值运算符:= += -= *= /= %= <<= >>= &= ^= !=
杂项运算符:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论