C++学习01

本文详细介绍了C++01的基础语法,包括iostream库、命名空间的使用、作用域运算符、命名空间嵌套、using声明与编译指令,以及C++对C语言的增强,如全局变量检测、函数增强、类型转换和引用等概念。
摘要由CSDN通过智能技术生成

C++01学习思维导图:

 一、初始C++:
#include <iostream>:标准输入输出流  i - input  输入  o - output 输出 stream 流  相当于 stdio.h

using namespace std:使用 标准 命名空间

cout : 标准输出流对象

<< 左移:在C++下有了新的寓意 用于在cout后拼接输出的内容

endl : end line 刷新缓冲区 并且换行

#include <iostream>  //标准输入输出流   i - input  输入  o - output 输出  stream 流  相当于 stdio.h
using namespace std; //使用  标准  命名空间  
//#include <time.h>
//#include <ctime>
//#include <math.h>
//#include <cmath>
//程序入口函数
int main()
{
	// cout  标准输出流对象
	// << 左移  在C++下有了新的寓意  用于在cout后拼接输出的内容   
	// endl   --- end line  刷新缓冲区 并且换行  
	cout << "hello world"  << endl;
	system("pause");  //阻塞
	return EXIT_SUCCESS;  //返回正常退出
}

二、双冒号作用运算符:

:: :代表作用域 如果前面什么都不添加 代表全局作用域

void test()
{
	int atk = 2000;
	std::cout << "atk = " << atk << std::endl;
	// ::代表作用域  如果前面什么都不添加 代表全局作用域
	std::cout << "全局 atk = " << ::atk << std::endl;
}

 

 三、命名空间

3-1.命名空间用途: 解决名称冲突

void test01()
{
	KingGlory::goAtk();
	LOL::goAtk();
}

3-2.命名空间下 可以放  变量、函数、结构体、类...

namespace A
{
	int m_A;
	void func();
	struct Person
	{};
	class Animal
	{};
}

3-3.命名空间 必须要声明在全局作用域下

void test02()
{
	//namespace B{}; 不可以命名到局部作用域
}

 3-4.命名空间可以嵌套命名空间

namespace B
{
	int m_A = 10;
	namespace C
	{
		int m_A = 20;
	}
}
void test()
{
	cout << "B空间下的m_A = " << B::m_A << endl;
	cout << "C空间下的m_A = " << B::C::m_A << endl;
}

 3-5.命名空间是开放的,可以随时给命名空间添加新的成员

namespace B
{
	int m_B = 100;
}
void test()
{
	cout << "B空间下的m_A = " << B::m_A << endl;
	cout << "B空间下的m_B = " << B::m_B << endl;
}

3-6.命名空间可以是匿名的

namespace
{
	int m_C = 1000;
	int m_D = 2000; 
	//当写的命名空间的匿名的,相当于写了  static int m_C = 1000; static int m_D = 2000;
}

void test()
{
	cout << "m_C = " << m_C   << endl;
	cout << "m_D = " << ::m_D << endl;
}

 3-7.命名空间可以起别名

namespace veryLongName
{
	int m_E = 10000;
	void func()
	{
		cout << "aaa" << endl;
	}
}

void test06()
{
	namespace veryShortName = veryLongName;
	cout << veryShortName::m_E << endl;
	cout << veryLongName::m_E << endl;

}

 四、using声明及using编译指令

当using声明与就近原则同时出现,出错,尽量避免

当using编译指令与就近原则同时出现  优先使用就近

当using编译指令有多个,需要加作用域区分

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

namespace KingGlory
{
	int sunwukongId = 1;
}

namespace LOL
{
	int sunwukongId = 3;
}

void test1()
{
	int sunwukongId = 2;
	//1、using声明
	//using KingGlory::sunwukongId ;  
	//当using声明与就近原则同时出现,出错,尽量避免
	cout << sunwukongId << endl;

}
void test2()
{
	//2、using编译指令
	using namespace KingGlory;
	using namespace LOL;
	//当using编译指令与就近原则同时出现  优先使用就近
	//当using编译指令有多个,需要加作用域区分
	cout << KingGlory::sunwukongId << endl;
	cout << LOL::sunwukongId << endl;
}

 五、 C++对C语言增强和扩展_C语言

5-1.全局变量检测增强 

C++检测出重定义

int a;
//int a = 10;

5-2.函数检测增强  

返回值检测、形参类型检测、函数调用参数个数

int getRectS(int w,int h)
{
	return w *h;
}
int main()
{
	printf("%d\n", getRectS(10, 10));
}

