函数的重载
函数的重载函数的重载构成的条件:函数的参数类型,参数个数不同,才能构成函数的重载。函数的重载是发生在一个类中的。
以下两种情况都不构成重载:
1、void output();
int output();
2、void output(int a,int b=5);//b带缺省值
void output(int a);
eg:
#include<iostream>
using namespace std;
class Point
{public:
int x;
int y;
Point()
{
x=0;
y=0;
}
Point(int a,int b)//函数的重载
{
x=a;
y=b;
}
~Point()
{}
void output()
{
cout<<x<<endl<<y<<endl;
}
};
void main()
{
Point pt(3,3);
pt.output();
}
output:
3
3
函数的覆盖
函数的覆盖是发生在父类与子类之间。eg:
#include<iostream>
using namespace std;
class Animal
{
public:
void breathe()
{cout<<"animal breathe"<<endl;}
};
class Fish:public Animal
{
public:
void breathe()//对父类函数的覆盖
{
Animal::breathe();//若加上此句输出结果为①,没加结果为②
cout<<"fish bubble"<<endl;
}
};
void main()
{
Fish fh;
fh.breathe();
}
输出结果①:
animal breathe
fish bubble
输出结果②:
fish bubble