C++基础-函数的覆盖和函数重载

函数的覆盖: 在父类里面定义的函数,我们可以在子类里面命名相同的函数,将基类函数覆盖

函数重载:在父类里面定义的时候,根据输入参数的不同进行函数的重载 

//
// Created by qq302 on 2020/7/19.
//
#include <iostream>

using namespace std;

class Animal{
public:
    void eat();
    void eat(int count); //在基类中使用函数的重载
};

void Animal::eat() {
    cout << "正在吃东西" << endl;
}

void Animal::eat(int count) {
    cout << "吃了" << count << "碗饭" << endl;
}
class Pig:public Animal{
public:
    void eat(); //在子类中对于相同的函数名进行覆盖
    void eat(int count);
};

void Pig::eat() { //将基类实现的方法重新进行实现
    Animal::eat();
    cout << "猪正在吃饭" << endl;
}

void Pig::eat(int count) {
    Animal::eat(count);
}


int main() {
    Pig pig;
    pig.eat();
    pig.eat(15); 
}

 

原文链接: https://www.cnblogs.com/my-love-is-python/p/13340731.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++基础-函数的覆盖和函数重载

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/368350

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 下午6:21
下一篇 2023年3月2日 下午6:21

相关推荐