如何一个子类调用超类的方法与子类的相同的方法名称? [英] How can a subclass call a method of the superclass with the same method name of the subclass?

查看:113
本文介绍了如何一个子类调用超类的方法与子类的相同的方法名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

class Person {
public:
    void sing();
};

class Child : public Person {
public:
    void sing();
};

Person::sing() {
    cout << "Raindrops keep falling on my head..." << endl;
}

Child::sing() {
    cout << "London bridge is falling down..." << endl;
}

int main() {
    Child suzie;
    suzie.sing(); // I want to know how I can call the Person's method of sing here!

    return 0;
}


推荐答案

suzie.Person::sing();

这篇关于如何一个子类调用超类的方法与子类的相同的方法名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