-
面向对象和面向过程
面向过程:以过程为中心的编程思想,通过分析出解决问题所需要的步骤,然后用函数把步骤一步一步实现。
面向过程核心:功能分解,自顶向下,逐层细化(程序=数据结构+算法)。
面向过程缺点:语言重用性低,维护困难。面向对象:以对象为中心的编程思想,将每个模块对象化只提供特定的功能,每个对象彼此独立。
面向对象核心:对象=算法+数据结构,程序=对象+对象…
面向对象缺点:程序复杂,运行效率相对低。 -
对象的三大特性
封装:把客官事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类(派生类)或者对象操作(友元函数),对不可信的进行信息隐藏。
继承:类之间相关的关系,这种关系使得对象可以继承另外一类对象的特征和能力。(该特性可以避免公用代码的重复开发,减少代码和数据冗余)。
多态:“一个接口,多种方法”,程序在运行时才决定调用的函数,它是面向对象编程领域的核心概念。(virtual) -
命名空间
3.1 命名空间的创建
3.2 命名空间只能在全局范围内定义
3.3 命名空间可以嵌套命名空间
3.4 命名空间中函数的声明和实现可以分开
3.5 无名命名空间相当于全局范围的static,只能本文件访问
3.6 命名空间的别名,可在全局和局部范围内变更命名空间名称
3.7 using声明的使用方法
3.8 二义性的产生
#include<iostream>
using namespace std;
//3.1 创建命名空间
namespace namespaceA {
int a = 10;
}
namespace namespaceB {
int a = 20;
}
void namespace_test_3_1()
{
cout << "3.1" << endl;
cout << namespaceA::a << endl;
cout << namespaceB::a << endl;
}
//3.2 命名空间只能在全局范围内定义
//void namespace_test_3_2()
//{
// namespace namespaceC {
// int a = 10;
// }
// cout << "3.2" << endl;
// cout << namespaceC::a << endl;
//}
//3.3 命名空间可以嵌套命名空间
namespace namespaceD {
int a = 10;
namespace namespaceE {
int a = 10;
}
}
void namespace_test_3_3()
{
cout << "3.3" << endl;
cout << namespaceD::a << endl;
cout << namespaceD::namespaceE::a << endl;
}
//3.4 声明和时间可分离
namespace namespaceF {
int a = 10;
int func1();
int func2(int i);
}
int namespaceF::func1()
{
return 3.4;
}
int namespaceF::func2(int i)
{
return i;
}
void namespace_test_3_4()
{
cout << "3.4" << endl;
cout << namespaceF::func1() << endl;
cout << namespaceF::func2(5) << endl;
}
//3.5无名命名空间,只有本文件内可访问,相当于给这个标识符加上了static,使得其可以作为内部链接
namespace {
int a = 10;
void func() { cout << "namespace,3.5" << endl; };
}
void namespace_test_3_5()
{
cout << "3.5" << endl;
cout << a << endl;
func();
}
//3.6 命名空间的别名
namespace namespaceG = namespaceA;
void namespace_test_3_6()
{
cout << "3.6" << endl;
cout << namespaceG::a << endl;
cout << namespaceG::a << endl;
}
//3.7 using声明
namespace namespaceH {
int h = 10;
void funch() { cout << "namespace,3.5" << endl; };
void funch(int i) { cout << "namespace,3.5" << i<< endl; };
}
void namespace_test_3_7()
{
using namespace namespaceH;
//或者 using namespaceH::h;
// using namespaceH::funch();
cout << "3.7" << endl;
cout << h << endl;
funch();
funch(3.7);
}
//3.8 二义性的产生
//void namespace_test_3_8()
//{
// using namespace namespaceA;
// using namespace namespaceB;
// cout << "3.8" << endl;
// cout << a << endl; //此时变量a产生二义性,不知是调用namespaceA还是调用namespaceB
//
//}
void main()
{
namespace_test_3_1(); //3.1 命名空间的创建
//namespace_test_3_2(); //3.2 命名空间只能在全局范围内定义
namespace_test_3_3(); //3.3 命名空间可以嵌套命名空间
namespace_test_3_4(); //3.4 命名空间中函数的声明和实现可以分开
namespace_test_3_5(); //3.5 无名命名空间相当于全局范围的static,只能本文件访问
namespace_test_3_6(); //3.6 命名空间的别名,可在全局和局部范围内变更命名空间名称
namespace_test_3_7(); //3.7 using声明的使用方法
//namespace_test_3_8(); //3.8 二义性的产生
}