5-3.型转换检测增强

void test()
{
	char * p = (char *)malloc(64);
}

5-4.struct增强

C++可以放函数,创建结构体变量,可以简化关键字 struct

struct Person
{
	int age;
	void func()
	{
		age++;
	}
};

void main()
{
	Person p;
	p.age = 17;
	p.func();
	cout << "p的age = " << p.age << endl;
}

 5-5.bool类型扩展

 C语言下 没有这个类型  C++有bool类型

bool flag = true; // bool类型 代表  真和假   true  ---- 真(1)    false  ---- 假(0)

void main()
{
	cout << sizeof(bool) << endl; //结果是1个字节
	//flag = false;
	//flag = 100; //将非0的数都转为1
	cout << flag << endl;
}

5-6.三目运算符增强

void main()
{
	int a = 10;
	int b = 20;
	printf("ret = %d\n", a > b ? a : b);
	(a < b ? a : b )= 100; // C++下返回的是变量  b = 100
	printf("a = %d\n", a);
	printf("b = %d\n", b);
}

 5-7.const增强

全局const   和C语言结论一致

const int m_A = 100;
void main()
{
	//m_A = 200;
	//int * p = (int *)&m_A;
	//*p = 200;
	//局部const
	const int m_B = 100;
	//m_B = 200;
	int * p = (int *)&m_B;
	*p = 200;
	cout << "m_B = " << m_B << endl;
	int arr[m_B]; //C++下const修饰的变量 称为常量 ,可以初始化数组

}

六、 C语言下const修饰全局变量默认是外部链接属性

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(){

	extern const int g_a; 
	printf("g_a = %d\n", g_a);
	system("pause");
	return EXIT_SUCCESS;
}

七、 C++下const修饰全局变量默认是内部链接属性

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

int main(){

	extern const int g_b;
	cout << "g_b = " << g_b << endl;;
	system("pause");
	return EXIT_SUCCESS;
}

八、const分配内存情况

8-1.对const变量 取地址 ,会分配临时内存  

void test01()
{
	const int a = 10;
	int * p = (int *)&a;
}

8-2.使用普通变量  初始化 const变量

void test02()
{
	int a = 10;
	const int b = a;
	int *p = (int *)&b;
	*p = 1000;
	cout << "b = " << b << endl;

}

8-3.对于自定义数据类型 

struct Person
{
	string m_Name;
	int m_Age;
};

九、引用基本语法

9-1.引用基本语法:  类型  &别名 = 原名

void test1()
{
	int a = 10;
	int &b = a;
	b = 100;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}

void test2()
{
	int a = 10;
	//int &b; //引用必须要初始化
	int &b = a;
	//引用一旦初始化后,就不可以引向其他变量
	int c = 100;
	b = c; // 赋值
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}

9-2.对数组建立引用

void test3()
{
	//1、直接建立引用
	int arr[10];
	int(&pArr)[10] = arr;
	for (int i = 0; i < 10; i++)
	{
		arr[i] = 100 + i;
	}
	for (int i = 0; i < 10; i++)
	{
		cout << pArr[i] << endl;
	}
	//2、先定义出数组类型,再通过类型 定义引用
	typedef int(ARRAY_TYPE)[10];
	//类型  &别名 = 原名
	ARRAY_TYPE & pArr2 = arr;
	for (int i = 0; i < 10; i++)
	{
		cout << pArr2[i] << endl;
	}

}

十、参数的传递方式

10-1.值传递

void mySwap1(int a  , int b)
{
	int temp = a;
	a = b;
	b = temp;
	cout << ":::a = " << a << endl;
	cout << ":::b = " << b << endl;
}

10-2.地址传递

void mySwap2(int *a, int *b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
}

10-3.引用传递

void mySwap3(int &a , int &b) // int &a = a; int &b = b;
{
	int temp = a;
	a = b;
	b = temp;
}

十一、指针引用

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

struct Person
{
	int age;
};

void allocateSpace(Person ** p)
{
	//p指向指针的指针    *p  指针 指向的是person 本体   **p  person本体
	*p = (Person *)malloc(sizeof(Person));
	(*p)->age = 10;

}

void test1()
{
	Person * p = NULL;
	allocateSpace(&p);

	cout << "p.age = " << p->age <<  endl;
}



void allocateSpace2(Person* &pp) // Person * &pp = p;
{
	pp = (Person *)malloc(sizeof(Person));
	pp->age = 20;
}

void test2()
{
	Person *p = NULL;
	allocateSpace2(p);
	cout << "p.age = " << p->age << endl;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值