|
在上一篇文章中我们已经接触到了protect这个关键字的作用了,今天我们继续深入继承的深入学习。 一、三种继承方式: 1、从问题引出主题: (1)冒号( class Parent 表示继承关系,Parent表示被继承的类,public的意义是什么?{2 m% u) P6 d. }% h) \) p 5 V9 S/ M* O. M9 `: L# J }; 2 g' c2 m; V' o% U* {; D class Child : public Parent( r% R/ O6 a8 W+ C {, r: Z! ?3 F9 u9 T9 K2 M+ l% f 6 V2 [( N& m; s1 _& K; N. {! p } (2)是否可以将继承语句中的public换成protected或者说private,因为写代码的时候,你肯定会有这样的好奇心;同时如果可以这样写的话,那么,这样与public继承又有什么区别呢?咋们还是通过实际代码来理解: #include <iostream>#include <string> + [) g; |$ f# ] using namespace std; class Parent {9 Y( q- _- `: F7 H- l4 e };, R. O( N, j: q$ ]$ q $ E# @! ?, |. _7 X+ E; J5 |3 o9 U class Child_A : public Parent { }; class Child_B : protected Parent9 M7 ?% n7 x+ L' R" E { }; : x9 k0 d2 B: ]7 Y/ N( Q class Child_C : private Parent { };+ n- W& {/ w3 ?$ g! }; @, {; p/ n& U+ ]1 y - w4 J( X/ x- a: L; ^! i; y int main(); b! M$ m+ R- \: p' E. J1 j { 0 @5 w, B& c6 `7 Q0 r return 0; } 4 B6 Q4 w% N( O. x3 P7 y2 I 试试看,能否编译通过: root@txp-virtual-machine:/home/txp# vim test.cpproot@txp-virtual-machine:/home/txp# g++ test.cpp/ u! t/ ?; w& ]/ |5 O, K( l; { root@txp-virtual-machine:/home/txp# 4 J V( X! z7 n6 A( D& n& o9 c ! U& p- }2 L4 q( F! T 注解:通过编译我们发现,完全没有毛病。 2、c++中支持三种不同的基础方式
继承成员的访问属性: =Max{继承方式,父类成员访问属性} 注意:c++中的默认继承方式为private 代码实践: #include <iostream>( r& i, }) W: L/ } m; O: O* o#include <string> @2 u* y$ i: K8 R9 k. ` using namespace std;/ e* W, y" Z# U class Parent& P# I3 w, c6 o$ g- [ |% p { protected: int m_a; protected:# Q X* ~" p3 K0 K9 M% ^ int m_b; public:* e! q" ~$ k; E$ D* `# S9 m int m_c; ) q( o, y. d# w7 h- N void set(int a, int b, int c)# ?% y8 q2 u6 C* {2 u- Y { m_a = a; m_b = b; m_c = c; }' G! V9 p `/ U7 Y C };# O3 A7 {1 o$ w" a 1 K! u* v" K+ ` class Child_A : public Parent7 W0 K2 X# q. L2 i { public: void print() { cout << "m_a" << m_a << endl;/ N! g+ r+ A! K2 a+ x% B cout << "m_b" << m_b << endl;. M& ?- ^( H. _' u" o* _ cout << "m_c" << m_c << endl; } };2 k$ K" }+ g- Y2 g: T) w' c/ [ . b/ U# M% f0 ]+ Z class Child_B : protected Parent2 _2 _) G0 ~3 v3 V/ N {' q v# N, \5 @/ ]' t; \ public:. g9 R3 P w/ t! [% [" x. u) d( M void print() {0 t0 v/ T0 G; s7 g: T; y cout << "m_a" << m_a << endl;$ S) A v( c% ?& h cout << "m_b" << m_b << endl; cout << "m_c" << m_c << endl; } }; class Child_C : private Parent {2 q u8 z4 d: e5 u# z" d+ B3 q public: void print() { cout << "m_a" << m_a << endl; cout << "m_b" << m_b << endl; cout << "m_c" << m_c << endl;* X3 ^2 e) p6 h3 R }2 R u% p8 y W7 t: k$ z };0 X) I9 {& Z. ^) g: i9 h9 j int main()5 }9 |+ d& T1 p8 H5 \( x& R Q; t { Child_A a;3 v3 \. }4 c. [; ~4 S Child_B b; C) T/ E% E' } p. t6 D8 U Child_C c; a.m_c = 100;1 o$ z9 _6 D' [4 W0 J // b.m_c = 100; // Child_B 保护继承自 Parent, 所以所有的 public 成员全部变成了 protected 成员, 因此外界无法访问 // c.m_c = 100; // Child_C 私有继承自 Parent, 所以所有的成员全部变成了 private 成员, 因此外界无法访问" Y/ m" S( X2 q$ I& q( t8 g. v 7 m1 j/ z! Z7 J9 k5 I+ v a.set(1, 1, 1); // b.set(2, 2, 2); // c.set(3, 3, 3);* y. J8 m) A" E" {1 r a.print();1 ^3 ]% U7 U8 n* x/ A& ^. V4 Q8 i b.print(); c.print(); ' Z& u2 e& l$ c& q return 0;' P0 t8 v; J R# D8 ?3 r }( U# k* M0 m `, v# L+ m. I/ [ . p' [# w5 E6 c 输出结果: root@txp-virtual-machine:/home/txp# ./a.outm_a12 _/ p% ~" c$ [/ E2 _$ m1 e$ R7 ~+ C m_b1 m_c1* ?. \5 y) M& s$ k m_a4197200 m_b0 m_c4196272 m_a-903133344 m_b32766 m_c0 " N6 I' q5 I$ t1 E7 W+ \/ [ 代码实践二: #include <iostream>#include <string>! P9 A9 R2 y/ P- ] + y0 \. s2 r9 F0 L/ i7 } using namespace std;/ d7 i. D) t% X6 f9 i9 a& x class Parent; j: P* |" j g/ }4 F {* A. U: t4 F: k6 C2 j protected: int m_a; protected:+ w( J9 p8 p5 c; t# M' v int m_b; public:% D! b9 G0 ?' t! _% g l+ Z int m_c; $ Q6 O6 o3 s+ x, m" @7 X0 E2 U void set(int a, int b, int c) d0 l* t: t. I& f+ a z. T { m_a = a; m_b = b; m_c = c;+ W5 q9 [' `& Q( J; y, D4 `/ L& @/ U }! R! b4 s- y2 t }; . i8 ~4 L+ B9 Q2 q& u$ w8 Z class Child_A : public Parent5 S& I& X% O2 ?6 |. O$ }; ^ { public: void print()1 `% _2 S7 n" { {5 _/ b" d: G; I' J4 W cout << "m_a" << m_a << endl;) o5 J# ]/ V6 P3 T" I cout << "m_b" << m_b << endl;9 Z1 Y: u7 b3 W$ j) w. D* ~ cout << "m_c" << m_c << endl;% y8 k/ m& [. o! n4 l' o9 R }6 K* W7 @* c" t2 `+ x };0 y9 ?, Y3 \% f* y Z! B' }( N 8 u2 c/ ~3 Q) M/ ~0 I! y* K class Child_B : protected Parent$ M* r6 \+ w& L% e4 Q { public: void print()' v: v6 Q# w2 _# R, @1 w; ] {6 A+ ]6 y; N$ M$ l3 b3 |1 Q cout << "m_a" << m_a << endl;- W# h8 L8 k) X cout << "m_b" << m_b << endl; cout << "m_c" << m_c << endl;1 y5 t1 ~7 R+ |- S8 Y& }9 t }& D1 M. s. {3 v6 S% @; K };, a x- E0 f- u ' s9 B6 V" R$ i8 n2 y5 Q class Child_C : private Parent4 |0 [' R0 T1 E/ w) @* K% G {* |$ T- G$ L7 \ public: void print() { cout << "m_a" << m_a << endl;' S8 n8 q( n$ s8 C* O2 ?) ?' l% ~8 J" E cout << "m_b" << m_b << endl; cout << "m_c" << m_c << endl;, r1 V5 k) q2 i2 Q- f( h } };5 }5 J# ~* X2 Q4 t6 E Q! W7 X int main()0 Y2 v# @$ i+ X# V6 @" I* X { 5 a# J9 |1 E2 z8 I9 P2 e Child_A a;, E2 ~/ d8 [' N! K% q Child_B b; Child_C c; a.m_c = 100; b.m_c = 100; // Child_B 保护继承自 Parent, 所以所有的 public 成员全部变成了 protected 成员, 因此外界无法访问5 L' \0 J8 u" g: z! _) q; ~" ?' g c.m_c = 100; // Child_C 私有继承自 Parent, 所以所有的成员全部变成了 private 成员, 因此外界无法访问 a.set(1, 1, 1); b.set(2, 2, 2);8 i2 d5 z, [! c) n9 L% T c.set(3, 3, 3); % N% K0 a' ]* Y" [- _ a.print();& B! I0 y; J+ ^: }2 j' U b.print();/ K2 Z1 V) a3 X9 `' I6 \7 p1 C2 \ c.print(); 6 h. C) s6 t+ f/ F6 B return 0;2 S# S0 q& q& G/ b9 q: M } & W8 g( W8 b$ k 输出结果: root@txp-virtual-machine:/home/txp# g++ test.cpptest.cpp: In function ‘int main()’:/ r) Z! t7 G8 q0 F$ e( ? test.cpp:13:9: error: ‘int Parent::m_c’ is inaccessible int m_c; ^2 m d4 t& ~ n. q test.cpp:63:7: error: within this context b.m_c = 100; // Child_B 保护继承自 Parent, 所以所有的 public 成员全部变成了 protected 成员, 因此外界无法访问 ^ test.cpp:13:9: error: ‘int Parent::m_c’ is inaccessible int m_c;) }! A/ |& O; |2 V5 g0 E ^! L& x, F2 X0 H! l [+ w test.cpp:64:7: error: within this context c.m_c = 100; // Child_C 私有继承自 Parent, 所以所有的成员全部变成了 private 成员, 因此外界无法访问1 n- ?- M, }& O7 q+ n% _ ^ test.cpp:15:10: error: ‘void Parent::set(int, int, int)’ is inaccessible* ^& f" P$ H1 J! Q7 h2 I void set(int a, int b, int c) ^; d# i9 c2 L7 v) t6 K! s test.cpp:67:18: error: within this context+ W2 R0 `! ^' A8 p& c- r b.set(2, 2, 2); ^ test.cpp:67:18: error: ‘Parent’ is not an accessible base of ‘Child_B’4 y: G+ c4 i8 u" { l9 c test.cpp:15:10: error: ‘void Parent::set(int, int, int)’ is inaccessible! B; I5 K/ x: [! ^7 @; [7 M' g void set(int a, int b, int c) ^ test.cpp:68:18: error: within this context! p7 l* O# e: ]; `2 `! z c.set(3, 3, 3); ^ test.cpp:68:18: error: ‘Parent’ is not an accessible base of ‘Child_C’ 注解:当子类定义的对象,如果继承关系不是public,那么子类定义的对象,就无法访问父类中的属性和方法了。 3、遗憾的事实
二、总结
好了,今天的分享就到这里,如果文章中有错误或者不理解的地方,可以交流互动,一起进步。 8 J9 [) i1 W, m' P, s3 t) o/ M |
微信公众号
手机版