+ 2
C++ Composition?
I'm stuck trying to understand composition in c++. I don't want to move on until I understand it. Can somebody give me a good explanation or link me a tutorial for this? It'd be greatly appreciated.
5 Answers
+ 4
To the best of my memory, it's about having an object of class A as a member of class B.
If class B inherits from class A, we say that class B is-a class A. E.g. A fish is an animal. The fish has traits of an animal.
If class B contains an object of class A (composition), we say that class B has-a class A. E.g. A kid has a ball.
class Ball {
int radius, diameter;
// etc
};
class Kid {
Ball myBall;
// etc
};
+ 3
Thanks Izaak GOLDSTEIN
+ 1
Maybe little help you
+ 1
I know it as function composition. If you have two functions `f(x)` and `g(x)`, then a function returning `f(g(x))` is called the composition of `f` and `g`.