Solved

Inhertiance question

Posted on 2006-11-15
3
280 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 …
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 learn how to clear a vector as well as how to detect empty vectors in C++.

856 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