Solved

Inhertiance question

Posted on 2006-11-15
3
282 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

751 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