Solved

Inheritance design question

Posted on 2006-11-16
5
215 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

6 Experts available now in Live!

Get 1:1 Help Now