02.C++基础

初识

一个简单的C++程序 simple.cpp

int main()
{
    return 0;
}

运行GNU编译器编译源码

g++ -o simple simple.cpp

输入输出程序


#include <iostream>

int main()
{
	std::cout << "Enter two numbers:" << std::endl;
	int v1 = 0, v2 = 0;
	std::cin >> v1 >> v2;
	std::cout << "Sum=" << (v1 + v2) << std::endl;
	return 0;
}

注释

#include <iostream>
/*
* 读取两个数并求取它们之和
*/
int main()
{
	//提示用户输入两个数
	std::cout << "Enter two numbers:" << std::endl;
	int v1 = 0, v2 = 0;
	std::cin >> v1 >> v2;
	std::cout << "Sum=" << v1 + v2 << std::endl;
	return 0;
}

基本内置类型

C++基本内置类型

基本数据类型在内存中存储值时需要占用的内存,以及该类型的变量所能存储的最大值和最小值

typesizeminmax
bool101
char1-128127(ASCII码)
signed char1-128127
unsigned char10255
int4-21474836482147483647
signed int4-21474836482147483647
unsigned int404294967295
short int2-3276832767
signed short int2-3276832767
unsigned short int2065535
long int4-21474836482147483647
signed long int4-21474836482147483647
unsigned long int404294967295
long long int8-92233720368547758089223372036854775807
signed long long int8-92233720368547758089223372036854775807
unsigned long long int8018446744073709551615
float41.17549e-383.40282e+38
double82.22507e-3081.79769e+308
wchar_t2065535
#include <iostream>
using namespace std;
int main()
{
	cout << "|type|" << "size|min|max|" << endl;
	cout << "|----|" << "----|----|----|" << endl;
	cout << "|bool|" << sizeof(bool) << "|" << (numeric_limits<bool>::min)() << "|" << (numeric_limits<bool>::max)() << endl;
	cout << "|char|" << sizeof(char) << "|" << int((numeric_limits<char>::min)()) << "|" << int((numeric_limits<char>::max)()) << "(ASCII码)" << endl;
	cout << "|signed char|" << sizeof(signed char) << "|" << int((numeric_limits<signed char>::min)()) << "|" << int((numeric_limits<signed char>::max)()) << endl;
	cout << "|unsigned char|" << sizeof(unsigned char) << "|" << int((numeric_limits<unsigned char>::min)()) << "|" << int((numeric_limits<unsigned char>::max)()) << endl;
	cout << "|int|" << sizeof(int) << "|" << (numeric_limits<int>::min)() << "|" << (numeric_limits<int>::max)() << endl;
	cout << "|signed int|" << sizeof(signed int) << "|" << (numeric_limits<signed int>::min)() << "|" << (numeric_limits<signed int>::max)() << endl;
	cout << "|unsigned int|" << sizeof(unsigned int) << "|" << (numeric_limits<unsigned int>::min)() << "|" << (numeric_limits<unsigned int>::max)() << endl;
	cout << "|short int|" << sizeof(short int) << "|" << (numeric_limits<short int>::min)() << "|" << (numeric_limits<short int>::max)() << endl;
	cout << "|signed short int|" << sizeof(signed short int) << "|" << (numeric_limits<signed short int>::min)() << "|" << (numeric_limits<signed short int>::max)() << endl;
	cout << "|unsigned short int|" << sizeof(unsigned short int) << "|" << (numeric_limits<unsigned short int>::min)() << "|" << (numeric_limits<unsigned short int>::max)() << endl;
	cout << "|long int|" << sizeof(long int) << "|" << (numeric_limits<long int>::min)() << "|" << (numeric_limits<long int>::max)() << endl;
	cout << "|signed long int|" << sizeof(signed long int) << "|" << (numeric_limits<signed long int>::min)() << "|" << (numeric_limits<signed long int>::max)() << endl;
	cout << "|unsigned long int|" << sizeof(unsigned long int) << "|" << (numeric_limits<unsigned long int>::min)() << "|" << (numeric_limits<unsigned long int>::max)() << endl;
	cout << "|long long int|" << sizeof(long long int) << "|" << (numeric_limits< long long int>::min)() << "|" << (numeric_limits< long long int>::max)() << endl;
	cout << "|signed long long int|" << sizeof(signed long long int) << "|" << (numeric_limits<signed long long int>::min)() << "|" << (numeric_limits<signed long long int>::max)() << endl;
	cout << "|unsigned long long int|" << sizeof(unsigned long long int) << "|" << (numeric_limits<unsigned long long int>::min)() << "|" << (numeric_limits<unsigned long long int>::max)() << endl;
	cout << "|float|" << sizeof(float) << "|" << (numeric_limits<float>::min)() << "|" << (numeric_limits<float>::max)() << endl;
	cout << "|double|" << sizeof(double) << "|" << (numeric_limits<double>::min)() << "|" << (numeric_limits<double>::max)() << endl;
	cout << "|wchar_t|" << sizeof(wchar_t) << "|" << (numeric_limits<wchar_t>::min)() << "|" << (numeric_limits<wchar_t>::max)() << endl;
}

