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 --
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 --
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--
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();
}
}
};