学习目标:

掌握C++的基础知识


学习内容:

1.C++关键字

C++总计63个关键字:这里不需要知道每个关键字的含义,但需要了解C++的关键字具体都有什么。

asm

do

if

return

try

continue

auto

double

inline

short

typedef

for

bool

dynamic_cast

int

signed

typeid

public

break

else

long

sizeof

typename

throw

case

enum

mutable

static

union

wchar_t

catch

explicit

namespace

static_cast

unsigned

default

char

export

new

struct

using

friend

class

extern

operator

switch

virtual

register

const

false

private

template

void

true

const_cast

float

protected

this

volatile

while

delete

goto

reinterpret_cast


2.命名空间

2.1为什么会有命名空间?

主要原因是为了解决C语言中的命名冲突。举个例子:在进入公司项目组以后,做的项目通常比较大,项目会由多人协作,那么有可能两个同事写的代码,会出现命名冲突的问题;其次我们自己定义的变量、函数可能跟库里面重名冲突。因此C++里提出了一个新语法:命名空间。

2.2命名空间定义

定义命名空间,需要使用namespace关键字,后面跟命名空间的名字,然后接一对{}即可,{}中即为命名空间成员。比如:

//1.普通的命名空间
namespace N1 //N1为命名空间的名称
{
//命名空间的内容,既可以定义变量,也可以定义函数
int a;
int Add(int left, int right)
  {
   return left+right;
  }
}
//2.命名空间可以嵌套
namespace N2
{
 int a;
 int b;
 int Add(int left, int right)
 {
 return left + right;
 }
 namespace N3
 {
 int c;
 int d;
 int Sub(int left, int right)
 {
 return left - right;
 }
 }
}
//3.同一个工程中允许存在多个相同名称的命名空间,编译器最后会合成同一个命名空间。
namespace N1
{
int Mul(int left, int right)
 {
 return left * right;
 }
}
//注意:一个命名空间就定义了一个新的作用域,命名空间中的所有内容都局限于该命名空间中。

2.3命名空间使用

假设我们定义了一个命名空间:

namespace N
{
	int a = 10;
	int b = 20;
	int Add(int left, int right)
	{
		return left + right;
	}
	int Sub(int left, int right)
	{
		return left - right;
	}
}

命名空间有三种使用方式:

  1. 加命名空间名称及作用域限定符
int main()
{
	printf("%d\n", N::a);
	return 0;
}

第一种命名空间使用方法结果

  1. 使用using将命名空间成员引入
using N::b;
int main()
{
	printf("%d\n", N::a);
	printf("%d\n", b);
	return 0;
}

第二种命名空间使用方法结果图

  1. 使用using namespace命名空间名称引入
using namespace N;
int main()
{
	printf("%d\n", a);
	printf("%d\n", b);
	int c=Add(10, 20);
	int d=Sub(20, 10);
	printf("%d\n", c);
	printf("%d\n", d);
	return 0;
}

第三种命名空间使用方法结果图

3.C++输入&输出

3.1 输入输出流

输入输出是数据传送的过程,C++中将此过程形象的称为流,C++中输入输出流是指由若干字节组成的序列,这些字节序列中的数据按顺序从一个对象传送到另一个对象。在输入操作时,字节流从输入设备流向内存;在输出操作时,字节流从内存流向输出设备。流中的内容可以是ASCII码值、二进制形式数据、数字音频视频、图形图像或者其他形式的信息。

3.2 C++中的输入输出语句

  1. 标准输入设备
    cin为标准输入设备(相当于键盘),连续从键盘中提取数据,">>"为提取运算符。
    使用方法:cin>>变量 (cin在输入字符串时,空格作为结束符)
  2. 标准输出设备
    cout为标准输出设备(相当于屏幕),“<<”为插入运算符。
    使用方法:cout<<输出项<<endl; (endl相当于换行符\n)
  3. 输入输出示例:
