Solved

Inhertiance question

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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 the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

832 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