Rick
Rick

Reputation: 327

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

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

Upvotes: 6

Views: 16068

Answers (2)

Richard Schneider
Richard Schneider

Reputation: 35477

The child can use Person::sign().

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

Upvotes: 2

James McNellis
James McNellis

Reputation: 355187

suzie.Person::sing();

Upvotes: 15

Related Questions