C++入门基础(上)

0 阅读7分钟
前言

笔者今天开始学习C++语法,一切源代码都被我放在我的github和gitee上了,读者可以去专栏介绍自取。

1.第一个C++程序

首先,我们需要先完成一个最简单的C++程序,也就是最基本的hello world:

#include<stdio.h>
int main()
{
	printf("hello world");
	return 0;
}

当然,上面的代码其实是C语言的代码,其实C++兼容绝大多数C语言的语法,因此直接用C语言的语法在C++亦可,下面笔者给大家看一下C++单独的语法形式.

#include<iostream>
using namespace std;
int main()
{
	cout << "hello world" << endl;
	return 0;
}

2.命名空间

<1>namespace的价值

首先,我们先看一个例子:

#include <stdio.h>
#include <stdlib.h>
int rand = 10;
int main()
{
	printf("%d\n", rand);
	return 0;
}

运行之后不难发现一个报错:

image.png
其实原因很简单,rand在stdlib库中是一个函数,然而我又把rand定义成了一个全局变量,因此会造成冲突,所以为了解决这个问题,C++引入了namespace。

<2>namespace的定义

  • namespace是一个关键字,定义命名空间时会用到,后面跟着变量名字,然后接{}即可,命名空间可以定义变量函数类型等
namespace han
{
	int rand = 10;
}
  • namespace本质是定义一个域,这个域和全局域各自独立,因此不同的域可以定义相同的变量名,rand就不再冲突了
#include<stdio.h>
#include<stdlib.h>
namespace han
{
	int rand = 10;
}
int main()
{
	printf("%d\n", han::rand);//::是域作用限定符
	printf("%p\n", rand);
	return 0;
}
  • C++中域有函数局部域,全局域,命名空间域,类域;域影响的是编译时语法查找⼀个变量/函数/类型出处(声明或定义)的逻辑,所有有了域隔离,名字冲突就解决了。局部域和全局域除了会影响编译查找逻辑,还会影响变量的⽣命周期,命名空间域和类域不影响变量⽣命周期
namespace han
{
	int rand = 10;

	int Add(int left, int right)
	{
		return left + right;
	}

	struct Node
	{
		struct Node* next;
		int val;
	};
}

int main()
{
	//printf("%d\n", Add(1, 1));
	printf("%d\n", han::Add(1, 1));
	//struct Node p1;
	struct han::Node p1;
	return 0;
}
  • namespace只能定义在全局,当然他还可以嵌套定义
namespace han
{
	// 
	namespace han01
	{
		int rand = 1;
		int Add(int left, int right)
		{
		return left + right;
		}
	}

	// 
	namespace han02
	{
		int rand = 2;
		int Add(int left, int right)
		{
			return (left + right) * 10;
		}
	}
}

int main()
{
	printf("%d\n", han::han01::rand);
	printf("%d\n", han::han02::rand);
	return 0;
}
  • 项⽬⼯程中多⽂件中定义的同名namespace会认为是⼀个namespace,不会冲突
#include"Stack.h"
// 全局定义了一份单独的Stack
typedef struct Stack
{
	int a[10];
	int top;
}ST;
void STInit(ST* ps) {}
void STPush(ST* ps, int x) {}

int main()
{
	// 调用全局的
	ST st1;
	STInit(&st1);
	STPush(&st1, 1);
	STPush(&st1, 2);
	printf("%d\n", sizeof(st1));

	// 调用han namespace的
	han::ST st2;
	printf("%d\n", sizeof(st2));
	han::STInit(&st2, 4);
	han::STPush(&st2, 1);
	han::STPush(&st2, 2);

	return 0;
}
  • C++标准库都放在⼀个叫std(standard)的命名空间中

<3>命名空间的使用

编译查找一个变量的声明/定义时,默认只会在局部或者全局查找,不会到命名空间找,因此使用命名空间中的函数/变量需要选择如下三种方法:

  • 指定命名空间访问(项目推荐)
  • using将命名空间中某个成员展开
  • 展开命名空间全部成员(项目不推荐)
#include <stdio.h>//展开头文件,和展开命名空间不一样
namespace han
{
	int a = 0;
	int b = 1;
}
using namespace han;//全部展开
using han::a;//某个成员展开
int main()
{
	printf("%d\n", a);
	printf("%d\n", a);
	printf("%d\n", a);
	printf("%d\n", a);
	printf("%d\n", a);
	printf("%d\n", b);
	printf("%d\n", han::b);
	return 0;
}

