943,871 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1846
  • C++ RSS
Nov 25th, 2008
0

Composite and inheritance class??

Expand Post »
1) Could someone explain the difference between composite and inheritance class

2) Is there a composite class with inheritance?
Last edited by Jason123; Nov 25th, 2008 at 3:28 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
Jason123 is offline Offline
42 posts
since Sep 2004
Nov 25th, 2008
0

Re: Composite and inheritance class??

I am assuming that you have googled this and are confused, so I will take a stab at explaining it but feel free to ask questions.

Composition is to contain. An example is easiest
c++ Syntax (Toggle Plain Text)
  1. class Wheelbarrow
  2. {
  3. private:
  4. Cement A;
  5. Water B;
  6. Sand C;
  7.  
  8. };
The class contains stuff. In this case material, but it can be anything.
The point is it contains objects/number or other things that the class can use.

Inheritance is to be the same type as. An example

c++ Syntax (Toggle Plain Text)
  1. class Car
  2. {. };
  3.  
  4. class OffroadCar : public Car
  5. { };

Anything a general car can do, is also done by OffroadCar BUT some things (normally functions) are specialized and some things can be added. e.g.

Car & Offroad car both might need double showSpeed() but Offroad car might have a bool isStuckinMud();
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Nov 25th, 2008
0

Re: Composite and inheritance class??

Does it mean that i can only have either 1 composition or 1 inheritance and not both at the same time?
Reputation Points: 10
Solved Threads: 0
Light Poster
Jason123 is offline Offline
42 posts
since Sep 2004
Nov 25th, 2008
0

Re: Composite and inheritance class??

No you can have a bit of both. In C++ you can have multiple inheritance
e.g.
class A : public B,public C and class A could contain any number of things... e.g.
C++ Syntax (Toggle Plain Text)
  1. class A : public B,public C
  2. {
  3. D Item;
  4. E array[5];
  5. F* ArrPtr;
  6. };

In the last example class A inherits from B and C but contains a single instance of D, 5 instances of E and a pointer to an F or some group of F
(exactly which is to be determined by later code).

The only problem you will get, is that the phrase "composite class" is often used to imply that the main function of the class is as a container. It does not preclude inheritance or complex internal functions but in discussions the intent of the phrase is that the class contains and manages stuff.
It is sometimes used in a verb form e.g. "class X is a composite of Y and Z", meaning that X contains one or many instances of Y and Z.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008
Nov 25th, 2008
0

Re: Composite and inheritance class??

Let's say i have a composition of classes. Does it refer to the same as inheritance class?
Reputation Points: 10
Solved Threads: 0
Light Poster
Jason123 is offline Offline
42 posts
since Sep 2004
Nov 25th, 2008
0

Re: Composite and inheritance class??

1) My Answer: Composition ( I believe you meant to say Composition ? ) is a form of delegation in which instances of a class use, but may not have the same interface of a target object.

The major benefit of Composition? There are many. Let's say you need to use a trait of a particular class but you don't want your class to be an extension of the class (there are many reasons one wouldn't want inheritance - constructor overhead, different 'base class' paths, to have methods defined for the objects main purpose instead of methods inherited from an existing class, etc), then you can use Composition to accomplish this feat --

c++ Syntax (Toggle Plain Text)
  1.  
  2. class Task{};
  3.  
  4. class Program{
  5.  
  6. Task myTask; // Program HAS-A Task instance (Composition)
  7.  
  8. //...
  9.  
  10. };

Now let's say you need existing methods from another class and you need to build upon that class or redefine the way the methods work. In addition, you want your class to be treated similarly as the class you are extending from. This would be a form of Inheritance --

c++ Syntax (Toggle Plain Text)
  1.  
  2. class Task2{};
  3.  
  4. /**
  5.  * Program2 inherits from Task2
  6.  */
  7. class Program2 : public Task2{
  8.  
  9.  
  10.  
  11. };



2) My Answer: Yes you can use both Composition and Inheritance for the same object. This is something one would call a "Proxy Class" because your class has the same information as the object that the class is using. You can use Proxy classes as a means of protecting the target class that is encapsulated and only accessing information from it when conditions are appropriate.

Note: Inlined definition(s) for convenience--

c++ Syntax (Toggle Plain Text)
  1.  
  2. class Notepad{
  3.  
  4. public:
  5. Notepad(){}
  6. virtual void writeData(){ cout << "Writing..." << endl;}
  7. virtual void deleteData(){cout << "Deleting..." << endl;}
  8. };
  9.  
  10. class Proxypad : public Notepad{
  11.  
  12. Notepad np;
  13. char mask;
  14.  
  15. public:
  16. Proxypad() : mask(0) {}
  17. void setState(char x){mask |= x;}
  18. void deleteState(char x){ mask &= ~x;}
  19. virtual void writeData(){
  20. if(mask & 0){
  21. cout << "Proxy Object is in sleeping state" << endl;
  22. }else{
  23. np.writeData();
  24. }
  25. }
  26. virtual void deleteData(){
  27. if(mask & 0){
  28. cout << "Proxy Object is in sleeping state" << endl;
  29. }else{
  30. np.deleteData();
  31. }
  32. }
  33. };
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Overloading ++ --
Next Thread in C++ Forum Timeline: introducing bit erasure in packets..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC