473,738 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance with specialized members

Consider the following implementation of a graph, whose nodes must be of
type Node or of a subclass of Node:

class Node {
public:
Node(Data* d) { adjList = new vector<Node*>; data = d; }
virtual ~Node() { delete adjList; }
virtual void addNode(Data* d) { /*...*/ }
void connectToNode(N ode* n) { adjList->push_back(n) ; }
Node* getChild(int i) const { return adjList->at(i); }
// etc...

protected:
vector<Node*>* adjList;
Data* data;
};
class GraphBase {
protected:
GraphBase() { adjList = new vector<Node*>() ; }
~GraphBase() { /* Free adjacency list */ }
void addNode(Data* d) { adjList->push_back(ne w Node(d)); }
void addEdge(Node* s, Node* t) { s->connectToNode( t); }
Node* getNode(int i) const { return adjList->at(i); }
Node* getFirstNode() const;
Node* getNextNode() const;
/* etc... */

private:
vector<Node*>* adjList;
int currentNode;
};

template <class NodeType>
class Graph : private GraphBase {
public:
Graph() : GraphBase() { }
~Graph;
void addNode(Data* d) { GraphBase::addN ode(d); }
void addEdge(Node* s, Node* t) { GraphBase::addE dge(s, t); }
NodeType* getNode(int i) const { return (NodeType*)Grap hBase::getNode(
i); }
NodeType* getFirstNode() const;
NodeType* getNextNode() const;
/* etc... */
};

class SpecialNode : public Node { SpecialNode* foo(); /* etc... */ };

class SpecialGraph : public Graph<SpecialNo de> { /*...*/ };

I have a few questions about the previous partial code:

1) Is there a better way of implementing Node, knowing that it will be
specialized? As it is, whenever one of the methods returning a Node* is
called, there probably must be an explicit cast to the correct subclass
in the caller code, e.g. an implementation of foo() might be

SpecialNode* foo() { return (SpecialNode*)a djList->at[0]; }

2) Using a template (Graph) privately inheriting from a class (GraphBase)
is the only way I found to constrain a user of a graph to instantiate
only graphs whose nodes are of type Node or of a subtype of it. Is there
a better way to accomplish that? If I wanted to hide the GraphBase class
to the world, could I make it an inner class of the template, instead of
inheriting from it?

3) A statement such as GraphBase* g = new Graph<SpecialNo de>() is not
allowed, because the template has a private base (right?). Should I make
the destructor of GraphBase virtual anyway?

Thanks in advance for your attention and for any advice you will be so
kind to give me.

Nicola
Jul 19 '05 #1
1 2104
1) Is there a better way of implementing Node, knowing that it will be
specialized? As it is, whenever one of the methods returning a Node* is
called, there probably must be an explicit cast to the correct subclass
in the caller code, e.g. an implementation of foo() might be
I prefer virtual base classes or interface classes for this.

class INode {
public:
virtual ~INode(void) { };
virtual foo(void) = 0;
virtual bar(void) = 0;
... etc ... to hand all types of routine
};

class SpecialNode1:pu blic INode
{
virtual foo(void) { ... };
virtual bar(void) { ... };
};

class SpecialNode2:pu blic INode
{
virtual foo(void) { ... };
virtual bar(void) { ... };
};

You will not have to type cast anything

SpecialNode* foo() { return (SpecialNode*)a djList->at[0]; }
the above becomes...
INode* foo() { return adjList->at[0]; };

2) Using a template (Graph) privately inheriting from a class (GraphBase)
is the only way I found to constrain a user of a graph to instantiate
only graphs whose nodes are of type Node or of a subtype of it. Is there
a better way to accomplish that? If I wanted to hide the GraphBase class
to the world, could I make it an inner class of the template, instead of
inheriting from it?


if you use pure virtual base classes or sometimes refered to interface
classes, you can prevent the
user from doing anything. You can force them to only create the type of
objects you want.
and if you put everything into a DLL you can even hide the implementation
from the user.
All you give them is the interface class.

// example .h
// given to users of Node
class INode {
protected:
// restrict user from new/delete, must use CreateInstance( ) and Release()
INode(void);
virtual ~INode(void);
public
virtual void Release(void) = 0;
virtual void DoSomething1(vo id) = 0;
virtual void DoSomething1(vo id) = 0;
static INode* CreateInstance( void) = 0;
static INode* CreateAltInstan ce(void) = 0
};

// example of .h for DLL use only
class SpecialNode1 : public INode
{
.....
};

// example of .cpp for DLL
#include "INode.h"
#include "SpecialNod e.h"
#include "AltSpecialNode .h"
INode* INode::CreateIn stance(void) {
return new SpecialNode;
};

INode* INode::CreateAl tInstance(void) {
return new AltSpecialNode;
};
The user only sees the INode class.

If you never change your interface class all old programs linked to an old
DLL will
still work with a newer revised DLL. If you have to change the interface,
add new virtual routines
to the end ( never delete routines or change how and an old routine is
called ).
You will end up with a dll that works with your old programs as well as your
new ones without being recompiled.


Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
425
by: Asapi | last post by:
When a derived class inherits from a base class, does the former inherits everything, including public/protected/private instance data, static data, and various methods(static, private/public, etc.) in base class? What happens to static data and methods during inheritance? What is an accurate definition of inheritance? Thanks!
14
12917
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at all obvious in VBA which lacks inheritance. I'm trying the explanation again now. I often find cases where a limited form of inheritance would eliminate duplication my code that seems impossible to eliminate otherwise. I'm getting very...
3
1750
by: Aaron Watters | last post by:
A C# question about constructors/static methods and inheritance: Please help me make my code simpler! For fun and as an exercise I wrote somewhat classical B-tree implementation in C# which I later ported to java and Python for comparison. http://bplusdotnet.sourceforge.net/ for full details and code ]
26
425
by: Michael A. Covington | last post by:
Suppose class X has some public properties, and class Y inherits from X. Is there a way that some of the public properties of X can be private (hidden) in Y (but still usable by the methods inherited from X that use them)?
60
4930
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
6
1884
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance" using the prototype property. I realize there are no classes in JS, that code therefore lives in objects instead of class definitions, and that "inheritance" must be achieved prototypically and not classically. That said, on with the code. Say I...
12
2379
by: Massimo | last post by:
Hi to all, I'm facing a problem in a particularly complex inheritance hierarchy, and I'd like to know what the standard says about it and if my compiler is correct in what it does. I have two consecutive layers of diamond-shaped inheritance; the first layer is declared as using virtual inheritance, while the second one is declared as *not* using it. This is what I'd like to have:
11
2247
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern to illustrate this. Problem: I need to expose two lists of objects from a high-level class. I would like to expose these lists as read-only, but require write access internally.
3
2554
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing base class is usually done automatically by the compiler, but a derived class can invoke the base...
0
8968
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9259
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.