在c++中的動態聯編是通過虛函數來實現,但在調用時 , 必須用引用、指針才能實現動態聯編
例子如下:
指針調用
#includeusing namespace std; const double pi = 3.14; class A { public: A() {}; virtual double area() const {return 0.0;} }; class B:public A { public: B(double myr) {R = myr;} virtual double area() const {return pi*R*R;} private: double R; }; int main() { A *fig; B c(3.0); cout<<"Area of B is "; fig = &c; cout< area()<
普通變量調用
<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">#include
using namespace std; const double pi = 3.14; class A { public: A() {}; virtual double area() const {return 0.0;} }; class B:public A { public: B(double myr) {R = myr;} virtual double area() const {return pi*R*R;} private: double R; }; int main() { A fig; B c(3.0); cout<<"Area of B is "; fig = c; cout<