一、 C++成员函数的重载
- (1) void func(int a);
- (2) virtual void func(int a);
- (3) void func(int a) const;
- (1)具有相同的作用域(即同一个类定义中);
- (2)函数名字相同
- (3)参数类型,顺序 或 数目不同(包括const参数和非const函数)
- (4)virtual关键字可有可无。
void func(int a) ==== void func(Base * this, int a)virtual func(int a) ==== virtual func(Base *this, int a)void func(int a)const === void func(const Base *this, int a) const
二 、C++成员函数的继承#include <iostream> using namespace std; class Base { public: void f(int a){ cout << "Base::f(int a)" << endl; } virtual void g(int a) { cout << "virtual Base::g(int a)" << endl; } }; class Derived : public Base { public: void h(int a) { cout << "Derivd::h(int a)" << endl; } }; int main() { Base b; b.f(3); b.g(4); Derived d; d.f(3); d.g(4); d.h(3); } #include <iostream>using namespace std;class Base {public: void f(int a){ cout << "Base::f(int a)" << endl; } virtual void g(int a) { cout << "virtual Base::g(int a)" << endl; }};class Derived : public Base{public: void h(int a) { cout << "Derivd::h(int a)" << endl; }};int main(){ Base b; b.f(3); b.g(4); Derived d; d.f(3); d.g(4); d.h(3);}
![](https://i-blog.csdnimg.cn/blog_migrate/b80103b34b44d50c43d6fb12baac169a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/895434240b61b878a0e7040a5090fb4c.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/60c02fd5c5c255807c7e7a3d49db7835.jpeg)
三、C++成员函数的覆盖
- (1)不同的作用域(非别位于派生类和基类中);
- (2)函数名称相同
- (3)参数列表完全相同;
- (4)基类函数必须是虚函数。
#include <iostream> using namespace std; class Base { public: void f(int a){ cout << "Base::f(int a)" << endl; } virtual void g(int a) { cout << "virtual Base::g(int a)" << endl; } }; class Derived : public Base { public: void h(int a) { cout << "Derivd::h(int a)" << endl; } virtual void g(int a) { cout << "virtual Derived::g(int a)" << endl; } }; int main() { Base b; b.f(3); b.g(4); Derived d; d.f(3); d.g(4); d.h(3); } #include <iostream>using namespace std;class Base {public: void f(int a){ cout << "Base::f(int a)" << endl; } virtual void g(int a) { cout << "virtual Base::g(int a)" << endl; }};class Derived : public Base{public: void h(int a) { cout << "Derivd::h(int a)" << endl; } virtual void g(int a) { cout << "virtual Derived::g(int a)" << endl; }};int main(){ Base b; b.f(3); b.g(4); Derived d; d.f(3); d.g(4); d.h(3);}
![](https://i-blog.csdnimg.cn/blog_migrate/66025345c53c2aaab438cfd73395cf97.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/78a77b73c1d3260fe04a37ab8b415193.jpeg)
四、C++成员函数的隐藏
#include <iostream> using namespace std; class Base { public: virtual void f(float x) { cout << "virtual Base::f(float) " << x << endl; } void g(float x) { cout << "Base::g(float) " << x << endl; } void h(float x) { cout << "Base::h(float) " << x << endl; } }; class Derived : public Base{ public: virtual void f(float x) { cout << "virtual Derived::f(float) " << x << endl; } void g(int x) { cout << "Derived::g(int) " << x << endl; } void h(float x) { cout << "Derived::h(float) " << x << endl; } }; int main(void) { Derived d; Base *pb = &d; Derived *pd = &d; pb->f(3.14f); pd->f(3.14f); pb->g(3.14f); pd->g(3.14f); pb->h(3.14f); pd->h(3.14f); } #include <iostream>using namespace std;class Base {public: virtual void f(float x) { cout << "virtual Base::f(float) " << x << endl; } void g(float x) { cout << "Base::g(float) " << x << endl; } void h(float x) { cout << "Base::h(float) " << x << endl; }};class Derived : public Base{public: virtual void f(float x) { cout << "virtual Derived::f(float) " << x << endl; } void g(int x) { cout << "Derived::g(int) " << x << endl; } void h(float x) { cout << "Derived::h(float) " << x << endl; }};int main(void){ Derived d; Base *pb = &d; Derived *pd = &d; pb->f(3.14f); pd->f(3.14f); pb->g(3.14f); pd->g(3.14f); pb->h(3.14f); pd->h(3.14f);}
![](https://i-blog.csdnimg.cn/blog_migrate/fe225ff80dc6d3f00a72b8ad3c805420.jpeg)
#include <iostream> using namespace std; class Base { public: virtual void f(int a) { cout << "virtual Base::f(int a)" << endl; } void f(double d) { cout << "Base::f(double d)" << endl; } }; class Derived : public Base { public: void f(double d) { cout << "Derivd::f(double d)" << endl; } }; int main() { Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5); Base b; b.f(5); b.f(3.5); Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5); } #include <iostream>using namespace std;class Base{public: virtual void f(int a) { cout << "virtual Base::f(int a)" << endl; } void f(double d) { cout << "Base::f(double d)" << endl; }};class Derived : public Base{public: void f(double d) { cout << "Derivd::f(double d)" << endl; }};int main(){ Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5); Base b; b.f(5); b.f(3.5); Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5);}
Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5); Derived d;d.f(3);d.f(2.5);Derived *pd = new Derived();pd->f(3);pd->f(2.5);只要通过Derived对象或者Derived指针执行f()函数,都只执行void Derived::f(double d)该函数。[html]Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5); Base *pBase = new Derived();pBase->f(5);pBase->f(3.5);
![](https://i-blog.csdnimg.cn/blog_migrate/addcc9fbf1492814639914b7f3ceed3c.jpeg)
#include <iostream> using namespace std; class Base { public: virtual void f(int a) { cout << "virtual Base::f(int a)" << endl; } void f(double d) { cout << "Base::f(double d)" << endl; } }; class Derived : public Base { public: void f(int a) { cout << "virtual Derived::f(int a)" << endl; } }; int main() { Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5); Base b; b.f(5); b.f(3.5); Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5); } #include <iostream>using namespace std;class Base{public: virtual void f(int a) { cout << "virtual Base::f(int a)" << endl; } void f(double d) { cout << "Base::f(double d)" << endl; }};class Derived : public Base{public: void f(int a) { cout << "virtual Derived::f(int a)" << endl; }};int main(){ Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5); Base b; b.f(5); b.f(3.5); Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5);}
Derived d; d.f(3); d.f(2.5); Derived *pd = new Derived(); pd->f(3); pd->f(2.5); Derived d;d.f(3);d.f(2.5);Derived *pd = new Derived();pd->f(3);pd->f(2.5);
view plaincopyprint?Base *pBase = new Derived(); pBase->f(5); pBase->f(3.5); Base *pBase = new Derived();pBase->f(5);pBase->f(3.5);