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); 
}