C++基础学习01

35 篇文章 1 订阅

C++语法学习

将通过一些简单demo来学习 C++的基本语法基础,博主是有一定C的基础的,所以在默写太基础的释义上将不会做过多的解释,顺序在某些情况而言可能是比较乱的,没有一个合理的规整,学到啥,看到啥,就记录啥。

什么是C++

C++就是C语言的继承,它既可以进行C语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计,还可以进行以继承和多态为特点的面向对象的程序设计。C++擅长面向对象程序设计的同时,还可以进行基于过程的程序设计,因而C++就适应的问题规模而论,大小由之。
这东西讲了好像也并不能给代码有啥帮助,认识认识就好了

简要介绍一段程序

#include <iostream>	//引用函数库(头文件)
using namespace std;
// main() 是程序开始执行的地方
int main()
{
   cout << "Hello World"<<endl; // 输出 Hello World
   return 0;
}

你可以用 “\n” 代替以上代码里的 endl。

cout << "Hello World\n"; // 输出 Hello World

引用

#include<iostream>

这是啥

iostream是C++标准库的头定义, 对应的基本上是C++的输入输出相关库定义
就好比stdio 是C标准库里面的函数库 对应的基本都是标准输入输出等等C语言常用库的定义

还有啥

  • #include<iomanip> 控制精度
  • #include<cmath> 数学函数
  • #include<string> 字符串
  • #include<algorithm>STL通用算法
  • 又没了
  • C++ 标准库头文件

命名空间

using namespace std;告诉编译器使用 std 命名空间。命名空间是 C++ 中一个相对新的概念

C++ 关键字

下表列出了 C++ 中的保留字。这些保留字不能作为常量名、变量名或其他标识符名称。

asmelsenewthis
autoenumoperatorthrow
boolexplicitprivatetrue
breakexportprotectedtry
caseexternpublictypedef
catchfalseregistertypeid
charfloatreinterpret_casttypename
classforreturnunion
constfriendshortunsigned
const_castgotosignedusing
continueifsizeofvirtual
defaultinlinestaticvoid
deleteintstatic_castvolatile
dolongstructwchar_t
doublemutableswitchwhile
dynamic_castnamespacetemplate

完整关键字介绍可查阅:C++ 的关键字(保留字)完整介绍

输入输出

输入

int a,b,c;
cin >> a >> b >> c;	//分别给a,b,c三个变量赋值

数组同样可以采用这种方式输入,也可以同时输入几个数据类型不相同的数据

int name[20];
cin>name;

输出

int a,b,c;
cout << a << b << c <<endl;//打印输入a,b,c
cout <<"a="<< a <<endl;//字符拼接

示例

给定你一个销售人员的名字,底薪以及月销售额。
请你计算他的月收入是多少。
已知月收入等于底薪加15%的月销售额。
所有数据保留两位小数。

输入格式

输入第一行包含一个由大写字母构成的长度不超过10的字符串,表示销售人员的名字。
第二行包含一个浮点数,表示该人员的底薪。
第三行包含一个浮点数,表示该人员的月销售额。

输出格式

输出格式为“TOTAL = R$ X”,X为该人员月收入。

#include <iostream>	//引用标准的函数库,包含输入输出
using namespace std;	//给定命名空间
int main(){
    char name[10];
    float a,b;
    cin>>name>>a>>b;	//多个数据输入,包含数组
    printf("TOTAL = R$ %.2f",a+b*0.15);	//通过printf控制精度
}

数据类型

基本数据类型

类型关键字
布尔型bool
字符型char
整型int
浮点型float
双浮点型double
无类型void
宽字符型wchar_t

搞不懂宽字符有啥用,不去钻了

类型范围
char1 个字节-128 到 127 或者 0 到 255
unsignedchar1 个字节 0 到 255
signedchar1 个字节 -128 到 127
int4 个字节-2147483648 到 2147483647
unsigned int4 个字节0 到 4294967295
signed int4 个字节-2147483648 到 2147483647
short int2 个字节-32768 到 32767
unsigned short int2 个字节0 到 65,535
signed short int2 个字节-32768 到 32767
long int8 个字节-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int8 个字节-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long int8 个字节0 到 18,446,744,073,709,551,615
float4 个字节精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字)
double8 个字节双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字)
long double16 个字节长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。
wchar_t2 或 4 个字节1 个宽字符

注:变量的大小会根据编译器和所使用的电脑而有所不同。

#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;  
}

运行结果:

type:           ************size**************
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:    所占字节数:12  最大值:1.18973e+4932   最小值:3.3621e-4932
float:          所占字节数:4   最大值:3.40282e+038    最小值:1.17549e-038
size_t:         所占字节数:4   最大值:4294967295      最小值:0
string:         所占字节数:4
type:           ************size**************
--------------------------------
Process exited with return value 0
Press any key to continue . . .

注意数据精度的控制与整型长度的控制,选用不同的数据类型,避免数据错误或数据溢出

typedef 声明

可以使用 typedef 为一个已有的类型取一个新的名字。下面是使用 typedef 定义一个新类型的语法:

typedef type newname; 

例如,下面的语句会告诉编译器,tyInt是 int 的另一个名称:

typedef int feet;

现在,下面的声明是完全合法的,它创建了一个整型变量 distance:

feet distance;

枚举类型

如果一个变量只有几种可能的值,可以定义为枚举(enumeration)类型。

所谓"枚举"是指将变量的值一一列举出来,变量的值只能在列举出来的值的范围内。

创建枚举,需要使用关键字 **enum**。枚举类型的一般形式为:

enum 枚举名{ 
     标识符[=整型常数], 
     标识符[=整型常数], 
... 
    标识符[=整型常数]
} 枚举变量;
    

如果枚举没有初始化, 即省掉"=整型常数"时, 则从第一个标识符开始。

enum color { red, green, blue } c;
c = blue;

默认情况下,第一个名称的值为 0,第二个名称的值为 1,第三个名称的值为 2,以此类推。

但是,也可以给名称赋予一个特殊的值,只需要添加一个初始值即可。例如,在下面的枚举中,green 的值为 5。

enum color { red, green=5, blue };

在这里,blue 的值为 6,因为默认情况下,每个名称都会比它前面一个名称大 1,但 red 的值依然为 0。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空 白

停止的只会是自己,你得追逐世界

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值