|
一、父子之间的冲突: 1、思考 子类中是否可以定义父类中的同名成员? 如果可以的话,那么该怎样区分呢? 如果不可以的话,那么又是为啥呢? 代码实践: #include <iostream>9 F% z) Q$ y" C! \#include <string> using namespace std;" r; \) u9 {( o; Z6 T3 Q# L 7 F( I0 x' Q' l+ [4 x; q class Parent {1 m6 _8 E7 v ^ public:9 V# E2 C s* h8 w5 x9 Q int mi; }; 5 V- A$ D+ l* U G* t. L& {& V class Child : public Parent% p0 g5 T% D( u, t* K { public: int mi;2 z7 q' D, h# B' x1 z7 _' i };+ K+ w- z: P0 _% _* H 4 ~, X% [- t2 B) Z7 ~ int main() {3 u1 f$ q! T8 r. i N2 H Child c; c.mi = 100; // mi 究竟是子类自定义的,还是从父类继承得到的?' r" Z# f/ J# T% F 9 X) P0 U( n( Y: ~5 H return 0;, r" z9 L8 M* r3 a8 V2 o } 代码是否可以编译通过,我们来看一下编译器编译的结果: root@txp-virtual-machine:/home/txp# g++ test.cpp5 H# Y" W) \6 n0 D5 jroot@txp-virtual-machine:/home/txp# + y ^. z7 \1 T' |7 c7 K 什么情况,居然可以编译通过,但是你从肉眼看,你看出到底是父类mi还是子类的mi呢,显然我们不能够去瞎猜,那么接下来我们来学习里面的真理! 2、父子之间冲突的规则:
) D5 p- y) p2 r c.mi = 100; //子类中的mi 7 E0 s2 d" o1 h# f- A c.Parent::mi = 1000; // 父类中的mi" h$ l' j* ~8 ?" c0 t7 V; t+ ` 代码实践: #include <iostream>/ n# v9 V& e, n6 a; g5 o( U#include <string>6 |& c# b3 x7 i8 u O ( u& ?/ K8 |" X8 X' d$ { using namespace std; 9 | o6 j4 p% f" P namespace A {$ U7 P) b" G4 T- l1 c1 } int g_i = 0; }7 E1 u2 r6 {8 Q! `( c5 D% M. P namespace B {# O+ l/ I. Z6 h' w: R3 Z2 `0 R int g_i = 1;// 同名的全局变量,但是位于两个不同的命名空间;% o1 d' C+ |# Z9 z/ g9 q0 I8 X }7 s# x: d T' j - v+ [9 `/ } e class Parent {' s) i: |9 g- w& h% R public:# \0 ~, r8 ^& K* k Z. P/ E+ R int mi; 6 t' l8 W C& i; z5 E+ a Parent()' L) j2 H' X; e9 W0 R* z2 e$ f {% @+ c3 @% I2 N9 q+ n- }7 T0 I cout << " arent() : " << "&mi = " << &mi << endl;}/ n1 u8 j/ o; W0 r& b4 A. z, Y }; class Child : public Parent {5 W: z9 q8 v- K) `. b5 `; D( P public: int mi; , Q" V. Z7 s4 u" I8 k5 q Child()( H0 m: v& O2 T: G1 {# a1 h {6 u% H7 `2 G5 L* @+ o cout << "Child() : " << "&mi = " << &mi << endl;# l3 U* S# V% _2 \( ] } };1 r2 L: d% W9 V7 W+ E9 E2 _ 1 }( s$ S1 y& Z int main()9 \ r, \5 M/ n6 u3 Y {2 E" L- B5 x' I$ @ Child c; 5 `8 ]4 v5 f3 n2 `3 ? g" p% p c.mi = 100; ' N2 y" L; `. k$ g" d1 i* Z c.Parent::mi = 1000;* m: t, d" l" m# D$ v0 T cout << "&c.mi = " << &c.mi << endl;& w/ A$ B6 m6 t1 ^4 F h cout << "c.mi = " << c.mi << endl; : U1 E- p/ n" ~ cout << "&c.Parent::mi = " << &c.Parent::mi << endl; cout << "c.Parent::mi = " << c.Parent::mi << endl; u1 I/ m7 w7 o return 0; }9 p( k* q$ v1 J! v# H2 |* [ 输出结果: root@txp-virtual-machine:/home/txp# g++ test.cpp/ `. M2 q! ?3 broot@txp-virtual-machine:/home/txp# ./a.out Parent() : &mi = 0x7ffc270e7bf0 Child() : &mi = 0x7ffc270e7bf4 &c.mi = 0x7ffc270e7bf45 X3 G3 l' r* o) W6 d2 w c.mi = 1006 j9 [- Q3 y0 L2 P0 _. b &c.Parent::mi = 0x7ffc270e7bf0 c.Parent::mi = 1000 3、回顾重载: (1)类中的成员函数可以进行重载
(2)子类中定义的函数是否能够重载父类中的同名函数呢? 代码实践: #include <iostream>. \$ H6 D s4 n" T1 E#include <string>! N$ y! d, ^: V d, ^ p 9 r/ [7 R% x2 ]" Y% s# g' E! x' H using namespace std;% b; @3 g; y& a% \ - S) N, |! |! |0 X. h: ?0 P+ P# e class Parent" H6 O5 {' |* a" ]7 b { public:! D; j+ g8 B. @# ~4 \ int mi; $ t- ]5 s3 Z+ o1 _9 A void add(int v) {* k2 i& v! M% r" K$ }, j/ ` mi += v; } 6 ` h3 Q" Q8 N! H5 R7 R void add(int a, int b)# H f9 M# T A& l- H& n { mi += (a + b); }- M0 ]9 P; n" @1 O) O }; / u8 Y( |& l5 Z! Z8 {- }! A B( b class Child : public Parent { public:& n. W, u7 E& ]2 @, E3 ]- n( z- o3 k int mi; void add(int v)+ O' e) a2 ]/ o; i% X! I% N {; c# \( M- `7 E m mi += v; } 8 I( r! u$ w9 J* @! m3 Z6 T# J; ] void add(int a, int b) {3 P0 u) v$ G0 i% V- p: M* G mi += (a + b); } void add(int x, int y, int z)3 R/ [" c3 V5 y {8 P$ ?: \" k6 l3 k6 L+ |( m4 \- w mi += (x + y + z); }+ M, ~2 V8 i$ z4 B8 c2 l: e5 u$ s };3 W4 Y6 W: A8 v) Z ( W. @, M& \' p% u! N. ?8 s3 [ int main()7 Z' T& T( J" @! \0 u# u3 I5 Z$ F {; L! g5 N* a8 [% K5 P' s Child c;; A' o ?4 G$ v* c _1 i c.mi = 100; 1 y) j. T$ k8 b6 Y9 z5 l5 ~ c.Parent::mi = 1000; W' b$ h5 G$ p5 F* r8 m Q D4 L. V; X4 U/ o: b' C w5 i cout << "c.mi = " << c.mi << endl;! a) N4 C2 {7 u% a- z6 m cout << "c.Parent::mi = " << c.Parent::mi << endl;5 {+ [6 ^4 A1 z2 Z, J y" P c.add(1);& U. w' F, ]% p/ r( ~7 d c.add(2, 3);- t; X1 r9 A+ B* }. P9 `2 k c.add(4, 5, 6);* T! B/ J4 X m# h + i! G0 l& U. Q: r cout << "c.mi = " << c.mi << endl; cout << "c.Parent::mi = " << c.Parent::mi << endl;" c2 a6 h+ l3 Y% _6 Z : ^4 M' E7 D5 _# X1 W6 q return 0;( v) k; [5 ~3 Q" x/ y6 k } 结果输出: root@txp-virtual-machine:/home/txp# g++ test.cpproot@txp-virtual-machine:/home/txp# ./a.out c.mi = 1007 p. \- q# ~! `" u: B c.Parent::mi = 1000( t; W D0 t I, r9 s6 I0 [ c.mi = 121, N4 Q- v3 h: C- {8 z% s2 e! H# G c.Parent::mi = 1000 注解:从实验观察来看,函数重名和成员重名的作用一样,子类会覆盖父类的。 为了更加说明这点,我们再来看一个示例: #include <iostream>#include <string> using namespace std; class Parent {" `. P9 x7 e- {3 d: g: D public: int mi;- z, A- B; B+ T& Q# T1 H 6 }) u: ]6 J3 S6 n9 n% {, S void add(int v) { mi += v; ~' l8 i5 b' W2 E7 ]8 e }# `- S K! }8 r& ~ void add(int a, int b) { mi += (a + b);% O# C% I; u3 \ g, p }& r7 M4 H* e# h2 B };7 s* m/ g9 \1 O class Child : public Parent {' m1 F$ ~6 w& X' k public: int mi; 3 q1 T! W) {5 s: \4 G# ~ void add(int x, int y, int z)% `* \2 F( Q4 U3 a" X { |* W; U5 G/ Z% `8 \, K7 T! J9 k* b$ N! I mi += (x + y + z);4 P# S! r+ R- @ }* N2 s0 l. [; S+ _9 T4 B* Y0 A% z }; : _, z. O- ]7 ? int main()9 O n4 o6 j) F) K1 U6 t8 w { Child c;0 V+ P9 Z4 W7 x0 v" F0 G" f 6 n. i" U0 W9 n" Q: v c.mi = 100; c.Parent::mi = 1000; & g3 R0 M$ V( R( A, z cout << "c.mi = " << c.mi << endl; ( t. Y1 x% V4 q3 I% U cout << "c.Parent::mi = " << c.Parent::mi << endl; - P, d/ [/ R! A1 e) o# I c.add(1);$ ~4 k, {2 ~- P& D: h c.add(2, 3);( Q* N% ?* S. t$ ] B: t9 @! q5 y9 y6 C c.add(4, 5, 6); cout << "c.mi = " << c.mi << endl;# E' ?. \: Y+ y% h1 f) J 9 S, K1 X3 f8 _' j8 v8 e cout << "c.Parent::mi = " << c.Parent::mi << endl;1 X' r8 V( S# K, m& z7 ~# j return 0;# s7 t' B5 g z/ K }* y4 l% V: W2 K+ G" ]$ A% a1 E* w 编译结果: root@txp-virtual-machine:/home/txp# g++ test.cpptest.cpp: In function ‘int main()’:% G7 l9 _4 |0 N/ z$ b8 } test.cpp:47:12: error: no matching function for call to ‘Child::add(int)’ c.add(1);( U0 h5 d/ {! g5 v5 l6 S ^, a' `3 R1 G! ?! q# b- M) t test.cpp:47:12: note: candidate is: test.cpp:29:10: note: void Child::add(int, int, int) void add(int x, int y, int z) ^- H2 t# ~2 I% i3 p+ F& m* c4 E test.cpp:29:10: note: candidate expects 3 arguments, 1 provided1 Y, ^6 P# R2 H test.cpp:48:15: error: no matching function for call to ‘Child::add(int, int)’ c.add(2, 3);* |1 R& ^1 \$ W9 {& ] ^/ E# [" k7 Y: E' L" G5 v% B% r* [ test.cpp:48:15: note: candidate is: test.cpp:29:10: note: void Child::add(int, int, int)7 ^; W- Z& Z- j; F/ B void add(int x, int y, int z)3 V" `0 |2 [: V: `$ b ^! P6 g* v/ S3 A% g) s! R, @ test.cpp:29:10: note: candidate expects 3 arguments, 2 provided 注解:显示匹配不到add(int)和add(int,int)这两个函数 解决方案,就是利用作用域符分辨符解决问题: #include <iostream>, B8 Y1 G9 q+ {' S; c- A" G#include <string> using namespace std; class Parent" |5 u* G5 j+ x/ \% Y {6 r$ T8 f5 e# F6 y& X public: int mi;# E9 s+ Z: D- Q" C$ z 0 a4 X/ c8 l9 a$ b) W4 ^ void add(int v)1 S m, z" E1 m2 C {) O& L( Q H" {4 Z& Y9 p" q+ B% y9 G mi += v;, `8 o/ r- B; K$ `. A9 j }9 B# x9 |- @7 V L: F, E! d: w6 Z4 ~; a# Z0 \7 v void add(int a, int b) {6 A% `6 ]: ?1 T+ w2 ^+ t$ o mi += (a + b);* y- q2 A+ \; ]% @0 ^ } }; & A$ W% r8 e+ G# P class Child : public Parent {: w; L4 ?) K5 ]0 y7 E8 F- m6 `% n public: int mi;- Y8 L- L$ E4 ]) X7 o% t& |7 N ) ] i# ], t& I- N7 Y# f+ p 1 r) J) N. f+ D( M# P , y3 E0 H0 U2 K void add(int x, int y, int z), [& ~6 R+ @+ x" j {; W+ J( W* b9 c, V% j( { mi += (x + y + z); }9 T4 R( o3 i7 L };5 s/ a9 \& f1 h9 ? / t$ n+ T r, |. w8 f$ o int main()" l4 a( N; d+ B {" l6 V" x5 l7 L2 v Child c; c.mi = 100;" ?3 Z& }, m* @/ f c.Parent::mi = 1000; + ?: M3 S/ [* L3 C5 R cout << "c.mi = " << c.mi << endl;8 ]2 F$ w" H g* u2 N' o ' K3 r8 S4 i8 z$ l8 b( ^1 B7 s cout << "c.Parent::mi = " << c.Parent::mi << endl; c.Parent::add(1); c.Parent::add(2, 3); c.add(4, 5, 6); cout << "c.mi = " << c.mi << endl;4 u! J0 o8 K( y- _' L . O$ u, \$ Y' ~" ]6 t6 Y cout << "c.Parent::mi = " << c.Parent::mi << endl;, ~ f/ E( Z) T. e8 P return 0;3 W5 j: q9 G- U, N- V }6 w4 f. X/ M9 z) p! D& G9 X/ S $ u1 K8 g/ Q- H- l 输出结果: root@txp-virtual-machine:/home/txp# ./a.out. x6 p6 U7 V3 E0 r+ Wc.mi = 100 c.Parent::mi = 10001 T9 Z; ?% _( q9 S c.mi = 115# A4 g4 W$ g0 L0 P2 t c.Parent::mi = 1006 4、小结:
二、总结
好了,今天的分享就到这里,如果文章中有错误或者不理解的地方,可以交流互动,一起进步。 |
微信公众号
手机版