重载、覆盖、隐藏
1 C++重载的发生条件
参数列表不同:(参数个数、顺序、类型)
2 extern“C”处理
3 隐式类型转换导致重载函数产生二义性

void output(int x)
{
cout << " output int " << x << endl;
}

void output(float x)
{
cout << " output float " << x << endl;
}


output(0.5)

因为程序的隐式转换,编译器不知道要调用哪个函数

4、重载覆盖、隐藏
重载的条件:同一个类中,参数列表不同
覆盖的条件:父子类中,参数列表完全相同,有Virtual关键字,多态性(父类指针指向子类)
隐藏条件两种情况:
<1> 函数名相同的情况下,子类基类的参数列表不同,此时无论父类的函数有没有virtual关键字,都会有隐藏
<2> 函数名相同的情况下,父类函数没有virtual关键字

5、子类调用父类

class Base
{
public:
void f(int x);
};

class Derived : public Base
{
public:
void f(char *str);
};

void Test(void)
{
Derived *pd = new Derived;

pd->f(10); // error
}
// 正确写法
class Derived : public Base
{
public:
void f(char *str);
void f(int x)
{
Base::f(x);
}
};

6、参数缺省值
<1>、注意只能在函数的声明中缺省
void Foo(int x=0, int y=0); // 正确,缺省值出现在函数的声明中>
<2>、如果函数有多个参数,参数只能从后向前挨个儿缺省


正确的示例如下:
void Foo(int x, int y=0, int z=0);
错误的示例如下:
void Foo(int x=0, int y, int z=0);

<3> 当心二义性

#include <iostream.h>
void output( int x);
void output( int x, float y=0.0);
void output( int x)
{
cout << " output int " << x << endl ;
}

void output( int x, float y)
{
cout << " output int " << x << " and float " << y << endl ;
}

void main(void)
{
int x=1;
float y=0.5;
// output(x); // error! ambiguous call
output(x,y); // output int 1 and float 0.5
}

7、运算符重载
运算符与普通函数在调用时的不同之处是:对于普通函数,参数出现在圆括号内;
而对于运算符,参数出现在其左、右侧