赞
踩
C++中总共63个关键字,包括了C语言中32个关键字
在C/C++中,变量、函数和后面要学到的类都是大量存在的,这些变量、函数和类的名称将都存在于全局作用域中,可能会导致很多冲突。使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突或名字污染,namespace关键字的出现就是针对这种问题的。
定义命名空间,需要使用到namespace关键字,后面跟命名空间的名字,然后接一对{}即可,{}中即为命名空间的成员。
C++为了防止命名冲突,把自己库里面的东西都定义在一个std的命名空间中要使用标准库里面的东西,有三种方式
代码如下:
int c = 100;
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;
}
}
代码如下:
using namespace std;
代码如下:
using std::cout;
using std::endl;
int main()
{
printf("%d\n", N::a);
printf("%d\n", N::b);
printf("%d\n", N::Add(1, 2));
printf("%d\n", N::Sub(1, 2));
int c = 10;
printf("%d\n", c); //局部变量优先,所以c为10
printf("%d\n", ::c); //指定访问左边域,空白表示全局域
}
代码如下:
struct Person { char name[10]; int age; }; int main() { std::cout << "bit education "; std::cout << "bit education" << std::endl; //cout与cin对比C语言printf\scanf 来说可以自动识别类型(函数重载+运算符重载) int a = 10; int* p = &a; printf("%d,%p\n", a, p); std::cout << a << "," << p << std::endl; std::cin >> a; printf("%d\n", a); char str[100]; std::cin >> str; //cin不用&,因为引用 std::cout << str << std::endl; struct Person P = { "uzi", 23 }; //格式化输出printf比cout好 printf("name:%s age:%d\n", P.name, P.age); std::cout << "name:" << P.name<<" age:"<< P.age << "\n"; }
1.缺省参数是声明或定义函数时为函数的参数指定一个默认值。在调用该函数时,如果没有指定实参则采用该默认值,否则使用指定的实参
代码如下:
void TestFunc(int a = 0)
{
cout << a << endl;
}
int main()
{
TestFunc(); // 没有传参时,使用参数的默认值
TestFunc(10); // 传参时,使用指定的实参
}
代码如下:
void testFunc3(int a, int b = 10, int c = 20)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
代码如下:
void testFunc2(int a = 10, int b = 20, int c = 30)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
代码如下:
void testFunc1(int a = 0)
{
std::cout << a << std::endl;
}
int main()
{
testFunc1(10);
testFunc2();
testFunc3(1);
return 0;
}
注意:
代码如下:
int Add(int left, int right)
{
return left + right;
}
double Add(double left, double right)
{
return left + right;
}
int main()
{
cout << Add(10, 20) << endl;
cout << Add(10.5, 20.0) << endl;
//fun();
return 0;
}
注意(特别重要): 缺省参数缺省参数符合重载的定义,但如果调用的时候编译器不识别函数重载调用哪个函数,所以分情况讨论。
代码如下:
void fun(int a, int b, int c = 10)
{
}
void fun(int a, int b)
{
}
为什么C++支持函数重载,而C语言不支持函数重载呢?在C/C++中,一个程序要运行起来,需要经历以下几个阶段:预处理、编译、汇编、链接。
其中编译和链接也分为几个步骤:
其中分为更细的话:
在C++调用Add函数
在C下调用Add函数
通过这里就理解了C语言没办法支持重载,因为同名函数没办法区分。而C++是通过函数修饰规则来区分,只要参数不同,修饰出来的名字就不一样,就支持了重载。
有时候在C++工程中可能需要将某些函数按照C的风格来编译,在函数前加extern “C”,意思是告诉编译器,将该函数按照C语言规则来编译,所以这个函数不能进行重载。
代码如下:
extern "C" int Add(int left, int right);
int main()
{
Add(1, 2);
return 0;
}
代码如下:
int main()
{
int x = 10;
int &y = x;
y = 20;
std::cout << "y=" << y << std::endl;
int &z = y;
z = 30;
std::cout << "z=" << z << std::endl;
}
代码如下:
void TestConstRef()
{
//常引用是创建一个临时变量,引用名是临时变量的引用
const int a = 10;
//int& ra = a; // 该语句编译时会出错,a为常量,而且a为不可以修改
const int& ra = a;
// int& b = 10; // 该语句编译时会出错,b为常量
const int& b = 10;
double d = 12.34;
//int& rd = d; // 该语句编译时会出错,类型不同
const int& rd = d;
}
rc是临时空间的别名
代码如下:
int c=10;
double d=1.11;
const double& rc=c;
1.做参数
代码如下:
void Swap2(int& a, int& b) //通过引用来交换
{
int tmp = a;
a = b;
b = tmp;
}
void Swap1(int* a, int *b) //通过指针来交换
{
int tmp = *a;
*a = *b;
*b = tmp;
}
2.做返回值
代码如下:
int& Add(int a, int b)
{
int c = a + b;
return c;
}
int main()
{
int& ret = Add(1, 2);
Add(3, 4);
cout << "Add(1, 2) is :" << ret << endl;
return 0;
}
如果函数返回时,出了函数作用域,如果返回对象还未还给系统,则可以使用引用返回,如果已经还给系统了,则必须使用传值返回
以inline修饰的函数叫做内联函数,编译时C++编译器会在调用内联函数的地方展开,没有函数压栈的开销,内联函数提升程序运行的效率。
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> int Add2(int left, int right) { return left + right; } inline int Add1(int left, int right) { return left + right; } int main() { int ret1, ret2; ret1 = Add1(1, 2); ret2 = Add1(1, 2); std::cout << ret1 << std::endl; std::cout << ret2 << std::endl; return 0; }
代码如下:
int main() { int x = 10; auto a = &x; // int* auto* b = &x; // int* int& y = x; // y的类型是什么?int auto c = y; // int auto& d = x; // d的类型是int, 但是这里指定了d是x的引用 // 打印变量的类型 cout << typeid(x).name() << endl; cout << typeid(y).name() << endl; cout << typeid(a).name() << endl; cout << typeid(b).name() << endl; cout << typeid(c).name() << endl; cout << typeid(d).name() << endl; return 0; }
代码如下:
// 此处代码编译失败,auto不能作为形参类型,因为编译器无法对a的实际类型进行推导
void TestAuto(auto a)
{}
代码如下:
void TestAuto()
{
int a[] = {1,2,3};
auto b[] = {4,5,6};
}
代码如下:
int main()
{
int array[] = { 1, 2, 3, 4, 5 };
for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)
{
cout << array[i] << " ";
}
cout << endl;
}
return 0;
}
代码如下:
int main()
{
// 范围for C++11新语法遍历,更简单,数组都可以
// 自动遍历,依次取出array中的元素,赋值给e,直到结束
for (auto& e : array)
{
e *= 2;
}
for (auto ee : array)
{
cout << ee << " ";
}
cout << endl;
}
1.程序本意是想通过f(NULL)调用指针版本的f(int*)函数,但是由于NULL被定义成0,因此与程序的初衷相悖。
2.在C++98中,字面常量0既可以是一个整形数字,也可以是无类型的指针(void*)常量,但是编译器默认情况下
3.将其看成是一个整形常量,如果要将其按照指针方式来使用,必须对其进行强转(void *)0。
以上就是今天要讲的内容,本文仅仅简单介绍了C++入门的简单知识,虽然这么知识范围很大,但也为我们以后学习C++有更好的了解,我们务必掌握。另外如果上述有任何问题,请懂哥指教,不过没关系,主要是自己能坚持,更希望有一起学习的同学可以帮我指正,但是如果可以请温柔一点跟我讲,爱与和平是永远的主题,爱各位了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。