473,231 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

Design problem with inheritance

class ABC
{
public :
virtual void operation1();
virtual void operation2();
virtual int GetValue();
virtual char GetValue();
virtual void SetValue(int);
virtual void SetValue(char);
}

class intABC : public ABC
{
public :
virtual void operation1();
virtual void operation2();
void SetValue(int);
int GetValue();
private:
int val;
}

class charABC : public ABC
{
public :
virtual void operation1();
virtual void operation2();
void SetValue(char);
char GetValue();
private :
char val;
}

class Controler
{
public :
//constructors and destructors
void somefunc() //this function handles container member
private :
vector<ABC*val;
}

client Controler class manipulates ABC derived classes polymorphically

I feel, In the above design surely it is not following Lispov
substitution principle.

here ABC is a fatty interface since intABC does not require char
version of get/set members
and charABC does not require int version of get/set members.

If I remove Get/Set members from ABC class and put int versions in
intABC and char versions in charABC then I have to use downcasting to
call specific versions.

so is there a good design to remove fatty interface from the ABC and at
the same time i should not use downcasting and another constraint is I
should use ABC polymorphically?

With Regards,
Sri.

Jul 13 '06 #1
4 1543
sr*****************@yahoo.com wrote:
class ABC
{
public :
virtual void operation1();
virtual void operation2();
virtual int GetValue();
virtual char GetValue();
virtual void SetValue(int);
virtual void SetValue(char);
Don't forget the virtual destructor.
}

class intABC : public ABC
{
public :
virtual void operation1();
virtual void operation2();
void SetValue(int);
int GetValue();
private:
int val;
}

class charABC : public ABC
{
public :
virtual void operation1();
virtual void operation2();
void SetValue(char);
char GetValue();
private :
char val;
}

class Controler
{
public :
//constructors and destructors
void somefunc() //this function handles container member
private :
vector<ABC*val;
}

client Controler class manipulates ABC derived classes polymorphically

I feel, In the above design surely it is not following Lispov
substitution principle.

here ABC is a fatty interface since intABC does not require char
version of get/set members
and charABC does not require int version of get/set members.

If I remove Get/Set members from ABC class and put int versions in
intABC and char versions in charABC then I have to use downcasting to
call specific versions.

so is there a good design to remove fatty interface from the ABC and at
the same time i should not use downcasting and another constraint is I
should use ABC polymorphically?
This is bad. You could use templates to make it better without doubling
the code to maintain.

template<typename T>
class ABC
{
public :
typedef T Type;

virtual void operation1() = 0;
virtual void operation2() = 0;
virtual Type GetValue() const = 0;
virtual void SetValue(Type) = 0;
};

class intABC : public ABC<int>
{
public :
virtual void operation1();
virtual void operation2();
virtual void SetValue(Type);
virtual Type GetValue() const;
private:
Type val;
};

Which could be used polymorphically like this:

template<typename T>
void Foo( const ABC<T>& abc )
{
abc.operation1();
cout << abc.GetValue() << endl;
}

Cheers! --M

Jul 13 '06 #2
mlimber wrote:
template<typename T>
class ABC
{
public :
typedef T Type;

virtual void operation1() = 0;
virtual void operation2() = 0;
virtual Type GetValue() const = 0;
virtual void SetValue(Type) = 0;
};

class intABC : public ABC<int>
{
public :
virtual void operation1();
virtual void operation2();
virtual void SetValue(Type);
virtual Type GetValue() const;
private:
Type val;
};
But now intABC and charABC don't have the same base type anymore,
right? OP wanted to put pointers to intABC and charABC in one vector,
and I fail to see how the above makes that possible.

Jul 13 '06 #3

jo******@gmail.com wrote:
mlimber wrote:
template<typename T>
class ABC
{
public :
typedef T Type;

virtual void operation1() = 0;
virtual void operation2() = 0;
virtual Type GetValue() const = 0;
virtual void SetValue(Type) = 0;
};

class intABC : public ABC<int>
{
public :
virtual void operation1();
virtual void operation2();
virtual void SetValue(Type);
virtual Type GetValue() const;
private:
Type val;
};
But now intABC and charABC don't have the same base type anymore,
right? OP wanted to put pointers to intABC and charABC in one vector,
and I fail to see how the above makes that possible.
They don't, but static polymorphism can still be employed, as with my
Foo() function. There are other ways to solve the problem, of course,
but that one may work for the OP.