复合类型(引用、指针、数组)

左值引用

#include <iostream>
int main()
{
	int ival = 1024;
	int& refVal = ival;
	std::cout << "refVal=" << refVal << std::endl;
	refVal = 2048;
	std::cout << "ival=" << ival << std::endl;
	return 0;
}

右值引用

TODO

指针

#include <iostream>
int main()
{
	int ival = 1024;
	//获取对象地址
	int* p1 = &ival;

	int* p2 = p1;
	std::cout << "p1=" << p1 << std::endl;
	//利用指针访问对象
	std::cout << "*p1=" << *p1 << std::endl;

	//由符号*得到指针p1所值的对象,并为它赋值
	*p1 = 2204;
	std::cout << "*p1=" << *p1 << std::endl;

	//void* 指针
	//TODO

	//指向指针的指针
	int** p3 = &p1;
	std::cout << "**p3=" << **p3 << std::endl;

	//指针的引用
	int*& r = p1;
	std::cout << "*r=" << *r << std::endl;
	return 0;
}

数组

#include <iostream>
int main()
{
	int arr1[3];
	//显示初始化数组
	int arr2[3] = { 1,2,3 };
	std::string str[2] = { "hello","world" };

	//访问数组
	for (auto item : str) {
		std::cout << item << std::endl;
	}

	//指针和数组
	std::string* p1 = &str[0];
	std::cout << "*p1=" << *p1 << std::endl;
	std::string* p2 = str;//等价于 p2=&str[0]
	std::cout << "*p2=" << *p2 << std::endl;
	p2++;
	std::cout << "p2++ =" << *p2 << std::endl;
	return 0;

	//多维数组
	//TODO
}

处理类型

类型别名

#include <iostream>
int main()
{
	int i = 1;
	//类型别名
	typedef int* pint;
	pint pt= &i;
	std::cout << "*pt=" << *pt << std::endl;
	return 0;
}

结构类型

#include <iostream>
using namespace std;

struct PERSON {   // Declare PERSON struct type
    int age;   // Declare member types
    long ss;
    float weight;
    char name[25];
} family_member;   // Define object of type PERSON

struct CELL {   // Declare CELL bit field
    unsigned short character  : 8;  // 00000000 ????????
    unsigned short foreground : 3;  // 00000??? 00000000
    unsigned short intensity  : 1;  // 0000?000 00000000
    unsigned short background : 3;  // 0???0000 00000000
    unsigned short blink      : 1;  // ?0000000 00000000
} screen[25][80];       // Array of bit fields

int main() {
    struct PERSON sister;   // C style structure declaration
    PERSON brother;   // C++ style structure declaration
    sister.age = 13;   // assign values to members
    brother.age = 7;
    cout << "sister.age = " << sister.age << '\n';
    cout << "brother.age = " << brother.age << '\n';

    CELL my_cell;
    my_cell.character = 1;
    cout << "my_cell.character = " << my_cell.character;
}
// Output:
// sister.age = 13
// brother.age = 7
// my_cell.character = 1

总结:
1)由可具有不同类型的成员构成
2)其成员默认为 public,不具有封装性
相关信息,请参阅 class、union 和 enum。

变量

变量声明、定义

#include <iostream>
int main()
{
	extern int v1;//声明变量v1,而非定义v1
	int v2 = -1;//声明并定义变量v2
	return 0;
}

字面值常量

表示

#include <iostream>
int main()
{
	int i1 = 20;//20表示十进制的整数字面值常量
	int i2 = 024;//024表示八进制的整数字面值常量
	int i3 = 0x24;//024表示十六进制的整数字面值常量
	std::cout << "hello world"<<std::endl;//hello world表示字符串字面值常量
	return 0;
}

转义序列

#include <iostream>
int main()
{
	std::cout << '\115' << '\n' << std::endl;
	return 0;
}

运算符

算术运算符

在这里插入图片描述

逻辑和关系运算符

在这里插入图片描述

赋值运算符

递增、递减运算符

成员访问运算符

#include <iostream>
int main()
{
	std::string str = "hello";
	std::string* p = &str;
	std::cout << "(*p).size()=" << (*p).size() << std::endl;
	std::cout << " p->size()=" << p->size() << std::endl;
}

条件运算符

位运算符

sizeof运算符

#include <iostream>
int main()
{
	int i = 0;
	std::cout << "sizeof(i)=" << sizeof(i) << std::endl;
}

逗号运算符

#include <iostream>
int main()
{
	std::string str = "hello", str2 = "world";
	std::string* p = &str;
	std::cout << "(*p).size()=" << (*p).size() << std::endl;
	std::cout << " p->size()=" << p->size() << std::endl;
}

表达式

Lvalues 和 Rvalues(TODO)

