函数的重载与函数的覆盖

本文详细介绍了函数重载的概念及其构成条件,包括参数类型和参数个数的不同。同时,文章阐述了函数覆盖的概念,它发生在父类与子类之间,并通过实例展示了如何实现覆盖。

函数的重载

函数的重载
  函数的重载构成的条件:函数的参数类型,参数个数不同,才能构成函数的重载。函数的重载是发生在一个类中的。
以下两种情况都不构成重载:
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


转载于:https://my.oschina.net/crooner/blog/147381

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值