C++学习笔记一

这篇博客介绍了C++的基础知识,从HelloWorld程序开始,讲解了注释的使用,包括单行和多行注释。接着讨论了变量的概念和声明,以及常量的两种定义方式:#define宏常量和const修饰的变量。此外,还提到了C++中的关键字及其分类,并阐述了标识符的命名规则。
摘要由CSDN通过智能技术生成

1.1Hello World

学习一门语言肯定也是从它的hello world开始的,下面就开始写,我使用的IDE工具是Visual Studio 2022,具体使用方法也不再说明,下面直接看代码

#include <iostream>
using namespace std;
int main()
{
	//代码的含义就是在屏幕中输出hello world
	cout << "hello world" << endl;
	system("pause");
		return 0;
}

代码很简单就是一个头文件加上一个main方法,里面输出hello world。

1.2注释

c++的注释的使用://加上你想加的注释为单行注释;/*加上你的注释*/为多行注释,有一个注意点是:一个项目里面只能有一个main方法。

#include <iostream>
using namespace std;
//单行注释
/*多行注释
int main1()
{
	cout << "hello world" << endl;
	system("pause");
	return 0;
}*/

1.3变量

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

语法:数据类型 变量名 = 初始值;(这和java是一样的)

下面是示例:

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

1.4常量

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

C++定义常量的两种方式:

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

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

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

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

代码示例:

#include <iostream>
using namespace std;
#define day 7
int main()
{
	//Day = 8;错误,Day是常量,一旦修改就会报错
	cout << "一周有:" << day <<"天"<< endl;
	const int second = 60;
	//second = 50;错误,const修饰的变量也称为常量
	cout << "一分钟有" << second << "秒" << endl;
	system("pause");
	return 0;
}

1.5关键字

关键字是C++中已经保留的单词也就是标识符,在定义变量和常量的时候不要使用关键字,以下为C++的关键字:

 其中的分类为:

数据类型:void,int,char,float,double,bool,w_char
类型定义:struct,union,enum,class,typedef
常量值:true,false
类型修饰符:long,short,singed,unsigned
类型限定符:const,volatile,restrict
存储说明符:auto,register,static,extern,thread_local,mutable
其它修饰符:inline,asm
循环控制:for,while,do
跳转控制:break,continue,return,goto
分支结构: if,else,switch,case,default
内存管理:new, delete
运算符:sizeof,and,and_eq,bitand,bitor,compl,not,not_eq,or,or_eq,xor,xor_eq
访问限定符:this,friend,virtual,mutable,explicit,operator
类访问修饰符:private,protected,public
模板:template,typename
命名空间:namespace,using
异常处理:throw,try,catch

1.6标识符命名规则

1.标识符不能是关键字

2.标识符是由字母、数字、下划线组成的

3.标识符第一个字符只能是字母或者是下划线

4.标识符区分大小写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值