This forum is in archive mode. You will not be able to post new content.

Author Topic: Can't understand 'has-a' relationship  (Read 5004 times)

0 Members and 2 Guests are viewing this topic.

pllaybuoy

  • Guest
Can't understand 'has-a' relationship
« on: January 15, 2013, 06:34:31 PM »
I got the 'is-a' relationship that it is about inheritance
Code: [Select]
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
Code: [Select]
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 ?
« Last Edit: January 15, 2013, 08:12:28 PM by Kulverstukas »

Offline zWaR

  • Serf
  • *
  • Posts: 32
  • Cookies: 7
    • View Profile
Re: Can't understand 'has-a' relationship
« Reply #1 on: January 15, 2013, 09:31:19 PM »
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:
Code: [Select]
class B: public class A {
}

I suggest reading more on class inheritance: http://www.cplusplus.com/doc/tutorial/inheritance/

Hope this answers your question.
« Last Edit: January 15, 2013, 09:34:29 PM by zWaR »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Can't understand 'has-a' relationship
« Reply #2 on: January 16, 2013, 01:17:28 PM »
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:
Code: [Select]
class B: public class A {
}

I suggest reading more on class 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
Code: [Select]
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
Code: [Select]
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)

Quote
its about declaring and defining a class in another class (yo dawg)

No, that would be a nested class.

Quote
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++.


pllaybuoy

  • Guest
Re: Can't understand 'has-a' relationship
« Reply #3 on: January 16, 2013, 02:01:40 PM »
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 ?
« Last Edit: January 16, 2013, 02:02:17 PM by pllaybuoy »

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Can't understand 'has-a' relationship
« Reply #4 on: January 16, 2013, 09:02:16 PM »
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:

Code: [Select]
class Car {
    public: Motor object_name();
};

(I am not a C++ programer, though)

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: Can't understand 'has-a' relationship
« Reply #5 on: January 16, 2013, 09:14:25 PM »
@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.

Code: (c++) [Select]
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

pllaybuoy

  • Guest
Re: Can't understand 'has-a' relationship
« Reply #6 on: January 16, 2013, 10:08:18 PM »
@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 :|

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.