C++基础(补充)

文章目录

  • 0.C++安排的意义
  • 1.命名空间(补的第一个坑)
  • 3.C++输入和输出
  • 2.缺省参数

0.C++安排的意义

  1. 在C的基础上,容纳进去了面向对象编程的思想,增加一些库以及编程范式等。
  2. 补充C语言语法的不足,以及对其不合理的地方进行优化。如作用域,IO,函数,指针,宏等。

C++63关键字,C语言32关键字

1.命名空间(补的第一个坑)

#include 
int rand = 10;
int main()
{
	printf("%d\n", rand);
	return 0;
}

此时代码可以运行,输出结果为10.

#include 

但当加入头文件时,输出代码显示错误
C++基础(补充)_第1张图片
显示的是rand重定义,为什么报错呢?
C语言库中有一个函数,rand。此时我们自己定义的和库里面相冲突
我们自己定义的和库里面相冲突,或项目组多个人定义的名字冲突
这是C语言的缺陷,C语言没办法解决类似的命名冲突问题,所以C++提出了namespace来解决。

void f1()
{
	int a = 0;
}
void f2()
{
	int a = 1;
}
int a = 2;

此时这个代码正常运行,因为每个变量a都不在一个域中
C++基础(补充)_第2张图片

    int a=2; //此行代码应放最上面,否则调用全局域的时候找不到

	printf("%d\n", a);//先找局部,后找全局,不在命名空间找。全局没有就报错
	printf("%d\n", ::a);//::左边是空就代表全局,从全局去找。想找全局,就用域作用限定符::

List.h

#pragma once
//小方实现
struct Node
{
	struct Node* next;
	struct Node* prev;
	int val;
};
Blist::min++;

Queue.h

#pragma once
namespace AQueue
{
//小明实现
struct Node
{
	struct Node* next;
	int val;
};
struct Queue
{
	struct Node* head;
	struct Node* tail;

};
}

Test.h

#include "List.h"
#include "Queue.h"
int main()
{
return 0;
}

C++基础(补充)_第3张图片
此时显示重命名。如何做?
设置一个命名空间——命名空间域。namespace,只影响使用,不影响生命周期。

//Queue.h:
namespace AQueue//命名空间域
//List.h
namespace BList
//此时不再冲突
int main()
{
struct AQueue::Node node1;//指定命名空间,此时可以重名
struct BList::Node node2;
AQueue::min++;
Blist::min++;
return 0;
}

不只针对类型,变量(int A=0;),函数也可.即可以将其放入命名空间中。
多个文件可以定义同一个名字的命名空间,最后合并在一起。或者可以嵌套一层命名空间

//嵌套调用
namespace q
{
   namespace p{
   //...
   }
}

命名空间的使用:

//Queue.h
void QueueInit(struct Queue* q)
{}
void QueuePush(struct Queue* q, int x)
{}

std是标准库专门构建出来的一道防线。防止自己定义的东西和库里冲突,但加了这一句毁于一旦。
所以一般情况下不建议展开,一般为了方便写小的代码可以展开。如果展开相当于没有命名空间了。
针对命名空间的展开方法有以下三条:

//Test.cpp
#include
using namespace AQueue;//1.全局展开
using namespace std;
int main()
{
	//struct AQueue::Queue q;//2.指定命名空间访问
	//AQueue::QueueInit(&q);
	//AQueue::QueuePush(&q,1);
	//AQueue::QueuePush(&q,2);
	struct Queue q;
	QueueInit(&q);
    QueuePush(&q,1);
	QueuePush(&q,2);
	cout<<"1111"<<endl;
	return 0;
}

using namespace std;//C++标准库的命名空间。

//3.部分展开
//常用展开,只展开了cout和endl
using std::cout;
using std::endl;
int main()
{
 std::cout<<"1111"<<std::endl;

实际开发的项目工程:
1.指定命名空间访问
2.常用部分访问
小的程序,日常联系,不太会出现冲突:
全局展开。但一般情况下不建议全局展开。

3.C++输入和输出

cout输出,cin输入。

int main()
{
	//<<流插入,自动识别类型。
	cout << "hello world" << endl;//endl等价于换行符
	//>>流提取
	
		int n;
		//动态数组
		double* a = (double*)malloc(sizeof(double) * n);
		if (a == NULL)
		{
			perror("malloc fail");
			exit(-1);
		}
	cin >> n;
	for (int i = 0; i < n; ++i)
	{
		cout << a[i] << endl;
	}
	
	return 0;

}

C++基础(补充)_第4张图片

#include
#include 
using namespace std;
int main()
{
	//<<流插入,自动识别类型。
	cout << "hello world" << endl;//endl等价于换行符
	//>>流提取
	//可以自动识别类型
		int n=0;
		cin >> n;
		//动态数组
		double* a = (double*)malloc(sizeof(double) * n);
		if (a == NULL)
		{
			perror("malloc fail");
			exit(-1);
		}
	
	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
	}
	for (int i = 0; i < n; ++i)
	{
		cout << a[i] << endl;
	}
	
	return 0;

}

C++基础(补充)_第5张图片
C语言和C++怎么舒服怎么用。

2.缺省参数

(备胎)

  • 全缺省参数
  • 半缺省参数
void func(int a=0)
{
 cout<<a<<endl;
}
int main()
{
 func(1);
 func();
 return 0;
}//输出1和0
//全缺省
void Func(int a = 10, int b = 20, int c = 30)
{
	cout << "a= " << a << endl;
	cout << "b= " << b << endl;
	cout << "c= " << c  << endl;
	cout << endl;
}
int main()
{
	Func(1, 2, 3);
	Func(1, 2);
	Func(1);
	Func();
	return 0;
}

C++基础(补充)_第6张图片
使用缺省值必须从连续使用
//Func( ,2, );
//Func( , ,3);//都不可行
半缺省(部分缺省)
必须从右向左连续缺省

void Func(int a , int b = 20, int c = 30)
struct Stack
{
	int* a;
	int top;
	int capacity;
};
void StackInit(struct Stack* ps,int defaultCapacity=4)//缺省参数,更灵活
{
	ps->a = (int*)malloc(sizeof(int)* defaultCapacity);//写固定了
	if (ps->a == NULL)
	{
		perror("malloc fail");
		exit(-1);
	}
	ps->top = 0;
	//ps->capacity = 4;//写法不好,写的空间被固定了
	ps->capacity = defaultCapacity;//可以控制

}
int main()
{//都初始化成4不合理
	Stack st1;//存100个数据
	StackInit(&st1,100);

	Stack st2;//存数据不知道个数
	StackInit(&st2);

}

你可能感兴趣的:(c++,开发语言)