Solved

Inheritance design question

Posted on 2006-11-16
5
216 Views
Last Modified: 2010-04-01
Hi Experts,

I have the following problem.

#include <iostream>
#include <string>
using namespace std;

class M{};
class N{};

class A{
};

class B: public A
{
private:
     M m_memM;
};

class C: public A
{
private:
   N m_memN;

};
int main()
{
   return 0;
}
In the code snippet above, class B, C are derived from class A.
For B, it should have a member of class M
For class C, it should have a member of class N.  That is, the derived classes of A differ by their member.  class M, class N are not related.

The way, I defined above seem not to work. The reason being parent class not being able to access membes of derived class.  It was pointed out in one of my earlier question and after some testing I found that is what happening.

So, I had to move the members as below.
class A{
protected:
   M m_memM;
   N m_memN;
};

class B: public A
{
private:

};

class C: public A
{
private:

};


But, I smell that having member variables that will be used by some derivation of the parent class is not a good design.  Is it so?  How can I prevent this?  
(The problem is related to my previous question on void pointers).  
0
Comment
Question by:ambuli
5 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 50 total points
ID: 17959334
Why should all the dreived classes inherit members that they do not need? Something that is not common to all derived classes should not be put in a base class for good reasons.
0
 
LVL 4

Assisted Solution

by:Raymun
Raymun earned 100 total points
ID: 17959694
Hi,

A member of a derived class is private to that class, which is why, in the first example, the parent class cannot access it.

Both are good designs, depending on the application. If a derived class does not need to have both M and N objects, then the first example would be better than the second.

In the second example, the protected members, of type M and N, are inherited by any class that derives from class A. This means that both classes B and C have copies of m_memM and m_memN from parent A. Note that these, and any other, inherited members can be overridden as well. This example would be good in situations where all derived classes must have m_memM and m_memN as members.

ray


0
 
LVL 49

Expert Comment

by:DanRollins
ID: 17961104
Since you are using void pointers (from your previous Q http:/Q_22062544.html) then why not just coerce the pointer a  B* or a C* (rather than an A*)??  That should let you access the desired members.
0
 
LVL 6

Assisted Solution

by:SJT2003A
SJT2003A earned 150 total points
ID: 17966614
I would go for the first type of definition/declaration. As a base class defines the common characteristics of all its derived classes and mostly all those characteristics are used by its derived classes, I would not declare a member where it may be used in one and not in all others. I would not say it is a good design.
Hence I would define the derived classes with those which are specific to that class and not common in other siblings.

And for your question, why you want to use base class to access the members of its derived???
A base class is parent and defines only those are common of all its derived while the derived has its own class-specific characteristics. The class-specific behaviour is only accessed by the derived and that is what the derived definitiion is for.
Accessing the derived class-specific features thru its base class is not allowed nor a right idea.

Could you please elaborate the scenario where you would like to access it such way???

Good luck :)
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 200 total points
ID: 17967684
SJT2003A,
As I understand the situation, ambuli's reasons relate to the need to pass an object to a function as a void pointer
     http:/Cplusplus/Q_22062544.html
It's not that unusual of request.
In MFC, a common scenario is that all you have is a CWnd* to pass, but you know that it is a pointer to a particular derived object, such as, say, a CListView or a CTreeCtrl.  If you know which kind of object it is, you can coerce the pointer into the correct type and then access the correct (specialized) properties and member functions.  If you don't, you can still use access the common base-class members.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

920 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

14 Experts available now in Live!

Get 1:1 Help Now