473,398 Members | 2,812 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,398 software developers and data experts.

C++ Polymorphism/Abstraction. Multi-Class Abstraction.

110 100+
Hello. This is my first thread here.

My problem has probably been came across by a lot of people, but tutorials and things I've seen don't address it (usually too basic).

My problem is that I would like to use Abstraction for a "plug-in" like interface to classes.

Expand|Select|Wrap|Line Numbers
  1. class ThreadHandle
  2. {
  3. /* stuff here not yet dealing with threads */
  4.  
  5. /** Method which loops inside thread, when implemented */
  6. virtual void update() = 0;
  7.  
  8. virtual void startThread() = 0;
  9. virtual void stopThread() = 0;
  10. }; // End ThreadHandle class
  11.  
  12. class ThreadTypeA : public ThreadHandle
  13. {
  14. /* stuff dealing with threads for linux */
  15.  
  16. protected: 
  17. /** Thread function. */
  18. static void *updateThread(void *input)
  19. { /* "input" refers to this class. 
  20. * Loops input->update() function here */ } 
  21.  
  22. public:
  23. virtual void startThread() { /* stuff specific to linux */ } 
  24. virtual void stopThread() { /* stuff specific to linux */ } 
  25. }; // End ThreadTypeA class
  26.  
  27. class ThreadTypeB : public ThreadHandle
  28. {
  29. /* stuff dealing with threads for windows */
  30.  
  31. public:
  32. virtual void startThread() { /* stuff specific to windows */ } 
  33. virtual void stopThread() { /* stuff specific to windows */ } 
  34. }; // End ThreadTypeB class
  35.  
  36. class ThreadTypeC : public ThreadHandle
  37. {
  38. /* stuff dealing with emulated/fake thread */
  39. static vector <ThreadTypeC *>emuThreads;
  40.  
  41. protected: 
  42. /** Thread function. Runs all update()'s when ran */
  43. static void *updateThread(void *input)
  44. { /* "input" is NULL. Loops through all elements
  45. * in emuThreads, running update() */ } 
  46.  
  47. virtual void startThread() { /* stuff specific to faking */ } 
  48. virtual void stopThread() { /* stuff specific to faking */ } 
  49. }; // End ThreadTypeC class 
  50.  
  51. class Thread : public ThreadTypeA, public ThreadTypeB,
  52. public ThreadTypeC
  53. { /** Stuff. This is probably a useless class. */ }; 
  54.  
  55. class BaseSystem : ThreadHandle
  56. {
  57. public:
  58. /** Implements what update() does */
  59. void update() { /* Important threading stuff */ }
  60. }; // End BaseSystem class
  61.  
  62. int main()
  63. {
  64. BaseSystem testThreadTypeA, testThreadTypeB,
  65. testThreadTypeC;
  66.  
  67. ThreadHandle *handleA = new ThreadHandleA();
  68. ThreadHandle *handleB = new ThreadHandleB();
  69. ThreadHandle *handleC = new ThreadHandleC();
  70.  
  71. // &testThreadTypeA = *handleA; // Error of course, but
  72. // functionality I want.
  73. // &testThreadTypeB = *handleB; // Error of course
  74. // &testThreadTypeC = *handleC; // Error of course
  75.  
  76. return(1); // unsuccess because I'm stuck : P
  77. }; // End main() Function
  78.  
//...ThreadHandle
//..../...|...\........|
//...A...B...C...BaseSystem
//.....................|
//..................Derived, if any

I am wishing to make BaseSystem mimic a linux ThreadTypeA, windows ThreadTypeB, or emulated ThreadTypeC. The threads A-C need to use the virtual method update(), as well as BaseSystem needs to implement it.

Would making threadHandle inherit BaseSystem instead of the other way around take care of my problem?

But I need the end user (me and/or my wife) to have another class inherit BaseSystem, not ThreadHandle and have access to the thread... oh. virtual functions would take care of that. But still have problems

Here's an alternative way I'm still stuck...

//.......BaseSystem
//........./...........\
//....Thread....GraphicsSystem
//.......|
//.Types-of-Threads

