#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; } - possible duplicate of How to call Base class method through base class pointer pointing to derived class, Call virtual method from base class on object of derived type.outis– outis2012-04-22 00:17:28 +00:00Commented Apr 22, 2012 at 0:17
Add a comment |
2 Answers
suzie.Person::sing(); 3 Comments
Ben Voigt
or
Person& sue = suzie; sue.sing();.James McNellis
or
static_cast<Person&>(suzie).sing(); or any variation thereof, yes.Ben Voigt
I wrote that and then removed it. I try to NEVER use a cast where an implicit conversion will work.
The child can use Person::sign().
See http://bobobobo.wordpress.com/2009/05/20/equivalent-of-keyword-base-in-c/ for a good explanation.