C++入门基础第一篇
1.写一个helloworld用C++
002第一个C++程序C++书写helloworld
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = { "Hello Word" };
cout << str << endl;
system("pause");
return 0;
}
003程序的注释-单行注释和多行注释
//单行注释
/*
多行注释,注意多行注释不可以嵌套
*/
2. 变量
004变量-变量的使用-变量的意义
变量的使用和变量的意义
变量存在的意义:方便我们管理内存空间
变量创建的语法:数据类型 变量名 = 变量初始值;
变量的声明,如果想声明一个变量而非定义它,就在变量的名前添加关键字extern,而不是显示的初始化变量:
extern int i ; //声明i而非定义i
int j ; //声明j并定义j;
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str = { "Hello Word" };
cout << str << endl;
int a = 10;
cout << "a = " << a << endl;
system("pause");
return 0;
}
005常量-常量与变量的区别-常量的使用
常量
作用:用于记录程序中不可更改的数据
C++定义常量的两种方式
1、#define 宏常量
#define 常量名 常量值
2、const修饰的变量
const 数据类型 变量名 = 常量值
通常在变量定义前加关键字const,修饰变量为常量,不可修改
#include<iostream>
#include<string>
using namespace std;
#define Day 7
int main()
{
//Day = 14; //错误,Day是常量,一旦修改就会报错
cout << "一周总共有:" << Day << "天" << endl;
//const 修饰的变量
const int month = 12;
//month = 24; //错误,const修饰的变量也称为常量
cout << "一年有:" << month << "个月" << endl;
system("pause");
return 0;
}
3. 关键字与标识符
006关键字-C++常用的编程关键字
作用:关键字是C++中预先保留的单词(标识符) 在定义变量或者常量的时候,不要用关键字。
#include<iostream>
#include<string>
using namespace std;
int main()
{
//创建变量:数据类型 变量名称 = 变量初始值
//不要用关键字给变量或常量起名称
//int int = 10; //错误,第二个int是关键字,不可以作为变量的名称
int a = 10;
cout << "a = " << a << endl;
system("pause");
return 0;
}
007标识符命名规则
标识符命名规则
作用:C++规定标识符(变量、常量)命名时,有一套自己的规则
- 标识符不能是关键字
- 标识符只能由字母、数字、下划线组成
- 第一个字符必须为字母或下划线
- 标识符中字母区分大小写 建议:给标识符命名时,争取做到见名知意的效果
4. 数据类型
008数据类型-整型
C++规定在创建一个变量或者常量时,必须要指定出相应的数据类型,否则无法给变量分配内存
作用:整型变量表示的是整数类型的数据
c++中能够表示整型的类型有以下几种方式,区别在于所占的内存空间不同:
#include<iostream>
#include<string>
using namespace std;
//语法:数据类型 变量名 = 变量的初始值
//数据类型存在的意义:给变量分配合适的内存空间
int main()
{
//整型
short num1 = 10; //1、短整型(-32768 ~ 32767)
int num2 = 10; //2、整型
long num3 = 10; //3、长整型
long long num4 = 10; //4、长长整型
cout << "num1 =" << num1 << endl;
cout << "num2 =" << num2 << endl;
cout << "num3 =" << num3 << endl;
cout << "num4 =" << num4 << endl;
system("pause");
return 0;
}
009数据类型-sizeof关键字
作用:利用sizeof关键字可以统计数据类型所占内存大小 语法:sizeof(数据类型/变量)
#include<iostream>
#include<string>
using namespace std;
int main()
{
short num1 = 10;
cout << "short占用的内存空间为:" << sizeof(short) << endl;
int num2 = 10;
cout << "int占用的内存空间为:" << sizeof(int) << endl;
long num3 = 10;
cout << "long占用的内存空间为:" << sizeof(long) << endl;
long num4 = 10;
cout << "long long占用的内存空间为:" << sizeof(long long) << endl;
system("pause");
return 0;
}
//short < int <=long <= long long
010数据类型-实型
作用:用于表示小数
浮点型变量分为两种:
1、单精度float
2、双精度double
两者的区别在于表示的有效数字范围不同
#include<iostream>
#include<string>
using namespace std;
int main()
{
//默认情况下输出一个小数,会显示出6位有效数字
float f1 = 3.1415926f;
cout << "f1 = " << f1 << endl;
double d1 = 3.1415926;
cout << "d1 = " << d1 << endl;
//统计float 和double的占用的内存空间
cout << "float占用的内存空间为:" << sizeof(float) << endl;
cout << "double占用的内存空间为:" << sizeof(double) << endl;
//科学计数法
float f2 = 3e3f; //3*10^3
cout << "f2 = " << f2 << endl;
float f3 = 3e-2f; //3*0.1^2
cout << "f3 = " << f3 << endl;
system("pause");
return 0;
}
011数据类型-字符型
作用:字符型变量用于显示单个字符
//语法:
char ch = 'a';
- note1:在显示字符变量时,用单引号将字符括起来,不要用双引号
- note:单引号内只能有一个字符,不可以是字符串
C和C++中字符型变量只占用1个字节
字符型比变量并不是把字符本身放在内存中存储,而是将对应得ASCII码放入存储单元
#include<iostream>
//#include<string>
using namespace std;
int main()
{
//1、字符型变量的创建方式
char ch = 'a'; //a- 97
char ch4 = 'A'; //A- 65
cout << "ch = " << ch << endl;
//2、字符型变量所占的内存大小
cout << "char型字符串所占的内存空间:" << sizeof(char) << endl;
//3、字符型变量常见的错误
//char ch2 = "b"; //创建字符型变量时候,要用单引号
char ch3 = 'abcd'; //创建字符型变量的时候单引号只能有一个字符
cout << ch3 << endl;
//4、字符型变量对应的ASCII码
cout << (int)ch << endl;
cout << (int)ch4 << endl;
system("pause");
return 0;
}
012数据类型-转义字符
作用:用于表示一些不能显示出来的ASCII字符
现阶段我们常用的转义字符有:\n \ \t
#include<iostream>
#include<string>
using namespace std;
int main()
{
//转义字符
cout << "hello world\n"; //换行符 \n
cout << "\\" << "\n"; // 反斜杠 "\\"
//水平制表符 \t 作用可以整齐的输出数据
cout << "aaa\thello world" << endl;
cout << "aaaaa\thello world" << endl;
cout << "a\thello world" << endl;
system("pause");
return 0;
}
013数据类型-字符串类型
字符串类型 作用:用于表示一串字符 两种风格:
- C风格字符串:char 变量名[] = "字符串值"
- C++风格字符串:string 变量名 = "字符串"
#include<iostream>
#include<string>
using namespace std;
int main()
{
//1、C风格的字符串
//注意: char 字符串名 []
//注意2: 等号后面要用双引号,
char str1[] = "hello world";
cout << str1 << endl;
string str2 = "hello world!!!";
cout << str2 << endl;
system("pause");
return 0;
}
014数据类型-布尔类型
作用:布尔数据类型代表真或假的值
bool 类型只有两个值:
- true -----真(本质是1)
- false-----假(本质是0) bool类型占1个字节的大小
#include<iostream>
#include<string>
using namespace std;
int main()
{
//1、创建bool数据类型
bool flag = true;
cout << flag << endl;
flag = false;
cout << flag << endl;
//2、查看bool类型所占内存空间
cout << "bool类型所占的内存空间:" << sizeof(bool) << endl;
system("pause");
return 0;
}
015数据类型-数据输入
作用:用于从键盘获取数据
关键字:cin
语法:cin>>变量
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a;
cout << "请给整型变量赋值:" << endl;
cin >> a;
cout << "a = " <<a<< endl;
system("pause");
return 0;
}
016运算符-算术运算符-加减乘除运算
作用:用于执行代码的运算
算术运算符
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a1 = 10;
int b1 = 3;
cout << a1 + b1 << endl;
cout << a1 - b1 << endl;
cout << a1* b1 << endl;
//两个整数相除,结果依然是整数,将小数部分去掉
cout << a1 / b1 << endl;
system("pause");
return 0;
}
5. 运算符
017运算符-算术运算符-取模运算
#include<iostream>
#include<string>
using namespace std;
int main()
{
//取模运算的本质是求余数
int a1 = 10;
int b1 = 3;
cout << 10 % 3 << endl;
int a2 = 10;
int b2 = 20;
cout << a2%b2 << endl;
//两个小数是不可以做取模运算的
system("pause");
return 0;
}
018运算符-算术运算符-递增递减
作用:用于处理四则运算
#include<iostream>
#include<string>
using namespace std;
int main()
{
//1、前置递增
int a = 10;
++a;
cout << "a= " << a << endl;
//2、后置递增
int b = 10; b++;
cout << "b= " << b << endl;
//3、前置和后置的区别
//前置递增,先让变量+1然后进行表达式运算
int a2 = 10;
int b2 = ++a2 * 10;
cout << "a2 = " << a2 << endl;
cout << "b2 = " << b2 << endl;
//后置递增,先进行表达式运算,后让变量+1
int a3= 10;
int b3 =a3++ * 10;
cout << "a3 = " << a3 << endl;
cout << "b3 = " << b3 << endl;
system("pause");
return 0;
}
019运算符-赋值运算符
作用:用于将表达式的值赋给变量
赋值运算符包括以下几个符号:
#include<iostream>
#include<string>
using namespace std;
int main()
{
//赋值运算符
// =
int a = 10;a = 100;
cout << "a = " << a << endl;
// +=
a = 10; a += 2;
cout << "a = " << a << endl;
// -=
a = 10; a -= 2;
cout << "a = " << a << endl;
// *=
a = 10; a *= 2;
cout << "a = " << a << endl;
// /=
a = 10; a /= 2;
cout << "a = " << a << endl;
// %=
a = 10; a %= 2;
cout << "a = " << a << endl;
system("pause");
return 0;
}
020运算符-比较运算符
作用:用于表达式的比较,并返回一个真值或假值
比较运算符有以下符号:
#include<iostream>
#include<string>
using namespace std;
int main()
{
//比较运算符
// ==
int a = 10, b = 20;
cout << (a == b) << endl;
// !=
cout << (a != b) << endl;
// >
cout << (a > b) << endl;
// <
cout << (a < b) << endl;
// >=
cout << (a <= b) << endl;
// <=
cout << (a >= b) << endl;
system("pause");
return 0;
}
021运算符-逻辑运算符-非
作用:用于根据表达式的值返回真值或假值
逻辑运算符有以下符号:
#include<iostream>
#include<string>
using namespace std;
int main()
{
//逻辑运算符 非!
int a = 10;
//在C++中除了0都为真
cout << !a << endl;
cout << !!a << endl;
system("pause");
return 0;
}
022运算符-逻辑运算符-与
#include<iostream>
#include<string>
using namespace std;
int main()
{
//逻辑运算符 与 &&
int a = 10, b = 10;
cout << (a&&b) << endl;
a = 0, b = 10;
cout << (a&&b) << endl;
a = 0, b = 0;
cout << (a&&b) << endl;
system("pause");
return 0;
}
023运算符-逻辑运算符-或
#include<iostream>
#include<string>
using namespace std;
int main()
{
//逻辑运算符 或 ||
int a = 10, b = 10;
cout << (a||b) << endl;
a = 0, b = 10;
cout << (a||b) << endl;
a = 0, b = 0;
cout << (a||b) << endl;
system("pause");
return 0;
}