Re: Design problem with inheritance



srinivasarao_moturu@xxxxxxxxx wrote:
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 membersand 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.

I would not use inheritence in this way.

1. When you need to vary types use generics or templates if your
language supports them.

2. Otherwise, if there is an object root, like Java's "Object", you can
cast to Integer and Char from it. I've heard it said "avoid casts" but
that's how Sun did it. C++ libs may or may not have such a root though.

3. Failing that, back in the C days, we had to use "void *" with casts
to whatever type was required, it worked well.

Cheers.

.



Relevant Pages

  • Re: How to redesign big switch case?
    ... > I wrote an editor that reads chars, and does a switch to process ... if char is HOME then go to the beginning of the ... it would be nice to allow users to derive their own editor class ... virtual void onKey ...
    (comp.lang.cpp)
  • Re: Design problem with inheritance
    ... virtual void operation1; ... virtual int GetValue; ... virtual char GetValue; ... class charABC: public ABC ...
    (comp.object)
  • Re: Design problem with inheritance
    ... virtual void operation1; ... virtual int GetValue; ... virtual char GetValue; ... class charABC: public ABC ...
    (comp.object)
  • Design problem with inheritance
    ... virtual void operation1; ... virtual int GetValue; ... virtual char GetValue; ... class intABC: public ABC ...
    (comp.object)
  • Re: OT: C++ help
    ... virtual void f; ... int main{ ... To UNSUBSCRIBE, email to debian-user-REQUEST@xxxxxxxxxxxxxxxx ...
    (Debian-User)