EvilZone
Programming and Scripting => C - C++ => : pllaybuoy January 15, 2013, 06:34:31 PM
-
I got the 'is-a' relationship that it is about inheritance
class classA {
//inset code here
};
class classB:public class A {
//members
};
But I am a bit confused about the HAS A relationship , in simple words what is it and how does that relate with inheritance ?
like
class classA {
public: classB object_name();
};
This is all the information I could find yet and I am confused rather its about declaring and defining a class in another class (yo dawg) or is it about creating an instance of a class in another class , either way what would be the scope of that object and can it be accessed by main ?
-
Class B contains members declared in Class A. Or in other words: class B inherits members from class A. That is defined with the Class B declaration:
class B: public class A {
}
I suggest reading more on class inheritance: http://www.cplusplus.com/doc/tutorial/inheritance/
Hope this answers your question.
-
Class B contains members declared in Class A. Or in other words: class B inherits members from class A. That is defined with the Class B declaration:
class B: public class A {
}
I suggest reading more on class inheritance: http://www.cplusplus.com/doc/tutorial/inheritance/ (http://www.cplusplus.com/doc/tutorial/inheritance/)
Hope this answers your question.
That doesn't have to do with the question. You explain the is-a-relationship.
I got the 'is-a' relationship that it is about inheritance
class classA {
//inset code here
};
class classB:public class A {
//members
};
But I am a bit confused about the HAS A relationship , in simple words what is it and how does that relate with inheritance ?
like
class classA {
public: classB object_name();
};
This is all the information I could find yet and I am confused rather its about declaring and defining a class in another class (yo dawg) or is it about creating an instance of a class in another class , either way what would be the scope of that object and can it be accessed by main ?
This is not a C or C++ specific question. The name tells you you a lot. HAS-A means that in your example class A has an instance of class B (as a field).
HAS-A doesn't relate with inheritance.
It is like saying:
A dog has a tail. (HAS-A)
A dog is an animal. (IS-A)
its about declaring and defining a class in another class (yo dawg)
No, that would be a nested class.
what would be the scope of that object and can it be accessed by main ?
The scope is an entirely different question as it depends on the access modifier (in your example it is "public", but you can use as well another one). I suggest you just try it out or google for the access modifiers and scope in C++.
-
That doesn't have to do with the question. You explain the is-a-relationship.
This is not a C or C++ specific question. The name tells you you a lot. HAS-A means that in your example class A has an instance of class B (as a field).
HAS-A doesn't relate with inheritance.
It is like saying:
A dog has a tail. (HAS-A)
A dog is an animal. (IS-A)
No, that would be a nested class.
The scope is an entirely different question as it depends on the access modifier (in your example it is "public", but you can use as well another one). I suggest you just try it out or google for the access modifiers and scope in C++.
So summing this all up ,
suppose a class motor has a method of RUN and you derive a class CAR from class MOTOR , in this case CAR 'has-a' function run() which it inherits from class MOTOR , which relation is this ?
can you please provide some valid examples of has-a relation ?
-
So summing this all up ,
suppose a class motor has a method of RUN and you derive a class CAR from class MOTOR , in this case CAR 'has-a' function run() which it inherits from class MOTOR , which relation is this ?
can you please provide some valid examples of has-a relation ?
Why would you ever derive a Car from a Motor? That would be is-a (because you use inheritance) and it doesn't make sense in this case. A car has a motor, not a car is a motor.
Your Car should have an instance of Motor. That is a valid has-a. The snippet you gave for this, is correct, just change A for Car and B for Motor:
class Car {
public: Motor object_name();
};
(I am not a C++ programer, though)
-
@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.
-
@xires & deque :Very well explained , besides I would not appreciate a 'has-a' relation with a shoe :-\
Thanks guys and guess I was mixing things up between 'has-a' relation and nested classes .
@deque : well because a derived class can contain additional methods than its base class and in a motor it would be okay for the methods(just suppose) like motorspeed and others to be private and let the CAR class have a method start() calling a method ignition() of class motor and ignition() dealing with private members reserved to the base class or maybe ..maybe I should stop justifying :|