|
|
Re: c++ inheritance
|
Posted: May 16, 2007 1:10 PM
|
|
> i have a base class "person" and two inherited classes, > "student" and "teacher".Suppose there is an object which > is both a student and a teacher. given a reference to the > student object can i find out if this is a teacher as well?
Yes, consider code:
#include <iostream>
using std::cout; using std::endl;
class person { //some members ... public: //it's important - person have to have virtual methods virtual ~person(){} //good practice };
class student : virtual public person { //some members };
class teacher : virtual public person { //some members };
class adiunkt : public student, public teacher { //some members };
int main(int argc, char* argv[]) { adiunkt a; student * s = &a; cout << dynamic_cast<teacher*>(s) << endl; //you should see address of teacher ;) return 0; }
|
|