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 .ios

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

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

  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.spa

有virtual的例子code

<!-- 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)

(有待完善)ci

相關文章
相關標籤/搜索