From 3d44c8af081b88e1ef2111d2bd5c0913e113a08c Mon Sep 17 00:00:00 2001 From: light-city <455954986@qq.com> Date: Tue, 6 Aug 2019 13:38:00 +0800 Subject: [PATCH] update dir --- README.md | 2 ++ c_poly/README.md | 32 +++++++++++++++++++++++++ c_poly/c++_examp | Bin 0 -> 8720 bytes c_poly/c++_examp.cpp | 42 +++++++++++++++++++++++++++++++++ c_poly/c_examp | Bin 0 -> 8720 bytes c_poly/c_examp.c | 55 +++++++++++++++++++++++++++++++++++++++++++ union/README.md | 12 ++++++++++ union/union | Bin 0 -> 9704 bytes union/union.cpp | 50 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 193 insertions(+) create mode 100644 c_poly/README.md create mode 100755 c_poly/c++_examp create mode 100644 c_poly/c++_examp.cpp create mode 100755 c_poly/c_examp create mode 100644 c_poly/c_examp.c create mode 100644 union/README.md create mode 100755 union/union create mode 100644 union/union.cpp diff --git a/README.md b/README.md index 67357dd..085b490 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ - [x] [extern那些事](./extern) - [x] [struct那些事](./struct) - [x] [struct与class那些事](./struct_class) +- [x] [union那些事](./union) +- [x] [c实现c++多态](./c_poly) 代码运行: 全部在linux下用vim编写,使用gcc/g++调试!全部可正常运行! diff --git a/c_poly/README.md b/c_poly/README.md new file mode 100644 index 0000000..0b2ea41 --- /dev/null +++ b/c_poly/README.md @@ -0,0 +1,32 @@ +# C实现C++的面向对象特性 +## 1.C++实现案例 + +C++中的多态:在C++中会维护一张虚函数表,根据赋值兼容规则,我们知道父类的指针或者引用是可以指向子类对象的。 + +如果一个父类的指针或者引用调用父类的虚函数则该父类的指针会在自己的虚函数表中查找自己的函数地址,如果该父类对象的指针或者引用指向的是子类的对象,而且该子类已经重写了父类的虚函数,则该指针会调用子类的已经重写的虚函数。 + +学习案例代码见:[c++_examp.cpp](./c++_examp.cpp) + + + +## 2.C实现 + +- 封装 + +C语言中是没有class类这个概念的,但是有struct结构体,我们可以考虑使用struct来模拟; + +使用函数指针把属性与方法封装到结构体中。 + +- 继承 + +结构体嵌套 + +- 多态 + +类与子类方法的函数指针不同 + +在C语言的结构体内部是没有成员函数的,如果实现这个父结构体和子结构体共有的函数呢?我们可以考虑使用函数指针来模拟。但是这样处理存在一个缺陷就是:父子各自的函数指针之间指向的不是类似C++中维护的虚函数表而是一块物理内存,如果模拟的函数过多的话就会不容易维护了。 + + + +学习案例代码见:[c_examp.c](./c_examp.c) diff --git a/c_poly/c++_examp b/c_poly/c++_examp new file mode 100755 index 0000000000000000000000000000000000000000..fe986fd26f303345cb71edb58a029c5bf9672f44 GIT binary patch literal 8720 zcmeHMU2Igx6`tMoAAjqG@M9n<+XN~T)yo=;YoIN6*K2c4*WhA1r7GsK_O9(M_O7*i z*EKw}R0b4_kXUL|Syd}VY5JEwREd!4hqi#+hD2&5r)v7pCX!1_Y7$66w8$^8{m$Gu z`*Xc+qDp7a5iwYp+xHMTq+$u3&P_CI*DLof>8LlR$1vEJ|Sz!LS1)~LvI6~soUY;~nwD_l0 z1I&f+%6hV+SUqkjf}fBD=Kfq9Rz2GhOm?-(u2$JGpH+b|=l)UO=;~5_yPO&%k~Lx? zB=31nxWp?Gg&!;Yb7 z-B3J{8LJy>YOHH)@THT!owDELpL7o#>Xt3>IC>#b|JLH79H9EwLIa1s)BSFAZglUy zz3;BSy=TXd{bb7*iXWR+KyytAZHlK9{>dfqUBJD#77_a0N8M}`BN;O-3?pqu`c4{s zgC~vtNPGwYX%Lz+jp0Z<0c2n}nNWx!4uso+&Bjh&L$0*bw;RnkF&-*eAURN`{_{Mo zjmKBfhy@jv*%V(!Hetv_3)LqirySs1BA~`|f|#hpMgEw)1$e$yT9t zl>cUhSLo-inC0`GsL{uNT{XiR7SHZ}4Jz4fmvFD%qM=0cAXVO+%YwGOLNbMNb5_bP zkxU`nydvdZ$o-RKKvaLbZQ`xt`owj8{BL(Uy4tQ?Ia8qt{n}NJWY?z4H8KBl82+=r zdP_)%=>zCMw_c}#>EjD-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<%^`F~V(~U4|i|u~cjzo;G7C!yGpHhLVXG zo%%(S#=uarH!@^I&15QVL^5NdFF8Ci6fN+#wIEM5`_dWN0b#UtwHx`N z>1uDLf$56$4#niTsHX8TCmn9KW)cBm#G(;10#C&Urg=>oG27W`zLMNv)ZxK8NQF>z zs*1XF(in^+qKLp~I}EL8JYi(gF^oVycp3o;CkU`NomRfAW7>nG#t}8u%|nrNTFx_i z#)C{<=3FT1u<)f%4V#f(a5H7`K`zA;$ea=3OC-&hFA!|2Ga~~^8c1Y(y_qe%9%8H8H z?L)xtV=d3^PdPQIIi&!OpZ{U;zCkMCb_f^kUD$rwsX?NBj$~rYV)CC9^_}f&6#g_g z?QPhe*Ry`L;W?`GnAykgI4Wpw!uGuGeMZ^yK8xdLJ?1B2PkSAfd3`*k?ER|Y3h=`C z6)b2yLu1PJypDcD*>gX+eS81UD*K>vIOo!2X-?Ve3MYH=Ww(D07{x{L5%IdbR!ubT zTdmr~`7?ovLVI4ncPM*}4Hf7AJIr6O+4DMoQrUC7?8YvC&t}ix;}@0vbqcP4xLt<6 zk3wPmyl*(G288#utgqa%D1M^sdH#4^F{$jI%xPK$yZx&+d)|le|IpA!*k88U^L}MP zwclXtzkU2@ol3uYRl4N%HT-|nu5XuVeN8QS&LaIm>>0U*pCC)?AX+CpM`=Cr=$whQ$_>OgEI sE2>JHDBI>|r_g>(#m~>tCs4Tq7x#zXi}ZY!YQIVyV13r++}{4b0pHwq(EtDd literal 0 HcmV?d00001 diff --git a/c_poly/c++_examp.cpp b/c_poly/c++_examp.cpp new file mode 100644 index 0000000..3cb6bc6 --- /dev/null +++ b/c_poly/c++_examp.cpp @@ -0,0 +1,42 @@ +/** + * @file c++_examp.cpp + * @brief c++中的多态 + * @author 光城 + * @version v1 + * @date 2019-08-06 + */ + +#include + +using namespace std; +class A +{ + public: + virtual void f()//虚函数实现 + { + cout << "Base A::f() " << endl; + } +}; + +class B:public A // 必须为共有继承,否则后面调用不到,class默认为私有继承! +{ + public: + virtual void f()//虚函数实现,子类中virtual关键字可以没有 + { + cout << "Derived B::f() " << endl; + } +}; + + +int main() +{ + A a;//基类对象 + B b;//派生类对象 + + A* pa = &a;//父类指针指向父类对象 + pa->f();//调用父类的函数 + + pa = &b; //父类指针指向子类对象,多态实现 + pa->f();//调用派生类同名函数 + return 0; +} diff --git a/c_poly/c_examp b/c_poly/c_examp new file mode 100755 index 0000000000000000000000000000000000000000..458fb2a5db728b32f9b99b15c7b598fb0f65583a GIT binary patch literal 8720 zcmeHMU2Igx6`tMoAAjqGKw=;&+XN&N$(J=4*O0c{UH@=R*WemEO;yZg?Ooej>|Ja3 zE;XoXQyFM1LSm^^<(En*C9Rr1G^vnksXsv6hD4&3(* z0COSyvYzZHR-adj;74SEc_0^uUC(v|lU=Q{t5tT)XH{U#xqs9*db*Y0ZnqAJWSy93 zk?%YwU82rwR+P$bQlpaVMSLl{&%;icNd1p``IJJJYHz8$+*JKzu0*kIINsOTux&V6 zHylr7#_GnJ8tWPxv~*J2F8fXXNq6u59@!G-(GQ9Gw-yi00jmFI`-7sPg(;7PRF>k|~tyvr>MZ zWD4Q>B`Nno9+)HpqWTZoC*C-0O#HADOk6v*ahg0)*sFUlQbl$S$|ngFy)zW$fX4DKslLbf#^0oa z%lABRU`*UJF3;~VT(25$+_To@yscnw)tlhQZvUyzTwm@NBD1;&hVuyo*86Jt9{{;- z%a$(sv49xkdqk%E{cpi8=j;|4WM_M?pMVXOFJ3?o=YM`Lo1N^Pk-#ei{3s7#Oaf8k zOxc!Nbi|lwU8uigTxp&C4pIU>_XFZ*TW3*o(>OzqPqvo*9niVOs2$3gj)kbPW2N+o zVGg6Y*Jtwi34S5?L~tS->c;0RKJs zo8a$&e+YgESzn7;c@_K+IIT}at`l9tHRf||Tv=H)>8kV*PQOV*@b?%gC@QTl#Aei! z{`|kQS)FvfzBX^ozUmcUsd`H6+3@&dJ2pQ;FzK}5sX#od2?%<9r`^q~D#B41En$D_l4jOs&P-*fJex8_+-tJiISULl62PZj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m< zTFT*OR#e<+-vay&_VV2Rlv|gYQwret%O#pW? zLmxG$fjU<}&iv6n)r&_>m)yCA{~vYgJ7wA*RTtRv`KN}Qf}HwJ`Onay5V?ImXZSy+ z{kyQGRI@#wr}+QtS=E0v9JzRS9!xpf=l>NKs8b+kyeu>SJ9G-;7rakY4Fsf@{EO|F zFF5RZe;ZKt94C8VJC^UEKp6L*_l3GLbn2^gu(|L6Aj}yj+tWX_g~ayEX@ARY7wWaB w3$c}~s45+zY@5HGLi;fl|D+nYPoQ!e9_|mXi}Zb#YQIWdV13%*+}Zwr0KepfGXMYp literal 0 HcmV?d00001 diff --git a/c_poly/c_examp.c b/c_poly/c_examp.c new file mode 100644 index 0000000..a083db8 --- /dev/null +++ b/c_poly/c_examp.c @@ -0,0 +1,55 @@ +/** + * @file c_examp.c + * @brief C实现多态 + * @author 光城 + * @version v1 + * @date 2019-08-06 + */ + +#include + +/// 重定义一个函数指针类型 +typedef void (*pf) (); + +/** + * @brief 父类 + */ +typedef struct _A +{ + pf _f; +}A; + + +/** + * @brief 子类 + */ +typedef struct _B +{ + A _b; ///< 在子类中定义一个基类的对象即可实现对父类的继承。 +}B; + +void FunA() +{ + printf("%s\n","Base A::fun()"); +} + +void FunB() +{ + printf("%s\n","Derived B::fun()"); +} + + +int main() +{ + A a; + B b; + + a._f = FunA; + b._b._f = FunB; + + A *pa = &a; + pa->_f(); + pa = (A *)&b; /// 让父类指针指向子类的对象,由于类型不匹配所以要进行强转 + pa->_f(); + return 0; +} diff --git a/union/README.md b/union/README.md new file mode 100644 index 0000000..5811fb9 --- /dev/null +++ b/union/README.md @@ -0,0 +1,12 @@ +# UNION那些事 + +联合(union)是一种节省空间的特殊的类,一个 union 可以有多个数据成员,但是在任意时刻只有一个数据成员可以有值。当某个成员被赋值后其他成员变为未定义状态。联合有如下特点: + +- 默认访问控制符为 public +- 可以含有构造函数、析构函数 +- 不能含有引用类型的成员 +- 不能继承自其他类,不能作为基类 +- 不能含有虚函数 +- 匿名 union 在定义所在作用域可直接访问 union 成员 +- 匿名 union 不能包含 protected 成员或 private 成员 +- 全局匿名联合必须是静态(static)的 diff --git a/union/union b/union/union new file mode 100755 index 0000000000000000000000000000000000000000..36082ab8cb5cf2b9dcc4e8ba6df29c1c71452ffe GIT binary patch literal 9704 zcmcIqeQaCR6~DHVG-*@EWrcMOP}5NeEj3A-W+{weCvp11(l+FybkO$2v7N?iVn_CK zn^a?@3RLn!OT;RzpiS#Erj36_kTz6ps7V(_#e}Ay3B<(2DmF$^%2Y6ZoOH=3|u&OIbf?ah;46;1y8<)+A7 zpqE5sM}3`E_0!8cA(3qIQPqzL>X;<2{3e*}irrEK@011RmsH1?^Z00N^l4Xq+ns($ zB>Rbp2KhbDNtdYQJtIQpXO~G4+%F5v_n5F?&UO#Lj{5nZl1C}Fs`eI(%PQ4>=4!a& zuHINzP4!*9k&V5vczR&tKyA&&nrdGv;oBs~P5w!D+l~&|635XCiTb<}9||?qJKj0+ z{qb~cv(@Qvn`Crj%Gn-07P zXi+MF<#l;#sh{3F_?kuVcY){g)3%8J#zpYWi}>HU2)<sQnF5 zQVicTF0T#$6xa$;E`~gvC}9@hm*^>cNWasjFR(6BMkKsp1FeM`q3iqm5^+6cgp!7? z3;q6sdTVrlEM-KK^}V4~Dw+~{Q~P#35=|<&eS3XxA|7oIb@fKgy8JZ{?PxP4k!lJDgRQ_e!)+|ACsIZ-8tQ9m*V`-gU|S7_u(08JvthUlC(=f? z3o#1D{%}}NQ7o#2-Vr|(i$@TERJt!pm0=DN;uAighkFj_-Jw`7`Wzkz=^-OJ5Q96N zG*t-uLb15mR$s4g_Em~)%}sUn`X*m>wzSE&Mbz)!9jI&4t9+YHc=v87S7WZst?b0U zGY`1}AVv7lAD^iX>^L{(nD<4?ZG$+8ibPb+y92r#RDC6Uu8hU76$?b4!qTf_CA6~- zS@fw${)qnY`~fsoD2A23amQ0QGM(bM(w{BslzMcQsiQ~Ay#A=4>L`}@Dzz?`KvFIw z?zP~2Mp8wE1?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* 可以含有构造函数、析构函数 + */ + UnionTest() : i(10) {print(i);}; + ~UnionTest(){}; + int i; +private: + void print(int i){std::cout<