The Artima Developer Community
Sponsored Link

Design Forum
c++ inheritance

3 replies on 1 page. Most recent reply: Jun 28, 2007 3:37 AM by balaji kn

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
sara butt

Posts: 1
Nickname: sarabutt82
Registered: Mar, 2007

c++ inheritance Posted: Mar 11, 2007 9:10 AM
Reply to this message Reply
Advertisement
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?


Martin Kardigen

Posts: 6
Nickname: kardigen
Registered: Oct, 2006

Re: c++ inheritance Posted: May 16, 2007 1:10 PM
Reply to this message Reply
> 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;
}

balaji kn

Posts: 3
Nickname: balu1
Registered: Jun, 2007

Re: c++ inheritance Posted: Jun 28, 2007 3:36 AM
Reply to this message Reply
c++ inheritance
thanks for nice explaination.

balaji kn

Posts: 3
Nickname: balu1
Registered: Jun, 2007

Re: c++ inheritance Posted: Jun 28, 2007 3:37 AM
Reply to this message Reply
what is dynamic polymorphism?

Flat View: This topic has 3 replies on 1 page
Topic: Mobile Video Streaming Testing Previous Topic   Next Topic Topic: Web Site Development  in Hyderabad at vSplash

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use