3.C++输入&输出

  • iostream是Input Output Stream缩写,是标准输入输出流库
  • std::cin 是 istream 类的对象,面向输入流
  • std::cout 是 ostream 类的对象,面向输出流
  • <<是流插⼊运算符,>>是流提取运算符
  • C++输入输出可以自动识别变量类型(本质通过函数重载实现,后面会学)
  • IO流涉及类和对象,运算符重载、继承等很多⾯向对象的知识,在此处我们只能简单认识
  • cout/cin/endl等都属于C++标准库,C++标准库都放在⼀个叫std(standard)的命名空间中,所以要通过命名空间的使用方式去用他们
  • 这⾥我们没有包含<stdio.h>,也可以使⽤printf和scanf,在包含iostream间接包含了。vs系列编译器是这样的,其他编译器可能会报错。
#include<iostream>
using namespace std;

int main()
{
	/*int i = 1234;
	int j = -1234;
	std::cout << i << endl;
	cout << i << endl;*/

	int a = 0;
	double b = 0.1;
	char c = 'x';

	cout << a << "             " << b << " " << c << "\n" << '\n' << endl;
	std::cout << a << " " << b << " " << c << std::endl;

	scanf("%d%lf", &a, &b);
	printf("%d %lf\n", a, b);

	// 可以自动识别变量的类型
	/*cin >> a;
	cin >> b >> c;*/
	cin >>a>> b >> c;
	cout << a << endl;
	cout << b << " " << c << endl;
	return 0;
}
int main()
{
	double d = 2.22222222;
	printf("%.2lf\n", d);
	cout << d << endl;
	cout << &d << endl;

	return 0;
}

4.缺省参数

  • 缺省参数就是声明定义函数时为函数的参数指定了一个值,这个值就是缺省值(默认值),在调用函数时,如果没有指定实参,则采用形参的缺省值
void Func(int a = 0)
{
	cout << a << endl;
}
  • 全缺省就是全部形参给缺省值,半缺省就是部分形参给缺省值。C++规定半缺省参数必须从右往左依次连续缺省,不能间隔跳跃给缺省值。
#include<iostream>
using namespace std;
void Func(int a=0)
{
	cout << a << endl;
}
// 全缺省
void Func1(int a = 10, int b = 20, int c = 30)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl << endl;
}

// 半缺省
void Func2(int a, int b = 10, int c = 20)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl << endl;
}

int main()
{
	Func(); // 没有传参时,使用参数的默认值
	Func(10); // 传参时,使用指定的实参

	Func1();
	Func1(1);
	Func1(1, 2);
	Func1(1, 2, 3);

	//Func2();
	Func2(100);
	Func2(100, 200);
	Func2(100, 200, 300);

	return 0;
}
  • 带缺省参数的函数调⽤,C++规定必须从左到右依次给实参,不能跳跃给实参。
  • 函数声明和定义分离时,缺省参数不能在函数声明和定义中同时出现,规定必须函数声明给缺省值。
#include<iostream>
using namespace std;
#include"Stack.h"
int main()
{
	han::ST s1;
	han::STInit(&s1);

	// 确定知道要插入1000个数据,初始化时一把开好,避免扩容
	han::ST s2;
	han::STInit(&s2, 1000);

	for (size_t i = 0; i < 1000; i++)
	{
		han::STPush(&s2, i);
	}

	return 0;
}

5.函数重载

函数重载是C++规定的一个小语法,就是支持在同一作用域中出现同名函数,但是要求同名函数的形参不同,其中可以是参数个数不同,也可以是参数类型不同,这样C++就表现出了多态行为,使用更灵活

// 1、参数类型不同
int Add(int left, int right)
{
	cout << "int Add(int left, int right)" << endl;
	return left + right;
}

double Add(double left, double right)
{
	cout << "double Add(double left, double right)" << endl;
	return left + right;
}

void Swap(int* px, int* py)
{}

void Swap(double* px, double* py)
{}

// 2、参数个数不同
void f()
{
	cout << "f()" << endl;
}
void f(int a)
{
	cout << "f(int a)" << endl;
}

// 3、参数类型顺序不同(本质类型不同)
void f(int a, char b)
{
	cout << "f(int a,char b)" << endl;
}

void f(char b, int a)
{
	cout << "f(char b, int a)" << endl;
}

- 注:下面是特殊情况

// 下面两个函数构成重载
// f()但是调用时,会报错,存在歧义,编译器不知道调用谁
//void f1()
//{
//	cout << "f()" << endl;
//}
void f1(int a = 10)
{
	cout << "f(int a)" << endl;
}
//解决方法,此时不是函数重载
namespace han
{
	void f1()
	{
		cout << "f()" << endl;
	}
}
// //返回值不同不能作为重载条件,因为调用时也无法区分
//void fxx()
//{}
//
//int fxx()
//{
//	return 0;
//}
结语

本文着重介绍了一些C++语法,读者需注意其与C语言的异同,当然本文仅仅是C++入门基础的上篇,读者可以关注作者后续内容。