Expand|Select|Wrap|Line Numbers
  1. class BaseSystem
  2. {
  3. public: // virtual for anything ThreadHandle and 
  4. // BaseSystem needs access to. 
  5. virtual void startThread() = 0;
  6. virtual void stopThread() = 0;
  7.  
  8. /** Implements what update() does */
  9. virtual void update() = 0;
  10. }; // End BaseSystem class
  11.  
  12. /** Now ThreadHandle could be any name... */
  13. class ThreadHandle : public BaseSystem
  14. {
  15. /* stuff here not yet dealing with threads */
  16.  
  17. }; // End ThreadHandle class
  18.  
  19. class ThreadTypeA : public ThreadHandle
  20. {
  21. /* stuff dealing with threads for linux */
  22.  
  23. protected: 
  24. /** Thread function. */
  25. static void *updateThread(void *input)
  26. { /* "input" refers to this class. 
  27. * Loops input->update() function here */ } 
  28.  
  29. public:
  30. virtual void startThread() { /* stuff specific to linux */ } 
  31. virtual void stopThread() { /* stuff specific to linux */ } 
  32. }; // End ThreadTypeA class
  33.  
  34. class ThreadTypeB : public ThreadHandle
  35. {
  36. /* stuff dealing with threads for windows */
  37.  
  38. public:
  39. virtual void startThread() { /* stuff specific to windows */ } 
  40. virtual void stopThread() { /* stuff specific to windows */ } 
  41. }; // End ThreadTypeB class
  42.  
  43. class ThreadTypeC : public ThreadHandle
  44. {
  45. /* stuff dealing with emulated/fake thread */
  46. static vector <ThreadTypeC *>emuThreads;
  47.  
  48. protected: 
  49. /** Thread function. Runs all update()'s when ran */
  50. static void *updateThread(void *input)
  51. { /* "input" is NULL. Loops through all elements
  52. * in emuThreads, running update() */ } 
  53.  
  54. virtual void startThread() { /* stuff specific to faking */ } 
  55. virtual void stopThread() { /* stuff specific to faking */ } 
  56. }; // End ThreadTypeC class 
  57.  
  58. class GraphicsUpdatingSystem : public BaseSystem
  59. {
  60. int foo;
  61. public:
  62. GraphicsUpdatingSystem() { foo = 5; }
  63.  
  64. virtual void update() { /* finally implement update() */ }
  65. int getFoo() const { return(foo); }
  66. }; // End GraphicsUpdatingSystem class
  67.  
  68. int main()
  69. {
  70. GraphicsUpdatingSystem *gus;
  71.  
  72. // Error because gus inherits BaseSystem, but
  73. // so does ThreadHandleA...
  74. gus = new ThreadHandleA();
  75.  
  76. return(1); // unsuccess because I'm stuck : P
  77. }; // End main() Function
  78.  
//.......BaseSystem
//........./...........\
//....Thread....GraphicsSystem
//.......|
//.Types-of-Threads

Is a template the only way to solve this? That makes the executable so much bigger though. And getting GraphicsSystem to still implement update() is a problem too.

The diagram which I need is:

//...SystemA.....SystemB...SystemC......SystemZ
//.................\..........|.............|....... ........./
//..................\.........|.............|....... ......../
//...................-----BaseSystem--------
//..................................|
//......................-----Threadhandle----
//...................../.........|...........|..............\
//..................../..........|...........|...............\
//.......ThreadA...ThreadB....ThreadC.....ThreadZ

[HTML]
// where SystemA *systemA_ptr = new ThreadC();
// for example. But SystemA-Z is independent
// from one another, and ThreadA-Z is independent
// from one another.
[/HTML]

Thank you for any replies. Even if it's not helpful/relevant to solving this problem--a problem I've had for at least 5 years, but one way or another been able to avoid (mostly with less effective code).

And again, I am sorry for the length of this post. Any help on how I post (being a forum noob) would be appreciated too (like with formatting).


I'm currently running Gentoo Linux while I'm running samba to my wifes computer which is running micro$oft. She's in college while I've been out for a year. Both of us CS majors with some basic CIS classes, too.

My compilers are MinGW and GNU gcc. My IDE's are CodeBlocks, dev-cpp, gleany (linux), and notepad/xemacs.
Apr 15 '08 #1
3 1880
TamusJRoyce
110 100+
[HTML]
http://etutorials.org/Programming/Pr...4+Inheritance/
Some programmers speak of inheritance trees or hierarchies, but with multiple base classes, the organization of inheritance is a directed acyclic graph, not a tree. Thus, C++ programmers sometimes speak of inheritance graphs.
[/HTML]

Does this mean that if you have an indirect class inherited more than once, that the code size doesn't not increase, but rather links back to it? So it isn't a tree anymore, but a graph with no extra duplicated code?

I would love to see a link to an example. But I don't want someone to take up too much of their time on this.
Apr 15 '08 #2
Banfa
9,065 Expert Mod 8TB
However I am sure there are plenty of people here who would be willing to help you.

