UML to C++ mapping
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   John_Stevens
Posted On:   Tuesday, May 7, 2002 06:29 AM

Hi everyone, I am desperately in need of help and advice. Please can anyone tell me how I would convert my UML class diagram into C++? Programming the classes is easy but I am totally lost as to how I would represent the relationships. How would I program the following relationships in C++? Letters in [] denote classes. 1: [A] 1----------* [B] 1-to-many (How would A,B be programmed?) 2: [A] 1 <>---------* [B] 1-to-many composition Please any help would be very much appreciated. Thanks. John    More>>

Hi everyone,


I am desperately in need of help and advice. Please can anyone tell me how I would convert my UML class diagram into C++? Programming the classes is easy but I am totally lost as to how I would represent the relationships. How would I program the following relationships in C++? Letters in [] denote classes.



1: [A] 1----------* [B]
1-to-many (How would A,B be programmed?)



2: [A] 1 <>---------* [B]
1-to-many composition



Please any help would be very much appreciated. Thanks.



John

   <<Less

Re: UML to C++ mapping

Posted By:   Frederic_Fondement  
Posted On:   Tuesday, May 14, 2002 01:16 AM

I cannot answer in C++, but I can for Java. You should translate yourself in your favorite language, for instance translating HashSet into the STL UnboundedSetByReference...



import java.util.Set;

import java.util.HashSet;

class A {

  /* a B object must not be referenced more than once*/

  private HashSet b = new HashSet();



  public Set getB () {

    /* cloning to avoid access as myA.getB().add(myB)*/

    return (Set)this.b.clone();

  }



  public void addB (B aB) {

    if (this.b.contains(aB))

      return;

    this.b.add(aB);

    aB.setA(this);

  }



  public void removeB (B aB) {

    if (! this.b.contains(aB))

      return;

    this.b.remove(aB);

    if (aB.getA() == this)

      aB.setA(null);

  }

}



class B {

  private A a = null;



  public A getA () {

    return a;

  }



  public void setA (A aA) {

    if (this.a == aA)

      return;

    if (this.a != null)

      this.a.removeB(this);

    this.a = aA;

    aA.addB(this);

  }

}



To make a composition, you should too implement a destructor in the A class that will delete each referenced B. This is a nonsense in Java, so I cannot describe it.
About | Sitemap | Contact