您的位置:首页 > 编程语言 > C语言/C++

C++学习------过载多态的例子

2007-06-16 11:45 295 查看
#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);
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ iostream include