c++ name hiding, overriding, overloading

c++ name hiding overloading overriding

  • overriding : The redefinition of a virtual function in a sedprived class is usually called overriding.
  • overloading :重载
  • redefine :
<!-- lang: cpp -->
在此输入代码
#include <iostream>
#include <string>
using namespace std;

class base
{
    public:
        int f() const{
            cout << "base::f()" << endl;
            return 1;
        }
        int f(string) const{
            cout << "base::f(string)" <<endl;
            return 1;
        }
        void g(){cout << "base::g()"<<endl;}
};
class derived1 : public base
{
    public:
        void g() const{cout << "derived1::g()" <<endl;}
};
class derived2 : public base
{
    public:
        int f() const{
            cout << "derived2::f()" << endl;
            return 2;
        }

};
class derived3 : public base
{
    public:
        void f() const{cout << "derived3::f()" <<endl;}
};
class derived4 : public base
{
    public:
        int f(int) const{
            cout << "derived4::f()" <<endl;
            return 4;
        }
};

int main()
{
    string s("hello");
    derived1 d1;
    int x = d1.f();
    d1.f(s);
    d1.g();
    deprived2 d2;
    // d2.f(s)  // error base::f(string) 被隐藏
    derived3 d3;
    // int x = d3.f(); // error 说明不能调用基类的int 型的f()
    d3.f();
    derived4 d4;
    // d4.f(); // error 说明只能调用自己类里面的f(int);
    d4.f(1);

    cin.get();
    return 0;
}
  • 上面这个例子说明:基类的函数没有声明为virtual,在派生类中一个同名函数会覆盖基类的所有重载函数。自己总结,有错还请大家指出)。

thinking in c++: In base you see an overloaded function f(), and deprived1 doesnt make any change to f() but it does redefine .

  1. In main(), you can see that both overloaded versions of f() are available in deprived1

  2. deprived2 redefines one overloaded version of f() but not the other

  3. In deprived3, changing the return type hides both the base class versions

  4. The deprived4 shows that changing the arguement list also hides both the base class versions.

有virtual的例子

<!-- lang: cpp -->
在此输入代码
#include <iostream>
#include <string>
using namespace std;

class Base {
public:
  virtual int f() const { 
    cout << "Base::f()\n"; 
    return 1; 
  }
  virtual void f(string) const {cout << "base::f(string)" << endl;}
  virtual void g() const {}
};

class Derived1 : public Base {
public:
  void g() const {}
};

class Derived2 : public Base {
public:
  // Overriding a virtual function:
  int f() const { 
    cout << "Derived2::f()\n"; 
    return 2;
  }
};

class Derived3 : public Base {
public:
  // Cannot change return type:
  //! void f() const{ cout << "Derived3::f()\n";}
};

class Derived4 : public Base {
public:
  // Change argument list:
  int f(int) const { 
    cout << "Derived4::f()\n"; 
    return 4; 
  }
};

int main() {
  string s("hello");
  Derived1 d1;
  int x = d1.f();
  d1.f(s);
  Derived2 d2;
    Base& b2 = d2;

  x = d2.f();
  b2.f(s);
//!  d2.f(s); // string version hidden
  Derived4 d4;
  x = d4.f(1);
//!  x = d4.f(); // f() version hidden
//!  d4.f(s); // string version hidden
  Base& br = d4; // Upcast
//!  br.f(1); // Derived version unavailable
  br.f(); // Base version available
  br.f(s); // Base version abailable
  cin.get();
  return 0;
}

运行结果:
Base::f()
base::f(string)
Derived2::f()
base::f(string)
Derived4::f()
Base::f()
base::f(string)

(有待完善)

你可能感兴趣的:(C++,继承)