Please remember to keep your examples down the the minimum, your question is rather longer than average (although at least it is an intelligent one as opposed to some collage kid looking for an answer involving no effort on their part) which may put off a few people. If you follow the "Help" link at the top of the page you will get to our posting guidelines but basically apart from length this post is formatted fine.

I believe the top experts in this forum (C/C++) are mainly from USA so you may have to wait a little for a reply but in the mean time I will give it a stab.


This

Expand|Select|Wrap|Line Numbers
  1. GraphicsUpdatingSystem *gus;
  2.  
  3. // Error because gus inherits BaseSystem, but
  4. // so does ThreadHandleA...
  5. gus = new ThreadHandleA();
  6.  
can't work. You can only assign a pointer to an object to a pointer variable with a type of pointer to that object or pointer to a base class of that object.

Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3. <definition here>
  4. }
  5.  
  6. class A : public Base
  7. {
  8. <definition here>
  9. }
  10.  
  11. class B : public Base
  12. {
  13. <definition here>
  14. }
  15.  
  16. int main()
  17. {
  18. Base *baseA = new A; // OK
  19. A *AA = new A; // OK
  20. Base *baseB = new B; // OK
  21. B *BB = new B; // OK
  22. A *AB = new B; // NOT OK
  23. B *BA = new A; // NOT OK
  24. }
  25.  
For your original code this suggests 1 of 2 things to me (understanding that I don't really know what the software you have written does)
  1. GraphicsUpdatingSystem should actually have a base class of ThreadHandleA, the code you have would still not be quite right though.
  2. The design is wrong, GraphicsUpdatingSystem is either not an object but an instance of ThreadHandleA, or GraphicsUpdatingSystem is an object a part of the system and part of it is 1 (or more) ThreadHandleA object(s) in which case GraphicsUpdatingSystem does not want to have ThreadHandleA as a base class, or vice versa but rather GraphicsUpdatingSystem should contain 1 or more instances or the class ThreadHandleA that may need to be available through a class method (although that would break the class encapsulation)
In English my point is; is the Graphics Updating System a system part of which is a thread or is it a thread that is used to update the graphics. Depending on the answer the class should be designed appropriately.


In the later part of you post I think you are talking about inheriting multiple instances of a base class into a single class. Expanding my example hierarchy with

Expand|Select|Wrap|Line Numbers
  1. class C : public A, public B
  2. {
  3. <definition here>
  4. }
  5.  
Then C inherits A and through A a copy of Base and C also inherits B and through B another copy of Base. So C inherits Base twice. These are not the same class that is there are 2 discrete copies of the class Base with-in the definition of C resulting in
Expand|Select|Wrap|Line Numbers
  1. C c;
  2. Base *pBase = &c; // Compiler error ambiguous conversion to Base *
  3.  
The compiler can not covert to Base * directly because there are 2 copies of Base in the definition of C and it doesn't know which one you want a pointer to. You would ave to resolve it like this for instance
Expand|Select|Wrap|Line Numbers
  1. Base *pBase = static_cast<A *>(&c);
  2.  
which forces the cast up a particular branch of the hierarchy resolving the ambiguity.


Your hour glass shaped hierarchy diagram does not feel right to be. It sounds like you want to be able to have ThreadType(T) on SystemType(S) but in that diagram BaseSystem does not represent a specific type of system but rather all types of system at the same time. For this reason I suspect it is wrong.
Apr 15 '08 #3
TamusJRoyce
110 100+
Wow... I'm sorry for my mistake on byte magazine. But your input is really helpful. I see what you mean about my graph being wrong. And bearing with me though all the code.

I actually didn't expect a reply, considering the size of my post. Usually I can figure or research stuff on my own.

I've had this question since I started college. Though after a year of being out, finally running across it again.

I never thought of using a cast using multi-class inheritance. That solves a lot of my OO problems I've been having.

Is it static_cast or dynamic_cast in this case? Well, it'll give me something to research it on my own. nm. I can kinda see it's static_cast since it's at compiler time that it determines which class I'm wishing to inherit from.

Thank you a lot Banfa... If you ever need me, I'm in your debt.
Apr 15 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: rashkatsa | last post by:
Hi all ! I have written a little module called 'polymorph' that allows to call different methods of a class that have the same name but that have not the same number of arguments. *args are...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
11
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
18
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two...
7
by: doina | last post by:
Hello, Can I have runtime polymorphism using references. I knew that runtime polymorphism could be obtained only by pointers, but now I have tried this in Visual C++ 8.0: #include <iostream>...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.