#include <iostream>
using namespace std;
int main()
{
	int a;
	char ch;
	cout << "输入一个常数" << endl;
	cin >> a;
	cout << "输入一个字母" << endl;
	cin >> ch;
	cout << "常数为" << a << endl;
	cout << "字母为" << ch << endl;
	return 0;
}

4.缺省参数

4.1 缺省参数概念

缺省参数是声明或定义函数时为函数的参数指定一个默认值。在调用该函数时,如果没有指定实参则采用该默认值,否则使用指定的实参。

#include <iostream>
using namespace std;
void Test(int a = 0)
{
	cout << a << endl;
}
int main()
{
	Test();   //没有传参时,使用参数的默认值
	Test(10); //传参时,使用指定的实参
	return 0;
}

4.2 缺省参数分类

  1. 全缺省参数
#include <iostream>
using namespace std;
void TestFunc(int a = 10, int b = 20, int c = 30)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl<< endl;
}
int main()
{
	TestFunc();
	TestFunc(1);
	TestFunc(1,2);
	TestFunc(1,2,3);
	return 0;
}

  1. 半缺省参数
#include <iostream>
using namespace std;
void TestFunc(int a , int b = 20, int c = 30)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl<< endl;
}
int main()
{
	TestFunc();
	TestFunc(1);
	TestFunc(1,2);
	TestFunc(1,2,3);
	return 0;
}


注意:1.半缺省参数必须从右往左依次来给出,不能间隔着给。

2.缺省参数不能在函数声明和定义中同时出现。

3.缺省值必须是常量或者全局变量。

4.C语言不支持半缺省参数函数。

5.函数重载

5.1 函数重载的概念

函数重载:是函数的一种特殊情况,C++允许在同一作用域中声明几个功能类似的同名函数,这些同名函数的形参列表(参数个数 或 类型 或 顺序)必须不同,常用来处理实现功能类似数据类型不同的问题.
1.参数类型不同,以下函数构成函数重载。

#include <iostream>
using namespace std;
int Add(int left, int right)
{
	return left + right;
}
double Add(double left, double right)
{
	return left + right;
}
long Add(long left, long right)
{
	return left + right;
}
int main()
{
	int a=Add(10, 20);
	double b=Add(1.2, 2.2);
	long c=Add(10L, 20L);
	cout << a << " " << b << " " << c << endl;
	return 0;
}


2.参数个数不同,以下函数构成函数重载。

#include <iostream>
using namespace std;
void Func(int x)
{
	cout << "Func(int x)" << endl;
}
void Func(char ch, int x)
{
	cout << "Func(char ch,int x)" << endl;
}
int main()
{
	Func(100);
	Func('T', 200);
	return 0;
}

DECLARE result i declare result int_命名空间


3.参数顺序不同,以下函数构成函数重载。

#include <iostream>
using namespace std;
void Func(int x,char y)
{
	cout << "Func(int x,char y)" << endl;
}
void Func(char y, int x)
{
	cout << "Func(char y,int x)" << endl;
}
int main()
{
	Func(100,'T');
	Func('T', 100);
	return 0;
}

5.2 不支持函数重载的情况

1.返回值不同,调用时无法区分:

#include <iostream>
using namespace std;
int f(double a)
{
	;
}
void f(double a)
{
	;
}
int main()
{
	f(2.1);//无法区分进哪一个f函数
	return 0;
}


2. 缺省值不同,不能构成重载:

#include <iostream>
using namespace std;
void f(int a)
{
	cout << "f(int a)" << endl;
}
void f(int a = 10)
{
	cout << "f(int a)" << endl;
}
int main()
{
	f(100);
	return 0;
}

DECLARE result i declare result int_c++_02


3. 可以构成重载但存在歧义,并且使用时会出现问题:

#include <iostream>
using namespace std;
void Func()
{
	cout << "Func()" << endl;
}
void Func(int a = 1)
{
	cout << "Func(int a)" << endl;
}
int main()
{
	Func();//存在歧义,不知道调用无参的,还是缺省的
	Func(2);//能够调用
	return 0;
}


参考资料:百度百科[EB/OL]. []. https://baike.baidu.com/.