Solved

Inhertiance question

Posted on 2006-11-15
3
281 Views
Last Modified: 2010-04-01
Hi Experts,

I have been having some strange things happening in my code.  It worked one or two times and not after.  I began to doubt there is some problem in the way I am using inheritance/polimorphism.  here is the simplified version of the code.  Am I doing something wrong?  I tried this in visual studio 6.0 and it seems to work all the time.( Printing 3)

#include <iostream>

using namespace std;

class A
{
      
public:
      A() {};
      ~A() {};
      virtual int getMember(){ return 0;};
      virtual void setMember(int i ) { memA = 0;};
private:
      int memA;
      
};

class B : public A
{
public:
      B(){ };
      ~B(){ };
      int getMember(){return memB;};
      void setMember(int a){ memB = a; };
private:
      int  memB;
};

class X
{
public:
      X(){};
      ~X(){};
      void setSomething(A& a){ cout << a.getMember() << endl;};
      
};



int main()
{

      X *xx = new X();
      B *ab = new B();
      ab->setMember(3);

      xx->setSomething(*ab);

      return 0;
}
0
Comment
Question by:thanesh
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17953113
This is absolutely OK. Since 'getMember()' is declared virtual, 'X::setSomething()' will call 'B:getMember()' through the virtual function table.
0
 
LVL 1

Author Comment

by:thanesh
ID: 17953362
Thanks JKR. Am I using the pointers correctly.  Because, I would like to confirm this before I deal with my rest of the code.
0
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 17953371
Yes, you are (when we leave out the fact that you aren't deleting them before exiting ;o)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question