classA { public: A() = default; A(int a, int b, int c) : pub(a), pro(b), priv(c) {} int pub; protected: int pro; private: int priv; };
classB : public A { // 公有继承 public: B(int a, int b, int c) { pub = a; pro = b; // pri = c; // test.cc:20:9: error: ‘pri’ was not declared in this scope // pri = c; // ^~~ } };
intmain(){ B b(1, 2, 3); cout << "b.pub:" << b.pub << endl; // cout << "b.pro:" << b.pro << endl; // test.cc:31:27: error: ‘int A::pro’ is protected within this context // cout << "b.pro:" << b.pro << endl; // ^~~ // cout << "b.priv:" << b.priv << endl; // test.cc:32:28: error: ‘int A::priv’ is private within this context // cout << "b.priv:" << b.priv << endl; // ^~~~ return0; }
classB : public A { public: voidfunc(int a, int b){ cout << "B::func(int a, int b)" << endl; } };
intmain(){ B b; b.func(10); return0; }
1 2 3 4 5 6 7 8 9
@└────> # g++ test.cc test.cc: In function ‘int main()’: test.cc:20:14: error: no matching function for call to ‘B::func(int)’ b.func(10); ^ test.cc:13:10: note: candidate: ‘void B::func(int, int)’ void func(int a, int b) { ^~~~ test.cc:13:10: note: candidate expects 2 arguments, 1 provided