Composite and inheritance class??

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 40
Reputation: Jason123 is an unknown quantity at this point 
Solved Threads: 0
Jason123 Jason123 is offline Offline
Light Poster

Composite and inheritance class??

 
0
  #1
Nov 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Composite and inheritance class??

 
0
  #2
Nov 25th, 2008
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
  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

  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();
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 40
Reputation: Jason123 is an unknown quantity at this point 
Solved Threads: 0
Jason123 Jason123 is offline Offline
Light Poster

Re: Composite and inheritance class??

 
0
  #3
Nov 25th, 2008
Does it mean that i can only have either 1 composition or 1 inheritance and not both at the same time?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Composite and inheritance class??

 
0
  #4
Nov 25th, 2008
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 40
Reputation: Jason123 is an unknown quantity at this point 
Solved Threads: 0
Jason123 Jason123 is offline Offline
Light Poster

Re: Composite and inheritance class??

 
0
  #5
Nov 25th, 2008
Let's say i have a composition of classes. Does it refer to the same as inheritance class?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Composite and inheritance class??

 
0
  #6
Nov 25th, 2008
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 --

  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 --

  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--

  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. };
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC