@Deque; excellent description.
@pllaybuoy; I don't quite understand why you would derive 'car' from 'motor' but technically doing so would indeed give you the methods contained therein. However, that still is not quite a 'has-a' relationship. Deriving a class is always an 'is-a' relationship though, technically, anything that the base class 'has' the derived class would then also 'has'(hehe, lolcatish).
This truly is not a difficult concept to understand; 'has-a' simply means that a class CONTAINS some other object. You, ARE a person...you also HAVE a heart.
class Heart : class Organ { // Heart IS-A Organ
private:
unsigned int _bpm, _systolic, _diastolic, _age;
bool _sex;
enum { missing = -1, dead = 0, poor, below, average, above, good, excellent, athlete } _condition;
public:
Heart(void) : _bpm(70), _systolic(120), _diastolic(70), _age(18), _sex(1), _condition(average) {
// I've already coded too much for a simple example..I should really stop
}
unsigned int Rate(void) const {
return this->_bpm;
}
unsigned int Rate(unsigned int bpm) {
return (this->_bpm = bpm);
}
unsigned int Age(void) const {
return this->_age;
}
/* pretend the rest is completed */
};
class Human {
private:
Heart _heart; // Human HAS-A Heart
/* more crap would go here */
};
The 'has-a' relationship simply means that it contains it. It has one. In the above code "Human" 'has-a' "Heart". "Heart" 'is-a' "Organ" because it is derived from it and thus inherits its properties. "Heart" also 'has-a' integer(several, in fact). Again, truly, this concept should be super simple. If someone buries a shoe in your ass, you would 'has-a' shoe in your ass. You would probably also 'has-a' large medical bill, later on, but that is a more abstract relationship of similar type.