项目作者: saif86

项目描述 :
Example of converting a UML Class diagram to C++ class
高级语言: C++
项目地址: git://github.com/saif86/Student-case-study---UML-Class-Diagram-to-CPP-Code.git


Student case study

Example of converting a UML Class diagram to C++ class

UML Class Diagram

class

where

  • vis = visibility (+ for public, - for private)
  • attribute = data member (aka field)
  • operation = method (or constructor)

Note:

  • The arg list is a list of parameter types (e.g., int, double, String); parameter names are not included in the UML class diagram
  • Methods that don’t return a value (i.e. void methods) should give a return type of void
  • Class (i.e. static) methods and fields are indicated by underlining
  • Constant (i.e. final) fields are indicated via naming convention: constants should be in ALL_CAPS

Example: Student UML Class diagram

student

Corresponding C++ Class

  1. class Student {
  2. char *name;
  3. const int ROLL_NO;
  4. float gpa;
  5. static int noOfStudents;
  6. public:
  7. Student(char *, int, float);
  8. Student(const Student &);
  9. ~Student();
  10. void setName(char *);
  11. void setGpa(float);
  12. int getROLL_NO() const;
  13. char * getName() const;
  14. float getGpa() const;
  15. static int getTotalStudent();
  16. };
  17. /*private static member cannot be accessed outside the class except for initialization*/
  18. int Student::noOfStudents = 0;
  19. /* Overloaded constructor with default values assigned using member initializer list*/
  20. Student::Student(char *aName = "", int aRollNo = 0, float aGPA = 0.0) : name(aName), ROLL_NO(aRollNo), gpa(aGPA) {
  21. noOfStudents++;
  22. }
  23. /* Copy constructor with deep copy for pointer members*/
  24. Student::Student(const Student &obj) : ROLL_NO(obj.ROLL_NO), gpa(obj.gpa) {
  25. int len = strlen(obj.name);
  26. name = new char[len + 1];
  27. strcpy_s(name, len + 1, obj.name);
  28. noOfStudents++;
  29. }
  30. /* Destructor*/
  31. Student::~Student() {
  32. noOfStudents--;
  33. }
  34. void Student::setName(char * aName) {
  35. strcpy_s(name, strlen(aName), aName);
  36. }
  37. void Student::setGpa(float aGPA) {
  38. if (aGPA < 0.0) {
  39. gpa = 0.0;
  40. }
  41. else
  42. {
  43. gpa = aGPA;
  44. }
  45. }
  46. int Student::getROLL_NO()const {
  47. return ROLL_NO;
  48. }
  49. char * Student::getName() const {
  50. return name;
  51. }
  52. float Student::getGpa() const {
  53. return gpa;
  54. }
  55. int Student::getTotalStudent() {
  56. return noOfStudents;
  57. }