473,666 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1565
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<typena me 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<typena me T>
void Foo( const ABC<T>& abc )
{
abc.operation1( );
cout << abc.GetValue() << endl;
}

Cheers! --M

Jul 13 '06 #2
mlimber wrote:
template<typena me 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<typena me 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
1964
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 to the parent in the inheritance tree). 2. This hierarchy evolved over time to different versions for each class. So for example, version's 1 hierarchy would be A_v1 <-B_v1 <-..<-Z_v1. 3. At runtime, only one version is selected by a factory...
5
2424
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 face. Say you have: class Car { virtual void speedup();
8
1849
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 what arguments do you decide whether to pass a reference to the object of class B to the member function like this operator()(classB& objB) or to have in class A a data member (a const pointer to a class B object or so) and have this set during...
0
1860
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. You may want to create several classes that are used throughout the assembly, but are not accessible outside the assembly. . . If your base class will be inherited across multiple assemblies and future applications, then I would then
1
1873
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 software has been designed using the .NET framework and coded in C#. The patterns can be stored as plug-ins in XML, adding any number of attributes like Intent, Behavior and the like... Class
18
1974
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 classes that each
10
2338
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. #include<iostream>
6
2133
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 times when I program, I will create objects for a specific purpose for a program and if I need to add to it I just add the code.
7
3062
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 this is all new and I'm still learning, I was hoping someone can give me some pointers on best practices on applying these ideas. If my logic is incorrect on anything, please don't hesitate to tell me where I'm wrong - I'm completely open to any...
7
1715
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 which it teaches me how to design C++ OOP better as long as I know how to program OOP in C++. Think of ancient 6502 microprocessor which it was used for Commodore, Atari, and Apple II. This MPU_6502 is an object of MPU_6502 class. All member
0
8348
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
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8636
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
7376
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...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4186
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
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
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.