#include<iostream>
using namespace std;
//命名空间 namespace
namespace N1
{
int a = 10;
int Add(int left, int right)
{
return left + right;
}
}
//命名空间可以嵌套
namespace N2
{
int a = 20;
int b = 30;
int Add(int left, int right)
{
return left + right;
}
namespace N3
{
int a = 40;
int d = 50;
int Sub(int left, int right)
{
return left - right;
}
}
}
#if 0
int main()
{
cout << N1 :: a << endl;
cout << N2 :: a << endl;
cout << N2 :: b << endl;
cout << N2 :: N3 :: a << endl;
cout << N2 :: N3 :: d << endl;
cout << N1 :: Add(10, 20) << endl;
cout << N2 :: Add(10, 20) << endl;
cout << N2 :: N3 :: Sub(10, 20) << endl;
system("pause");
return 0;
}
#endif
//using N1:: a;
//using namespace N2;
//int main()
//{
// //cout << a << endl;
// cout << N1 :: a << endl;
// cout << b << endl;
//
// system("pause");
// return 0;
//}
//C++的输入输出流
#if 0
int main()
{
int a;
double b;
char c;
cin >> a;
cin >> b >> c;
cout << a << endl;
cout << b << "\n" << c << endl;
system("pause");
return 0;
}
#endif
#if 0
//缺省参数
void TestFunc(int a = 0)
{
cout << a << endl;
}
//全缺省
void TestFunc1(int a = 10, int b = 20, int c = 30)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
//半缺省
void TestFunc2(int a, int b = 50, int c = 40)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
int main()
{
//TestFunc();
//TestFunc(10);
//TestFunc1();
//TestFunc1(2, 4, 8);
//TestFunc1(4, 8);
TestFunc2(1, 3, 5);
TestFunc2(2);
system("pause");
return 0;
}
#endif
#if 0
//函数重载
int Add(int left, int right)
{
return left + right;
}
int Add(int a, int b, int c)
{
return a + b + c;
}
double Add(double left, double right)
{
return left + right;
}
int main()
{
cout << Add(1, 2) << endl;
cout << Add(1, 2, 3) << endl;
cout << Add(1.5, 2.2) << endl;
system("pause");
return 0;
}
#endif
//extern "c"
#if 0
extern "c" int Add(int left, int right)
{
return left + right;
}
int main()
{
Add(1, 2);
system("pause");
return 0;
}
#endif
#if 0
//函数参数缺省不缺省不能形成函数重载
void TestFunc(int a = 0)
{
cout << "void TestFunc(int = 0)" << endl;
}
void TestFunc(int a)
{
cout << "void TestFunc(int)" << endl;
}
int main()
{
TestFunc();
TestFunc(10);
system("pause");
return 0;
}
#endif
#if 0
//引用
//引用可以做参数
void Swap(int& left, int& right)
{
int temp = left;
left = right;
right = temp;
}
//引用做返回值 注意:如果函数返回时,离开函数作用域后,
//其栈上空间已经还给系统,因此不能用栈上的空间作为引用类型 返回。
//如果以引用类型返回,返回值的生命周期必须不受函数的限制(即比函数生命周期长)。
int& TestFunc(int &a)
{
a += 10;
return a;
}
int& Add(int a, int b)
{
int c = a + b;
return c;
}
int main()
{
//int a = 10;
//int &b = a;
//int &c = b;
//int &d = a;
//cout << a << "\n" << b << endl;
//cout << &a << "\n" << &b << endl;
//cout << c << "\n" << &c << endl;
//cout << d << "\n" << &d << endl;
常引用
//const int a = 10;
int &ra = a; //编译时报错,a为常量
//const int &ra = a;
int &rb = 20; //编译时报错,20为常量
//const int &rb = 20;
//double c = 12.34;
int &rc = c; //编译时报错,类型不同
//double &rc = c;
int a = 1, b = 3;
Swap(a, b);
cout << a << "\n" << b << endl;
int c = 1;
cout << TestFunc(c) << endl;
int &ret = Add(1, 2);
cout << "Add(1, 2) is :" << ret << endl;
Add(3, 4);
cout << "Add(1, 2) is :" << ret << endl;
system("pause");
return 0;
}
//引用和指针的区别:
//1. 引用在定义时必须初始化,指针没有要求
//2. 引用在初始化时引用一个实体后,就不能再引用其他实体,而指针可以在任何时候指向任何一个同类型实 体
//3. 没有NULL引用,但有NULL指针
//4. 在sizeof中含义不同:引用结果为引用类型的大小,但指针始终是地址空间所占字节个数(32位平台下占4 个字节)
//5. 引用自加即引用的实体增加1,指针自加即指针向后偏移一个类型的大小
//6. 有多级指针,但是没有多级引用
//7. 访问实体方式不同,指针需要显式解引用,引用编译器自己处理
//8. 引用比指针使用起来相对更安
//9. 在语法上不占空间,指针占有空间
//9. 引用和指针的底层实现是一样的
#endif
//内联函数:以inline修饰的函数叫做内联函数,编译时C++编译器会在调用内联函数的地方展开,没有函数压栈的开销, 内联函数提升程序运行的效率。
//如果在上述函数前增加inline关键字将其改成内联函数,在编译期间编译器会用函数体替换函数的调用。
//查看方式:
//1. 在release模式下,查看编译器生成的汇编代码中是否存在call Add
//2. 在debug模式下,需要对编译器进行设置,否则不会展开
//1. inline是一种以空间换时间的做法,省去调用函数额开销。所以代码很长或者有循环 / 递归的函数不适宜使 用作为内联函数。
//2. inline对于编译器而言只是一个建议,编译器会自动优化,如果定义为inline的函数体内有循环 / 递归等 等,编译器优化时会忽略掉内联。
//3. inline不建议声明和定义分离,分离会导致链接错误。因为inline被展开,就没有函数地址了,链接就会找 不到。
//宏的优缺点?
//优点:
//1.增强代码的复用性。
//2.提高性能。
//缺点:
//1.不方便调试宏。(因为预编译阶段进行了替换)
//2.导致代码可读性差,可维护性差,容易误用。
//3.没有类型安全的检查 。
//C++有哪些技术替代宏?
//1. 常量定义 换用const
//2. 函数定义 换用内联函数
#if 0
//auto关键字(C++11)
int TestAuto()
{
return 10;
}
int main()
{
//int a = 10;
//auto b = a;
//auto c = 'a';
//auto d = TestAuto();
//cout << typeid(b).name() << endl;
//cout << typeid(c).name() << endl;
//cout << typeid(d).name() << endl;
//auto e; //无法通过编译,使用auto定义变量时必须对其进行初始化
int x = 10;
auto a = &x;
auto *b = &x;
auto& c = x;
cout << typeid(a).name() << endl;
cout << typeid(b).name() << endl;
cout << typeid(c).name() << endl;
auto a = 1, b = 2;
//auto c = 3, d = 4.0; // 该行代码会编译失败,因为c和d的初始化表达式类型不同
system("pause");
return 0;
}
//1. auto不能作为函数的参数
//2. auto不能直接用来声明数组
//3. 为了避免与C++98中的auto发生混淆,C++11只保留了auto作为类型指示符的用法
//4. auto在实际中常见的优势用法就是跟以后会讲到的C++11提供的新式for循环,还有lambda表达式等进 行配合使用。
//5. auto不能定义类的非静态成员变量(暂不做讲解,后面讲)
//6. 实例化模板时不能使用auto作为模板参数
#endif
#if 0
//nullptr 的类型为 nullptr_t
int main()
{
int* a = nullptr;
int *b = 0;
int *c = NULL;
nullptr_t d;
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << typeid(d).name() << endl;
system("pause");
return 0;
}
#endif
C++入门(2)
最新推荐文章于 2023-08-15 11:56:51 发布