Mastering Object-Oriented Analysis and Design with UML
Appendix: UML to C++ Mapping
Mastering Object-Oriented Analysis and Design with UML
Appendix: UML to C++ Mapping
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 2
// Notes will be used in the// rest of the presentation// to contain C++ code for// the attached UML elements
class Course{public: Course(); ~Course();};
Course
Mapping Representation: Notes
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 3
class Student{
public: void addSchedule (theSchedule: Schedule, forSemester: Semester); int hasPrerequisites(forCourseOffering: CourseOffering);
protected: int passed(theCourseOffering: CourseOffering);
private: char* name;};
Visibility for Attributes and Operations
Student
- name : char*
+ addSchedule (theSchedule: Schedule, forSemester: Semester)+ hasPrerequisites(forCourseOffering: CourseOffering) : int# passed(theCourseOffering: CourseOffering) : int
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 4
class Student {
public: static int getNextAvailID();
private: static int nextAvailID = 1;};
Student
- nextAvailID : int = 1
+ getNextAvaiIID() : int
Class Scope Attributes and Operations
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 5
<<utility>> MathPack
-randomSeed : long = 0randomSeed : long = 0-pi : double = 3.14159265358979-pi : double = 3.14159265358979
+sin (angle : double) : double+cos (angle : double) : double+random() : double
class MathPack {public: static double sin(double angle); static double cos(double angle); static double random();private: static long randomSeed = 0; static double pi = 3.14159265358979;};
void main(void). . . myCos = MathPack::cos(90.0);. . .
Utility Class
A grouping of “free” attributes and operations
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 6
Outer
Outer::Inner
class Outer{public: class Inner { public: Inner(); ~Inner(); }; Outer(); ~Outer();};
Nested Class
Hide a class that is relevant only for implementation
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 7
Schedule
Student
#include “Schedule.h”
class Student{public: Student(); ~Student();private: Schedule* theSchedule;};
class Student;
class Schedule{public: Schedule(); ~ Schedule();private Student* theStudent;};
Associations
Bi-directional associations
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 8
Schedule
Student
#include “Schedule.h”
class Student{public: Student(); ~Student();private: Schedule * theSchedule;};
class Schedule{public: Schedule(); ~ Schedule();};
Association Navigability
Uni-directional associations
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 9
Professor
CourseOffering
instructor
#include "Company.h"
class Professor {public: Professor(); ~ Professor ();private: CourseOffering* theCourseOffering;};
class Professor;
class CourseOffering {public: CourseOffering(); ~ CourseOffering ();private: Professor* instructor;};
Association Roles
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 10
CourseOffering
Schedule
#include “CourseOffering.h”
class Schedule{public: Schedule(); ~ Schedule(); private: CourseOffering* primaryCourses[4];};
class CourseOffering{public: CourseOffering(); ~ CourseOffering();};
0..4 primaryCourses
Association Multiplicity
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 11
Schedule
class CourseOffering;
class PrimaryScheduleOfferingInfo {public: PrimaryScheduleOfferingInfo(); ~ PrimaryScheduleOfferingInfo (); const CourseOffering * get_the_CourseOffering() const; void set_the_CourseOffering(CourseOffering *const toValue);private: const char get_Grade() const; void set_Grade(const char toValue); char value = ‘I’; CourseOffering *theCourseOffering;};
Association Class
CourseOffering
0..40..*
primaryCourses
PrimaryScheduleOfferingInfo
- grade: char = I
alternateCourses
0..20..*
0..*
Schedule CourseOffering
11 0..4
0..20..*
alternateCourses
primaryCourseOfferingInfo PrimaryScheduleOfferingInfo
- grade: char = I
Design Decisions
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 12
Course
prerequisites
0..*
class Course {public: Course(); ~Course();private: UnboundedSetByReference<Course> prerequisites;};
Reflexive Associations
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 13
Schedule
Student
class Schedule{public: Schedule(); ~ Schedule();};
0..*
1
#include “Schedule.h”
class Student{public: Student(); ~Student();private UnboundedSetByReference<Schedule> the_Schedule;};
Aggregation
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 14
Schedule
Student
class Schedule{public: Schedule(); ~ Schedule();};
0..*
1
#include “Schedule.h”
class Student{public: Student(); ~Student();private UnboundedSetByValue<Schedule> the_Schedule;};
Composition
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 15
GroundVehicle
class Truck : public GroundVehicle{public: float tonnage; void getTax();};
class GroundVehicle{public: int licenseNumber; void register();};
+licenseNumber: int
+register()
Truck
+tonnage: float
+getTax()
Generalization
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 16
Amphibious Vehicle
Vehicle
WaterVehicle
LandVehicle
{overlapping}
#include “LandVehicle.h”#include “WaterVehicle.h”
class AmphibiousVehicle : public LandVehicle, private WaterVehicle{ . . .};
<<private>>
<<virtual>> <<virtual>>
#include “Vehicle.h”
class WaterVehicle : virtual public Vehicle{ . . .};
Multiple Inheritance
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 17
class Animal {public: virtual void talk() = 0;};
Animal{abstract}
Lion
+talk()
Tiger
+talk()
#include "Animal.h"
class Tiger : public Animal{public: Tiger(); ~Tiger(); void talk();};
+talk() {abstract}
Abstract Class
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 18
#include “Set.h”
typedef Set<float, 100> mySetOfFloats;
template <class T, int n>class Set {public: void insert (T anElement); void remove (T anElement);};
SetT, n:int
<<bind>>
mySetOfFloats
insert(T)remove(T)
<float, 100>
Parameterized Class
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 19
Interfaces and Realizes Relationships
Schedule<<entity>>
Serializable<<Interface>>
Mastering Object Oriented Analysis and Design with UMLCopyright © 2003 Rational Software, all rights reserved 20
CourseCatalog<<subsystem>>
ICourseCatalog
getCourseOfferings() : CourseOfferingList
Subsystems