Cheers! --M

Jul 13 '06 #4

sr*****************@yahoo.com wrote:
class ABC
{
public :
virtual void operation1();
virtual void operation2();
virtual int GetValue();
virtual char GetValue();
Can't be done. Can't overload return types of a function. A subclass
is allowed to override iff its new return type is a derivative of the
one it is overriding.
virtual void SetValue(int);
virtual void SetValue(char);
}

class intABC : public ABC
{
public :
virtual void operation1();
virtual void operation2();
void SetValue(int);
int GetValue();
private:
int val;
}

class charABC : public ABC
{
public :
virtual void operation1();
virtual void operation2();
void SetValue(char);
char GetValue();
private :
char val;
}
See what your plan is...don't do it. Not only because it would be
better with templates but because you should never have functions in a
base that are only used by some of its derivatives. A derivative
should use ALL of the interface of its base or it isn't a "base". In
other words, if you have a class named Vehicle and you have a function
"changeOil()" then a bicycle isn't a Vehicle because it has no engine
to put oil into (you can grease it but no oil). But if you have a base
class Vehicle and no "changeOil()" function in it you can have an
"Auto" class that does and then bicycle probably is a vehicle.

Public inheritance is always the IS-A relationship so these things are
important. Read about oop principles (search google for oop principles
- adding object mentor or wiki to the search string turns up good
sources).
>
class Controler
{
public :
//constructors and destructors
void somefunc() //this function handles container member
private :
vector<ABC*val;
}

client Controler class manipulates ABC derived classes polymorphically

I feel, In the above design surely it is not following Lispov
substitution principle.
Definately not because your ABC derivatives are not ABCs.
>
here ABC is a fatty interface since intABC does not require char
version of get/set members
and charABC does not require int version of get/set members.

If I remove Get/Set members from ABC class and put int versions in
intABC and char versions in charABC then I have to use downcasting to
call specific versions.

so is there a good design to remove fatty interface from the ABC and at
the same time i should not use downcasting and another constraint is I
should use ABC polymorphically?
templates. If you really need run-time poly then this actually becomes
somewhat difficult to work out. I have a feeling though that templates
will do it for you.
>
With Regards,
Sri.
Jul 13 '06 #5

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

Similar topics

18
by: George Sakkis | last post by:
I'm looking for a design to a problem I came across, which goes like this (no, it's not homework): 1. There is a (single inheritance) hierarchy of domain classes, say A<-B<-..<-Z (arrows point...
5
by: Andrew Ward | last post by:
Hi All, Sorry if this is off topic, but I could not seem to find a suitable OO Design newsgroup. If there is one feel free to let me know. Here is a simplification of a general design problem I...
8
by: Gert Van den Eynde | last post by:
Hi all, I have a question on interface design: I have a set of objects that are interlinked in the real world: object of class A needs for example for the operator() an object of class B. On...
0
by: JKJ | last post by:
I'm not an OOP guru, but I would say ". . .not necessarily" What if the base class and derived classes are internal? Then the base class has to be within the same assembly to be inherited from. ...
1
by: Josh28 | last post by:
Hi We are a group of two chaps just out of undergrad, we created a software to automate the use of Design Patterns. We have put it up at Source Forge--http://dpatoolkit.sourceforge.net/ The...
18
by: _dee | last post by:
Question about best use of interfaces: Say there's a 'Master' class that needs to implement a few interfaces: class Master : I1, I2, I3 { } The actual code already exists in smaller...
10
by: sunny | last post by:
Does this following program implement the factory design.if not what are things that i have to change in order to make this following program to be designed to factory design pattern. ...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
7
by: snewman18 | last post by:
In learning about design patterns, I've seen discussion about using inheritance when an object's relationship to another object is 'is-a' and composition when the relationship is 'has-a'. Since...
7
by: Immortal Nephi | last post by:
I have an idea how to design an object in a better way. I would like to give you my thought example. Please let me know what you think if it is best object design. Please recommend me any book...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.