<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 概念 多態性是指具有不同功能的函數可以用同一個函數名,可以用一個函數名調用不同內容的函數。 # 分類 * 靜態多態性 * 動態多態性 # 靜態多態性 ## 概念 靜態多態性采用**靜態聯編技術**,是指在程序編譯階段就可確定的多態性,又稱為**編譯時的多態性**。它有兩種實現形式:**強制多態**和**重載多態**。 ## 強制多態 * 將一種類型的值強制轉換成為另一種類型的值稱為**類型強制**,即強制多態. * 在**基本數據類型**之間可以通過**顯式**的強制類型轉換實現類型強制。 ### **例子** ~~~ #include <iostream> using namespace std; ? class A { ? ?public: ? ?A(int x) ? { ? ? ? ?a=x; cout<<a<<“ ?constructor of class A is called!\n”; ? } //構造函數用于類型強制 ? ?operator int() ? { ? ? ? ?cout<<a<<“ ?operator int() is called!\n”; return a; ? } //類型轉換成員函數用于類型強制 ? ?private: ? ?int a; }; ? void f(const A& x) { ? ?cout<<"function f is called!\n"; } ? int main( ) { ? ?A obj(1); ? ?f(2); ? ?obj=‘a’; ? ?int m=obj; ? ?return 0; } ~~~ ## 重載多態 重載實際上是給程序正文中相同作用域內的的同一個標識符賦予不同的操作語義。(參考函數重載) ### **例子** ~~~ #include <iostream.h> using namespace std; class Circle //圓形類 { ? ?public: ? ?Circle(double a=0.0, double b=0.0, double r=0.0) ? { ? ? ? ?x=a, y=b, radius=r; ? } ? ?double area( ) ? { ? ? ? ?return 3.14159*radius*radius; ? } ? ?Circle & max(Circle &c) ? { ? ? ? ?//函數重載,求2個Circle類對象的最大值 ? ? ? ?return area( )>c.area( )? *this : c; ? } ? ?friend ostream& operator << (ostream&, Circle&); ? ?//運算符重載,將流插入運算符'<<'重載為該類的友元函數 ? ?private: ? ?double x,y;//圓心坐標 ? ?double radius;//半徑 }; ostream& operator<<(ostream &output,Circle &c) { ? ?output<<“center:(”<<c.x<<‘,’<<c.y<<“)\narea=”<<c.area( )<<endl; ? ?return output; } int ?main( ) { ? ?Circle c1(1, 2, 3),c2(4, 5, 6),c3; ? ?c3=c1.max(c2); ? ?cout<<c3; ? ? ? ?return 0; } ~~~ # 動態多態性 ## 概念 動態多態性采用**動態聯編技術**,是指在**程序運行階段**才能確定的多態性,又稱為**運行時的多態性**。它有兩種實現形式:**類型參數多態**和**包含多態**。 ## 類型參數多態 **類型參數多態**即**模板機制**來實現類屬,這種方法可以直接將數據類型作為類的參數或函數的參數使用,從而可把功能上一致,而只是在參數類型上有差異的類或函數聲明為一個通用類或通用函數,即形成**類模板**或**函數模板**。 ### **例子** ~~~ #include <iostream> using namespace std; ? template<class T> ? ? ? ? //聲明類模板 class Compare { ? ?public: ? ?Compare(T a, T b):x(a), y(b) { }//定義函數模板 ? ?T max( ) ? ? ? ? ? ? ? //定義函數模板 ? { ? ? ? ?return (x>y)?x:y; ? } ? ?private: ? ?T x, y; }; int main() { ? ?Compare <int> c1(3,7); ?//定義對象c1,用于兩個整數的比較 ? ?cout<<c1.max( ) << endl; ? ?Compare <float> c2(45.78,93.6);//定義對象c2,用于兩個浮點數的比較 ? ?cout<<c2.max( ) <<endl; ? ?Compare <char> c3(’a’, ’A’); //定義對象c3,用于兩個字符的比較 ? ?cout<<c3.max( ) <<endl; ? ?return 0; } ~~~ ## 包含多態 通過虛函數(virtual function)實現的。包含多態是“一個接口,多種方法”。 ## 例子 ~~~ #include <iostream> using namespace std; ? //聲明類Point class Point { ? ?public: ? ?Point(float=0,float=0); ? ?void setPoint(float,float); ? ?float getX() const ? { ? ? ? ?return x; ? } ? ?float getY() const ? { ? ? ? ?return y; ? } ? ?friend ostream & operator<<(ostream &,const Point &); ? ?protected: ? ?float x,y; }; //定義Point類的成員函數 //Point的構造函數 Point::Point(float a,float b) { ? ?x=a;y=b; } //設置x和y的坐標值 void Point::setPoint(float a,float b) { ? ?x=a;y=b; } //輸出點的坐標 ostream & operator<<(ostream &output,const Point &p) { ? ?output<<"["<<p.x<<","<<p.y<<"]"<<endl; ? ?return output; } int main() { ? ?Point p(3.5,6.4); ? ?cout<<"x="<<p.getX()<<",y="<<p.getY()<<endl; ? ?p.setPoint(8.5,6.8); ? ?cout<<"p(new):"<<p<<endl; ? ?return 0; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看