目录
1.C++的第一个程序
2.namesapce命名空间域
2.1namespace的意义
2.2.2namespace的定义
2.3命名空间的使用
3.C++输入/输出
4.缺省参数
5.函数重载
6.引用
6.1引用的特性
6.2引用的使用
1.C++的第一个程序
c++版本:
#include<iostream>using std::cout;
using std::endl;int main()
{cout << "Hello Word!" << endl;return 0;
}
此外,由于C++是兼容绝大多数c语言的语法的我们还可以用c语言的方式实现。
#include<iostream>using std::cout;
using std::endl;int main()
{//cout << "Hello Word!" << endl;printf("Hello Word!");return 0;
}
2.namesapce命名空间域
2.1namespace的意义
在c\c++中,变量、函数和后面要学到的类都是大量存在的,这些变量、函数和类的名称将都存在于全局作用域中,可能会导致很多冲突。使用命名空间的目的就是对标识符的名称进行本地化,以免命名冲突或名字污染,namespace关键字的出现就是针对这种问题。
举个例子:c语言项目类似下面程序这样的命名冲突是普遍存在的问题,c++引入namespace就是为了更好的解决这样的问题。
此时我们在c++当中引用namespace就不会有这种问题:
#include<iostream>
#include<stdlib.h>
//命名域将rand和函数rand隔开
namespace lcl
{int rand = 10;
}using namespace::std;int main()
{cout << lcl::rand << endl;return 10;
}
2.2.2namespace的定义
1.定义命名空间,需要使用到namespace关键字,后面跟命名空间的名字,然后接一对{}即可,{}中即为命名空间的成员。命名空间中可以定义变量、函数、类型等 。
2.namespace本质是定义出一个域,这个域跟全局域各自独立,不同的域可以定义同名变量,所以下面的rand就不在冲突了。
//命名域将rand和函数rand隔开
namespace lcl
{int rand = 10;int Add(int x, int y){return x + y;}struct Node{struct Node* next;int data;};
}using namespace::std;int main()
{//这里访问的是rand函数指针printf("%p\n", rand);//这里访问的时lcl命名空间中的randprintf("%d\n", lcl::rand);//cout << lcl::rand << endl;return 10;
}
3.C++中域有函数局部域、全局域、命名空间域,类域;域影响的是编译时语法查找一个变量、函数、类型出处的逻辑,所以有了域隔离,名字冲突就解决了。局部域和全局域除了会影响编译查找逻辑,还会影响变量的生命周期,命名空间域和类域都不影响变量的生命周期。
4.namespace只能定义在全局,当然它还可以嵌套定义。
#include<iostream>namespace A
{namespace lcl{int rand = 10;int Add(int x, int y){return x + y;}struct Node{struct Node* next;int data;};}namespace xyy{int rand = 2;int Add(int x, int y){return x + y;}}}using namespace std;int main()
{//使用lcl的命名空间printf("%d\n", A::lcl::rand);//使用xyy的命名空间printf("%d\n", A::xyy::rand);return 0;
}
5.项目工程中多文件定义的同名namespace会认为时一个namespace,不会冲突。
C++标准库都放在一个叫std(standard)的命名空间中。
2.3命名空间的使用
编译查找一个变量的声明/定义时,默认只会在局部或者全局查找,不会到命名空间里面去找。所以我们要使用命名空间中定义的变量、函数,有三种方式:
1.指定命名空间访问,项目中推荐 这种方式
2.using将命名空间中某个成员展开,项目中经常访问的不存在冲突的成员 推荐这种方式。
3.展开命名空间全部成员,项目不推荐,平时练习的时候可以这样。
#include<iostream>namespace lcl
{int a = 20;int b = 10;
}
//将命名空间中的a展开
using lcl::a;
using namespace std;
int main()
{cout << a << endl;cout << lcl::b << endl;return 0;
}
#include<iostream>namespace lcl
{int a = 20;int b = 10;
}
//将命名空间全部展开
using namespace lcl;
using namespace std;
int main()
{cout << a << endl;cout << b << endl;return 0;
}
3.C++输入/输出
• <iostream> 是 Input Output Stream 的缩写,是标准的输⼊、输出流库,定义了标准的输⼊、输
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;int main()
{int a = 10;double b = 1.1;char c = 'a';cout << a << " " << b << " " << c << endl;std::cout << a << " " << b << " " << c << std::endl;scanf_s("%d%lf", &a, &b);printf("%d %lf\n", a, b);//可以自动识别变量类型cin >> a >> b >> c;cout << a << " " << b << " " << c << endl;return 0;
}
#include<iostream>
using namespace std;int main()
{int a = 10;double b = 1.1;char c = 'a';//在io需求比较高的地方,如部分大量输入的竞赛题中,加上一下三行代码可以提高C++IO效率ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);//可以自动识别变量类型cin >> a >> b >> c;cout << a << " " << b << " " << c << endl;return 0;
}
4.缺省参数
#include<iostream>namespace lcl
{// 命名空间中可以定义变量/函数/类型int rand = 10;int Add(int left, int right){return left + right;}struct Node{struct Node* next;int val;};
}using std::cout;
using std::endl;//缺省参数
void Fun(int a = 10)
{cout << a << endl;
}//全缺省
void test(int a = 20, int b = 40, int c = 50)
{cout << a << endl;cout << b << endl;cout << c << endl;
}//半缺省
void test2(int a, int b = 30, int c = 40)
{cout << a << endl;cout << b << endl;cout << c << endl;
}int main()
{//缺省参数Fun();Fun(22);test();test(70);test(100, 200);test(200.300,400);test2(100);test2(100,200);test2(100,200,300);}
5.函数重载
C++支持在同一作用域中出现同名函数,但是要求这些同名函数的形参不同,可以是参数个数不同或者类型不同,这样C++函数调用就表现出了多态行为,使用更加灵活,c语言是不支持这个的。
#include<iostream>using namespace std;//1.参数类型不同
int Add(int x, int y)
{cout << "int Add(int x, int y)" << endl;return x + y;
}double Add(double x, double y)
{cout << "double Add(double x, double y)" << endl;return x + y;
}//2.参数个数不同void fun()
{cout<<"void fun()" << endl;
}void fun(int a)
{cout << "void fun(int a)" << endl;
}//3.参数类型顺序不同void f(int a, double b)
{cout << "void f(int a, double b)" << endl;
}void f(double a, int b)
{cout << "void f(double a, int b))" << endl;
}int main()
{Add(1, 2);Add(1.2, 2.2);fun();fun(10);f(10, 2.2);f(10.1, 22);return 0;
}
注意:返回值不同不能作为函数重载条件,因为调用时也无法区分
6.引用
#include<iostream>
using namespace std;//引用的使用
void swap(int& rx, int& ry)
{int tmp = rx;rx = ry;ry = tmp;
}int main()
{//引用int a = 10;//引用:b和c是a的别名int& b = a;int& c = a;//也可以给b取别名相当于还是给a取别名int& d = b;d++;cout << a << endl;cout << b << endl;cout << c << endl;cout << d << endl;return 0;
}
6.1引用的特性
1.必须初始化才能使用
2.一个变量有多个引用
3.引用一旦引用了一个实体,就不能引用其他实体
6.2引用的使用
#include<iostream>
using namespace std;//引用的使用
void swap(int& rx, int& ry)
{int tmp = rx;rx = ry;ry = tmp;
}int main()
{//引用int a = 10;//引用:b和c是a的别名int& b = a;int& c = a;//也可以给b取别名相当于还是给a取别名int& d = b;d++;cout << a << endl;cout << b << endl;cout << c << endl;cout << d << endl;//引用的使用int i = 10;int j = 30;swap(i, j);cout << i << endl;cout << j << endl;return 0;
}