笔记模式用于个人实验,不套用那么多客套话了,后期后整理成正式博客
运用软件:VS2017
系统:Win10
C++ 特性
封装、抽象、继承、多态
C++ 库
核心语言、C++标准库、STL库(标准模板库)
C++程序可以定义为对象的集合,这些对象通过调用彼此的方法进行交互。
对象—— 具有状态和行为。例如:一只狗的状态- 颜色、名称、品种、行为等,它(对象)是类的实例 要素
类—— 可以定义为描述对象行为/状态的模板/蓝图 框架
方法—— 从基本上说,一个方法表示一种行为。一个类可以包含多个方法。可以在方法中写入逻辑、操作数据以及执行所有的动作
即时变量 —— 每个对象都有其独特的即时变量。对象的状态是由这些即时变量的值创建的。
基础代码结构-Hello world
#include <iostream>
using namespace std; //告诉编译器使用 std 命名空间。
// main() 是程序开始执行的地方
int main()
{
cout << "Hello World ,"; cout << " I think C++ is good tool";
return 0;
}
C++的分号与语句块
C++中的分号(;)是语句结束符号,以实现逻辑实体的结束
C++同时不以行末作为结束符的表示,可写成连续分号形式
C++中的cout是打印字符,将反馈显示在命令窗口上
需要注意的是main 函数的返回值是返回给主调进程,使主调进程得知被调用程序的运行结果。标准规范中规定 main 函数的返回值为 int,一般约定返回 0 值时代表程序运行无错误,其它值均为错误号,但该约定并非强制,同时 g++, 则要求 main 函数的返回值必须为 int 型。如果程序的运行结果不需要返回给主调进程,或程序开发人员确认该状态并不重要,比如所有出错信息均在程序中有明确提示的情况下,可以不写 main 函数的返回值。
int main格式
int main()
{
.......
}
int void格式
int void()
{
.......
}
C++ 标识符
用来标识变量、函数、类、模块,或任何其他用户自定义项目的名称。一个标识符以字母 A-Z 或 a-z 或下划线 _ 开始,后跟零个或多个字母、下划线和数字(0-9)。C++ 标识符内不允许出现标点字符,比如 @、& 和 %;同时C++ 是区分大小写的编程语言。
同时C++有独立的关键字(保留字)在写标识符时,需要避开这些,否则程序无法正常运行
C++的关键字完整列单
C++中的空格
用于区分某个元素在哪里结束用的。例如 int age;
fruit=apples+oranges; 则可以通过添加空格改为 fruit = apples + oranges; 增加可读性
C++ 注释
/* 这是多行注释 */
/*
这也是多行注释
*/
//这是单行注释
实例:
/*
#include <iostream>
using namespace std; //告诉编译器使用 std 命名空间。
// main() 是程序开始执行的地方
int main()
{
cout << "Hello World ,"; cout << " I think C++ is good tool";
return 0;
}
*/
C++数据类型
当使用C++进行编程时,需要用到各种变量来储存各类信息,变量保留的是它所存储的值的内存位置。所以需要通过变量数据类型,来分配内存和决定在保留内存中存储什么。
若你有C基础,则可以发现还是那些数据类型
类型 | 关键字 | 描述 |
---|---|---|
布尔型 | bool | 存储值true或false |
字符型 | char | 一个字符8位 |
整型 | int | 整数是机器认识中最自然的大小 |
浮点型 | float | 单精度,1位符号,8位指数,23位小数 |
双浮点型 | double | 双精度,1位符号,11位指数,52位小数 |
无类型 | void | 表示类型的缺失 |
宽字符型 | wchar_t | 宽字符类型 |
同时可以用signed、unsigned、short、long等对上述类型进行修饰
#include<iostream>
#include<string>
#include <limits>
using namespace std;
int main()
{
cout << "type: \t\t" << "************size**************"<< endl;
cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
cout << "\t最大值:" << (numeric_limits<bool>::max)();
cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
cout << "char: \t\t" << "所占字节数:" << sizeof(char);
cout << "\t最大值:" << (numeric_limits<char>::max)();
cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;
cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);
cout << "\t最大值:" << (numeric_limits<signed char>::max)();
cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;
cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);
cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();
cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;
cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);
cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();
cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;
cout << "short: \t\t" << "所占字节数:" << sizeof(short);
cout << "\t最大值:" << (numeric_limits<short>::max)();
cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;
cout << "int: \t\t" << "所占字节数:" << sizeof(int);
cout << "\t最大值:" << (numeric_limits<int>::max)();
cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);
cout << "\t最大值:" << (numeric_limits<unsigned>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;
cout << "long: \t\t" << "所占字节数:" << sizeof(long);
cout << "\t最大值:" << (numeric_limits<long>::max)();
cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;
cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);
cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();
cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;
cout << "double: \t" << "所占字节数:" << sizeof(double);
cout << "\t最大值:" << (numeric_limits<double>::max)();
cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;
cout << "long double: \t" << "所占字节数:" << sizeof(long double);
cout << "\t最大值:" << (numeric_limits<long double>::max)();
cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;
cout << "float: \t\t" << "所占字节数:" << sizeof(float);
cout << "\t最大值:" << (numeric_limits<float>::max)();
cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
cout << "\t最大值:" << (numeric_limits<size_t>::max)();
cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
// << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;
cout << "type: \t\t" << "************size**************"<< endl;
return 0;
}
得到
bool: 所占字节数:1 最大值:1 最小值:0
char: 所占字节数:1 最大值: 最小值:€
signed char: 所占字节数:1 最大值: 最小值:€
unsigned char: 所占字节数:1 最大值: 最小值:
wchar_t: 所占字节数:2 最大值:65535 最小值:0
short: 所占字节数:2 最大值:32767 最小值:-32768
int: 所占字节数:4 最大值:2147483647 最小值:-2147483648
unsigned: 所占字节数:4 最大值:4294967295 最小值:0
long: 所占字节数:4 最大值:2147483647 最小值:-2147483648
unsigned long: 所占字节数:4 最大值:4294967295 最小值:0
double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308
long double: 所占字节数:8 最大值:1.79769e+308 最小值:2.22507e-308
float: 所占字节数:4 最大值:3.40282e+38 最小值:1.17549e-38
size_t: 所占字节数:8 最大值:18446744073709551615 最小值:0
string: 所占字节数:32
上述数字会根据所使用的计算机改变
typedef声明
可以通过typedef为一个已有的类型取一个新的名字
格式:
typedef type newname;
当我们
typedef int feet;
后就可以合法声明,创建一个整型变量WWA
feet WWA
枚举类
枚举类型是C++中的一种派生数据类型,它是由用户定义的若干枚举常量的集合。
如果一个变量只有几种可能的值,可以定义为枚举(enumeration)类型。所谓"枚举"是指将变量的值一一列举出来,变量的值只能在列举出来的值的范围内。
一般用enum进行创建:
enum 枚举名{
标识符[=整型常数],
标识符[=整型常数],
...
标识符[=整型常数]
} 枚举变量;
需注意的是枚举类型不一定要用在main中定义
实例1:
#include <iostream>
using namespace std;
int main(){
enum days{one, two, three}day;
day = one;
switch(day){
case one:
cout << "one" << endl;
break;
case two:
cout << "two" << endl;
break;
default:
cout << "three" << endl;
break;
}
return 0;
}
实例2:
#include <iostream>
using namespace std;
enum time
{
first,second,
third,forth,fifth
};
int main()
{
enum time a=fifth;
if (a==fifth)
{
cout << "Succeed!";
}
return 0;
}
C++中的变量定义与声明
格式:
type variable_list
在这里,type 必须是一个有效的 C++ 数据类型,可以是 char、wchar_t、int、float、double、bool 或任何用户自定义的对象,variable_list 可以由一个或多个标识符名称组成,多个标识符之间用逗号分隔。下面列出几个有效的声明:
//变量的定义
int i, j, k;
char c, ch;
float f, salary;
double d;
实例:
#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 ;
f = 70.0/3.0;
cout << f << endl ;
return 0;
}
C++ 变量作用域
作用域是程序的一个区域,一般来说有三个地方可以定义变量:
1、局部变量:在函数或一个代码块内部声明的变量,它们只能被函数内部或者代码块内部的语句使用。
#include <iostream>
using namespace std;
int main ()
{
// 局部变量声明
int a, b;
int c;
// 实际初始化
a = 10;
b = 20;
c = a + b;
cout << c;
return 0;
}
2、全局变量:在所有函数外部定义的变量(通常是在程序的头部),其的值在程序的整个生命周期内都是有效的,同时可以被任何函数访问,一旦声明,在整个程序中都是可用的。
#include <iostream>
using namespace std;
// 全局变量声明
int g;
int main ()
{
// 局部变量声明
int a, b;
// 实际初始化
a = 10;
b = 20;
g = a + b;
cout << g;
return 0;
}
需要注意的是:在程序中,局部变量和全局变量的名称可以相同,但是在函数内,局部变量的值会覆盖全局变量的值。
#include <iostream>
using namespace std;
// 全局变量声明
int g = 20;
int main ()
{
// 局部变量声明
int g = 10;
cout << g;
return 0;
}
若是满足它们作用域不同,就不会出现覆盖现象:
#include <iostream>
using namespace std;
int main()
{
int b = 2;
{
int b = 1;
cout << "b = " << b << endl;
}
cout << "b = " << b << endl;
}
初始化局部变量和全局变量
当局部变量被定义时,系统不会对其初始化,您必须自行对其初始化。定义全局变量时,系统会自动初始化为下列值:
数据类型 | 初始化默认值 |
---|---|
int | 0 |
char | ‘\0’ |
float | 0 |
double | 0 |
pointer | NULL |