| | |
Composite and inheritance class??
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
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
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
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
Composition is to contain. An example is easiest
c++ Syntax (Toggle Plain Text)
class Wheelbarrow { private: Cement A; Water B; Sand C; };
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)
class Car {. }; class OffroadCar : public Car { };
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(); •
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
No you can have a bit of both. In C++ you can have multiple inheritance
e.g.
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.
e.g.
class A : public B,public C and class A could contain any number of things... e.g. C++ Syntax (Toggle Plain Text)
class A : public B,public C { D Item; E array[5]; F* ArrPtr; };
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.
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 --
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 --
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--
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)
class Task{}; class Program{ Task myTask; // Program HAS-A Task instance (Composition) //... };
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)
class Task2{}; /** * Program2 inherits from Task2 */ class Program2 : public Task2{ };
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)
class Notepad{ public: Notepad(){} virtual void writeData(){ cout << "Writing..." << endl;} virtual void deleteData(){cout << "Deleting..." << endl;} }; class Proxypad : public Notepad{ Notepad np; char mask; public: Proxypad() : mask(0) {} void setState(char x){mask |= x;} void deleteState(char x){ mask &= ~x;} virtual void writeData(){ if(mask & 0){ cout << "Proxy Object is in sleeping state" << endl; }else{ np.writeData(); } } virtual void deleteData(){ if(mask & 0){ cout << "Proxy Object is in sleeping state" << endl; }else{ np.deleteData(); } } };
![]() |
Other Threads in the C++ Forum
- Previous Thread: Overloading ++ --
- Next Thread: introducing bit erasure in packets..
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