每个 C++ 表达式是左值或右值。 左值是指在单个表达式的外部保留的对象。 可以将左值视为具有名称的对象。 所有变量(包括不能更改的 (const) 变量)都是左值。 左值是一个不在使用它的表达式的外部保留的临时值。

语句

循环语句

while循环

#include <iostream>
int main()
{
	int sum = 0, val1 = 1;
	while (val1 <= 5)
	{
		sum += val1;
		val1++;
	}
	std::cout << sum << std::endl;
	return 0;
}

for循环

#include <iostream>
int main()
{
	int sum = 0, val1 = 1;
	for (int val1 = 1; val1 <= 5; val1++) {
		sum += val1;
	}
	std::cout << sum << std::endl;
	return 0;
}

条件语句

if else语句

#include <iostream>
int main()
{
	int val = -1;
	while (val!=0)
	{
		std::cout << "请输入一个数字" << std::endl;
		std::cin >> val;
		if (val > 0) {
			std::cout << "它是正数" << std::endl;
		}
		else if (val < 0) {
			std::cout << "它是负数" << std::endl;
		}
		else {
			std::cout << "它是0" << std::endl;
		}
	}

	return 0;
}

switch语句

TODO

跳转语句

异常捕获语句

函数

函数声明、定义

#include <iostream>
//函数声明
int fact(int val);
int main()
{
	std::cout << "fact(5)=" << fact(5) << std::endl;
}

//函数定义
int fact(int val) {
	int ret = 1;
	while (val > 1) {
		ret = ret * val;
		val--;
	}
	return ret;
}

结论: 1)函数在使用之前必须先声明
2)函数声明建议在头文件中

参数传递

#include <iostream>
//函数声明,建议在头文件中声明
void reset1(int val);
void reset2(int& val);
int main()
{
	int val1 = 1;
	int val2 = 1;
	reset1(val1);
	reset2(val2);
	std::cout << " reset1(val1)=" << val1 << std::endl;//reset1(val1) = 1
	std::cout << " reset2(val2)=" << val2 << std::endl;//reset2(val2) = 0
}

//值传递
void reset1(int val) {
	val = 0;
}

//引用传递
void reset2(int& val) {
	val = 0;
}

结论: 1)值传递不影响实参的值
2)引用传递会影响实参的值

const形参和实参

#include <iostream>
//函数声明,建议在头文件中声明
void reset1(const int val);
void reset2(const int val);
void reset3(int& val);
void reset4(int& val);
int main()
{
	int val1 = 1;
	int val2 = 1;
	int val3 = 1;
	//const实参
	const int val4 = 1;
	reset1(val1);
	std::cout << " val1=" << val1 << std::endl;
	reset2(val2);
	std::cout << " val2=" << val2 << std::endl;
	reset3(val3);
	std::cout << " val3=" << val3 << std::endl;
	//reset4(val4);//error C2664: “void reset4(int &)”: 无法将参数 1 从“const int”转换为“int &”
}
//const形参
void reset1(const int val) {
	//val = 0;//error C3892: “val”: 不能给常量赋值
}

void reset2(const int val) {
}

void reset3(int& val) {
	val = 0;
}

void reset4(int& val) {
	std::cout << " val=" << val << std::endl;
}

结论:
1)const形参不可写值
2)const实参不可引用传递

数组形参

#include <iostream>
using namespace std;
//函数声明
void print(const int arr[], int size);

int main()
{
	int arr[] = { 1,2,3 };
	print(arr, end(arr) - begin(arr));
}

void print(const int arr[], int size) {
	for (int i = 0; i < size; i++) {
		cout << arr[i] << endl;
	}
}

返回值

命令行选项参数

main函数处理:略

函数重载

默认实参

函数指针

#include <iostream>
using namespace std;
//函数声明
bool compare(int v1, int v2);
bool (*pf)(int v1, int v2);

int main()
{
	cout << "compare(1, 2)=" << compare(1, 2) << endl;
	cout << "compare(2, 1)=" << compare(2, 1) << endl;
	pf = compare;//等同于pf = &compare;
	cout << "pf(1, 2)=" << pf(1, 2) << endl;
	cout << "pf(2, 1)=" << pf(2, 1) << endl;
}

bool compare(int v1, int v2) {
	return v1 > v2;
}

结论:
1)函数指针变量通过函数名赋值

const限定符

#include <iostream>
int main()
{
	const int bufSize = 1024;
	//bufSize = 2048;//错误:试图修改const对象的值

	//常量引用
	const int& ref = bufSize;
	std::cout << "ref=" << ref << std::endl;

	//指向常量的指针
	const int* p = &bufSize;
	std::cout << "*p=" << *p << std::endl;
	
	//指向int类型的常量指针
	int i = 0;
	int* const p1 = &i;

	//常量表达式
	const int max_files1 = bufSize + 1;
	//constexpr修改变量用来验证常量表达式(C++11)
	constexpr int max_files2 = bufSize + 1;
	return 0;
}

命名空间

#include <iostream>
//using声明
using std::cout; 
using std::endl;
int main()
{
	cout << "命名空间using" << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值