Brijesh Gurung
Brijesh Gurung

Reputation: 35

how do you write the derived ("/ ") access modifier into cpp code?

Is there any correct way to write the following UML diagram in to c++ ?

The UML class diagram :

UML class diagram

If so , how to write in c++ from the given diagram ? I was confuse in the "/" part ( " derived access modifier " ).

fyi : i am new to UML diagrams

Upvotes: 1

Views: 188

Answers (2)

Christophe
Christophe

Reputation: 73376

There is no universal mapping from UML to C++, because there are many ways to implement a design intent.

In the case of the derived attribute, there are three frequently used strategies:

  • use a getter that derives (computes) the value when it is needed:

    class Professor {
    public: 
        …
        int getSalary() const {
           // some computation, e.g:
           return salaryScale(yearsOfService)+numberOfClasses*CLASS_BONUS;
        }
        …
     };
    
    
  • use an attribute, and update it with the computation, every-time a parameter changes. This is however only reliable if the computation depends only on private members and is difficult to maintain:

    class Professor {
    private:
        int salary;
        …
    public: 
        …
    
        int getSalary() const {
           return salary; 
        }
        void setYearsOfService(int n)
            yearsOfService=n;
           // update:
           salary =  salaryScale(yearsOfService);
           // better put the calculation in a private function, to not repeat formula multiple times
        }
        …
     };
    
    
  • use a smart getter, that caches the value in a private salary attribute, but computes it only when needed. The idea is to have a „dirty“ flag that is set whenenver the state of the object changes, and if the getter is called and dirty flag is set, recompute the value. Be aware however that this majes it more difficult to maintain. Moreover, if the derivation depends on associated objects, you‘d need to consider the observer pattern. So I‘d advise to use this approach as last resort, in case of heavy computations.

By the way, / doesn’t say anything about visibility (I just assumed public in my example), whereas +, -, # refer to public, private, protected in C++, and UML‘s ~ package visibility has no equivalent in c++.

Upvotes: 1

qwerty_so
qwerty_so

Reputation: 36305

The "/" means the attribute is derived. This in turn means it comes from "higher spheres" which means e.g. from a general class or from some calculation using other attributes. P. 17 of UML 2.5 says

Attributes: each specified by its name, type, and multiplicity, and any additional properties such as {readOnly}. If no multiplicity is listed, it defaults to 1..1. This is followed by a textual description of the purpose and meaning of the attribute. If an attribute is derived, the name will be preceded by a forward slash. Where an attribute is derived, the logic of the derivation is in most cases shown using OCL.

Since the shown diagram does not tell the derivation logic you can't tell anything about it. For the time being implement a simple attribute or leave it from its parent. You will have to get the rules to make it a correct implementation before ending the coding.

Upvotes: 1

Related Questions