多态--过载多态

来源:互联网 发布:本科生发表论文 知乎 编辑:程序博客网 时间:2024/05/01 16:51
#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);
}