多態--過載多態

#include<iostream.h>
//演示過載多態。
class OverLoad
{
public:
    void test()
    {
        cout
<<"test()被執行"<<endl;;
    }
    void test(int a)
    {
        cout<<"test(int a)被執行"<<endl;;
    }
    void test(char a)
    {
        cout<<"test(char a)被執行"<<endl;
    }
    void test(int a,double x)
    {
        cout<<"test(int a,double x)被執行"<<endl;
    }
    /*
    void test(int a)
    {
        cout<<"test(int a)被執行";
    }
    與
    void test(int b)
    {
        cout<<"test(int b)被執行";
    }
    不能構成多態,它們只是變量名不同一樣,實際上是同一個函數
    */
};
void main()
{
    OverLoad OL;
    OL.test();
    OL.test('A');//若寫爲OL.test(0x65);則調用test(int a)函數
    OL.test(12);
    OL.test(5,5.0);
}    
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章