栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++学习--点滴记录001

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++学习--点滴记录001

1 C++初识 1.1 C++程序

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

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

​Visual Studio编写C++程序的工具,适合小白入门
官网下载安装: Visual Studio官网

1.1.2 创建文件

右键源文件,选择添加->新建项,给C++文件起个名称,然后点击添加即可

1.1.3 编写代码
#include
using namespace std;
int main() {
	cout << "Hello,World!" << endl;
	system("pause");
	return 0;
}
1.1.4 运行程序

1.2 注释

两种格式

  1. 单行注释:// 描述信息
    • 通常放在一行代码的上方,或者一条语句的末尾,对该行代码说明
  2. 多行注释:
    • 通常放在一段代码的上方,对该段代码做整体说明
1.3 变量

作用:给一段指定的内存空间起名,方便操作这段内存

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

示例:

#include
using namespace std;
int main() {
	int a = 10;
    cout << "a = " << a << endl;
	system("pause");
	return 0;
}

C++在创建变量时,必须给变量一个初始值,否则会报错

1.4 常量

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

C++定义常量两种方式

  1. #define 宏常量: #define 常量名 常量值
    • 通常在文件上方定义,表示一个常量
#include
using namespace std;
#define b 10
int main() {
    cout << b << endl;
	system("pause");
	return 0;
}
  1. const修饰的变量 const 数据类型 常量名 = 常量值
    • 通常在变量定义前加关键字const,修饰该变量为常量,不可修改
#include
using namespace std;
int main() {
    const int c = 20;
    cout << c << endl;
	system("pause");
	return 0;
}
1.5 关键字

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

C++关键字如下:

asmdoifreturntypedef
autodoubleinlineshorttypeid
booldynamic_castintsignedtypename
breakelselongsizeofunion
caseenummutablestaticunsigned
catchexplicitnamespacestatic_castusing
charexportnewstructvirtual
classexternoperatorswitchvoid
constfalseprivatetemplatevolatile
const_castfloatprotectedthiswchar_t
continueforpublicthrowwhile
defaultfriendregistertrue
deletegotoreinterpret_casttry

在给变量或者常量起名称时候,不要用C++得关键字,否则会产生歧义

1.6 标识符命名规则

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

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

希望本文对大家有帮助,上文若有不妥之处,欢迎指正

分享决定高度,学习拉开差距

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934521.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号