Solved

Inhertiance question

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now