Question

Need help in understanding "realization" relationship with classes. Can anyone give me a C++ example on this?

I browsed and I got to know that, a class implementing interface is an example of realization. I didn't get better picture. How do I represent the same using UML?

Thanks

Was it helpful?

Solution

Realization specifies a contract between two or more types. Where one type (here Interface Imammals) defines the contract and the other type (Cat, Dog) promises to carry out.

Below code is a lazy example of Realization...

#include<iostream>
using namespace std;

class IMammals{
public:
    virtual void walk() = 0;
};

class Cats: public IMammals {
public:
    void walk() {
        cout<< "Cat is walking" << endl;
    }
};

class Dogs: public IMammals {
public:
    void walk(){
        cout<< "Dog is walking" << endl;
    }
};

int main(void) {
    Cats aCat;
    Dogs aDog;
    IMammals *ptrMammals = NULL;

    ptrMammals = &aCat;
    ptrMammals->walk();

    ptrMammals = &aDog;
    ptrMammals->walk();

    return 0;
}

Using UML, realization is represented by an dotted arrow that points from the type two(Cat,Dog or Contractor) class to the type one class(IMammals or Contractee). The tip of the arrow is an empty triangle.

                +-----------------+
                |      IMammals   |
                |-----------------|
                |                 |
    +---------|>|                 |<|--------+
    |           +-----------------+          |
    |                                        |
    |                                        |
+-----+-----+                            +-----+-----+
|    Cat    |                            |    Dog    |
|-----------|                            |-----------|
|           |                            |           |
+-----------+                            +-----------+
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top