diff --git a/README.md b/README.md index b70efaf..6e8ddf5 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ - [x] [c实现c++多态那些事](./c_poly) - [x] [explicit那些事](./explicit) - [x] [friend那些事](./friend) +- [x] [using那些事](./using) 代码运行: 全部在linux下用vim编写,使用gcc/g++调试!全部可正常运行! diff --git a/c_poly/README.md b/c_poly/README.md index 790eebe..ed91220 100644 --- a/c_poly/README.md +++ b/c_poly/README.md @@ -1,4 +1,11 @@ # C实现C++的面向对象特性 + +## 关于作者: + +个人公众号: + +![](../img/wechat.jpg) + ## 1.C++实现案例 C++中的多态:在C++中会维护一张虚函数表,根据赋值兼容规则,我们知道父类的指针或者引用是可以指向子类对象的。 diff --git a/explicit/README.md b/explicit/README.md index 05dda6f..740b7b0 100644 --- a/explicit/README.md +++ b/explicit/README.md @@ -1,5 +1,11 @@ # explicit(显式)关键字那些事 +## 关于作者: + +个人公众号: + +![](../img/wechat.jpg) + - explicit 修饰构造函数时,可以防止隐式转换和复制初始化 - explicit 修饰转换函数时,可以防止隐式转换,但按语境转换除外 diff --git a/explicit/explict b/explicit/explicit similarity index 75% rename from explicit/explict rename to explicit/explicit index 888e363..ab3a412 100755 Binary files a/explicit/explict and b/explicit/explicit differ diff --git a/explicit/explict.cpp b/explicit/explicit.cpp similarity index 100% rename from explicit/explict.cpp rename to explicit/explicit.cpp diff --git a/friend/README.md b/friend/README.md index 768b95c..64ba791 100644 --- a/friend/README.md +++ b/friend/README.md @@ -1,4 +1,11 @@ # 友元函数与友元类 + +## 关于作者: + +个人公众号: + +![](../img/wechat.jpg) + ## 0.概述 友元提供了一种 普通函数或者类成员函数 访问另一个类中的私有或保护成员 的机制。也就是说有两种形式的友元: @@ -11,6 +18,13 @@ 缺点:破坏了类的封装性和数据的透明性。 +总结: +- 能访问私有成员 +- 破坏封装性 +- 友元关系不可传递 +- 友元关系的单向性 +- 友元声明的形式及数量不受限制 + ## 1.友元函数 在类声明的任何区域中声明,而定义则在类的外部。 diff --git a/union/README.md b/union/README.md index 5811fb9..5421d42 100644 --- a/union/README.md +++ b/union/README.md @@ -1,5 +1,11 @@ # UNION那些事 +## 关于作者: + +个人公众号: + +![](../img/wechat.jpg) + 联合(union)是一种节省空间的特殊的类,一个 union 可以有多个数据成员,但是在任意时刻只有一个数据成员可以有值。当某个成员被赋值后其他成员变为未定义状态。联合有如下特点: - 默认访问控制符为 public diff --git a/using/README.md b/using/README.md new file mode 100644 index 0000000..1e50179 --- /dev/null +++ b/using/README.md @@ -0,0 +1,120 @@ +# using那些事 +## 关于作者: + +个人公众号: + +![](../img/wechat.jpg) + +## 基本使用 + +局部与全局using,具体操作与使用见下面案例: + +```c++ +#include +#define isNs1 1 +//#define isGlobal 2 +using namespace std; +void func() +{ + cout<<"::func"< +using namespace std; + +class Base{ + public: + void f(){ cout<<"f()"< V1; +using V2 = vector; +``` +完整代码见:[using_typedef.cpp](using_typedef.cpp) diff --git a/using/derived_base b/using/derived_base new file mode 100755 index 0000000..952568a Binary files /dev/null and b/using/derived_base differ diff --git a/using/derived_base.cpp b/using/derived_base.cpp new file mode 100644 index 0000000..b25ec55 --- /dev/null +++ b/using/derived_base.cpp @@ -0,0 +1,48 @@ +#include + +using namespace std; + +class Base1 { + public: + Base1():value(10) {} + virtual ~Base1() {} + void test1() { cout << "Base test1..." << endl; } + protected: // 保护 + int value; +}; +// 默认为私有继承 +class Derived1 : Base1 { + public: + void test2() { cout << "value is " << value << endl; } +}; + +class Base { + public: + Base():value(20) {} + virtual ~Base() {} + void test1() { cout << "Base test1..." << endl; } + protected: //私有 + int value; +}; + +/** + * 子类对父类成员的访问权限跟如何继承没有任何关系, + * “子类可以访问父类的public和protected成员,不可以访问父类的private成员”——这句话对任何一种继承都是成立的。 + * + */ +class Derived : Base { + public: + using Base::value; + void test2() { cout << "value is " << value << endl; } +}; + + +int main() +{ + Derived1 d1; + d1.test2(); + + Derived d; + d.test2(); + return 0; +} diff --git a/using/using_derived b/using/using_derived new file mode 100755 index 0000000..641ac8b Binary files /dev/null and b/using/using_derived differ diff --git a/using/using_derived.cpp b/using/using_derived.cpp new file mode 100644 index 0000000..eb7b5ae --- /dev/null +++ b/using/using_derived.cpp @@ -0,0 +1,36 @@ +/** + * @file using_derived.cpp + * @brief 函数重装 + * @author 光城 + * @version v1 + * @date 2019-08-07 + */ + +#include +using namespace std; + +class Base{ + public: + void f(){ cout<<"f()"< +#define isNs1 1 +//#define isGlobal 2 +using namespace std; +void func() +{ + cout<<"::func"< +#include +using namespace std; + +typedef vector V1; +using V2 = vector; + + +int main() +{ + int nums1[] = {1,2,3,4,5,6}; + V1 vec1(nums1,nums1+sizeof(nums1)/sizeof(int)); + int nums2[] = {5,7,6}; + V2 vec2(nums2,nums2+sizeof(nums2)/sizeof(int)); + + for(auto i:vec1) + cout<