Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a subclass call a method of the superclass with the same method name of the subclass?

Tags:

c++

#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;
}
like image 562
Rick Avatar asked Dec 04 '22 08:12

Rick


2 Answers

suzie.Person::sing();
like image 60
James McNellis Avatar answered Jan 05 '23 16:01

James McNellis


The child can use Person::sign().

See http://bobobobo.wordpress.com/2009/05/20/equivalent-of-keyword-base-in-c/ for a good explanation.

like image 27
Richard Schneider Avatar answered Jan 05 '23 15:01

Richard Schneider