[C.C++] C++初学篇1(黑马教程笔记)

307 0
Honkers 2026-7-2 18:36:13 | 显示全部楼层 |阅读模式

1 C++初识

1.1 第一个C++程序

编写一个C++程序总共分为4个步骤

  • 创建项目
  • 创建文件
  • 编写代码
  • 运行程序

1.1.1创建项目

Visual Studio是常用工具用来编写C++

创建空项目

1.1.2创建文件

右击源文件——添加——新建项——给文件起名称——确定

1.1.3编写代码

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. cout << "hello world" << endl;
  6. system("pause");
  7. return 0;
  8. }
复制代码

1.1.4 运行程序

hello world
请按任意键继续. . .

1.2 注释

两种格式

1. 单行注释: //描述信息

  • 通常放在一行代码的上方,或者一条语句的末尾,该代码进行说明

2.多行注释:  /*描述信息*/

  • 通常在一段代码的上方,对该段代码做整体说明

1.3变量

变量存在的意义:方便我们管理内存空间

创建变量的语法: 数据类型 变量名 = 变量初始值;

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. //变量的创建
  6. int a = 5;
  7. cout << "a=" << a << endl;
  8. system("pause");
  9. return 0;
  10. }
复制代码

运行结果  a=5

1.4 常量

作用:用于记录程序中不可更改的数据

C++定义常量两种方式

        1. #define 宏常量:#define 常量名 常量值

                通常在文件上方定义:表示为一个常量

        2. const修饰的变量:const 数据类型 常量名 = 常量值

                通常在变量定义前加关键字const,修饰该变量为常量,不可更改

示例:

  1. #include <iostream>
  2. using namespace std;
  3. //常量的定义方式
  4. //1. #define 宏常量
  5. //2. const修饰的变量
  6. //1. #define 宏常量
  7. #define Day 7
  8. int main()
  9. {
  10. cout << "一周总共有" << Day << "天"<<endl;
  11. //Day = 8;//修改会报错
  12. //2.const修饰的变量
  13. const int month = 12;
  14. //month = 24; //报错,不可修改
  15. cout << "一年总共有" << month << "个月份" << endl;
  16. system("pause");
  17. return 0;
  18. }
复制代码

1.5 关键字

作用:关键字是C++中预先保留的单词(标识符)

  • 在定义变量或者常量时候,不要用关键字

C++关键字如下:

asmdoifreturntypedef
autodoubleinlineshorttypeid
booldynamic_castintsignedtypename
breakelselongsizedfunion
caseenummutablestaticunsigned
catchexplicitnamespacestatic_castusing
charexportnewstructvirtual
classexternoperatorswitchvoid
constfaiseprivatetemplatevolatile
const_castfloatprotectedthiswchar_t
continueforpublicthrow

while

defaultfriendregistertrue
deletegotoreinterpret_casttry

1.6 标识符命名规则

作用:C++规定给标识符(变量、常量)命名时,有一套自己的规则

  • 标识符不能时关键字
  • 标识符只能由字母、数字、下划线组成
  • 第一个字符必须为字母或下划线
  • 标识符中字母区分大小写

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行