Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. / Aaron Smith - Code Review Example
  2. #include <iostream>
  3. #include <string
  4. #pragma warning( disable : 4244)
  5. class TestResults
  6. {
  7. public:
  8. static TestResults* CreateTestResultsBasedOnAttitude(int const attitude)
  9. {
  10. TestResults newGrades;
  11. switch (attitude)
  12. {
  13. case 0:
  14. newGrades.m_TotalPercent = 98.0f;
  15. break;
  16. case 1:
  17. newGrades.m_TotalPercent = 5.0f;
  18. break;
  19. case 2:
  20. newGrades.m_TotalPercent = 45.0f;
  21. case 3:
  22. newGrades.m_TotalPercent = 70.0f;
  23. break;
  24. default:
  25. newGrades.m_TotalPercent = -1;
  26. break;
  27. }
  28. return &newGrades;
  29. }
  30. uint16_t getTotalPercent() { return m_TotalPercent; }
  31. uint16_t m_TotalPercent;
  32. };
  33. class Person
  34. {
  35. public:
  36. class Person(char* name, int age)
  37. {
  38. m_Age = age;
  39. }
  40.  
  41. ~Person() { }
  42.  
  43. char* GetName() {
  44. return m_Name;
  45. }
  46.  
  47. void SetName(char* name)
  48. {
  49. int SIZEOFNAME = strlen(name);
  50. memcpy(m_Name, name, SIZEOFNAME + 1);
  51. }
  52.  
  53. void SetAge(int const age) const
  54. {
  55. m_Age = age;
  56. }
  57. private:
  58. static int const MAXCHARS = 11;
  59. char m_Name[MAXCHARS];
  60. int m_Age;
  61. };
  62.  
  63. class Student : public Person
  64. {
  65. public:
  66. Student() {}
  67. Student(char* name, int age)
  68. : Person(name,age)
  69. {
  70. }
  71. ~Student() { }
  72.  
  73. float GetTestResults() const
  74. {
  75. return m_TestResults->getTotalPercent();
  76. }
  77.  
  78. void SetTestResults(TestResults* TestResults)
  79. {
  80. m_TestResults = TestResults;
  81. }
  82. private:
  83. TestResults* m_TestResults;;
  84. };
  85.  
  86. int main()
  87. {
  88. //create a m_Person called aaron smith who is 28 and take a test while happy
  89. Student* m_Person1 = new Student("Aaron Smith", 28);
  90. m_Person1->SetTestResults(TestResults::CreateTestResultsBasedOnAttitude(5));
  91. std::cout << m_Person1->GetName() << " is " << m_Person1->m_Age << " and Scored " << m_Person1->GetTestResults() <<std::endl;
  92. delete m_Person1;
  93.  
  94. //now lets reuse m_Person and change the name,age and take the test while angry
  95. Student* m_Person2 = new Student;
  96. m_Person2->SetName("Captain America The Third");
  97. m_Person2->SetAge(800);
  98. m_Person2->SetTestResults(TestResults::CreateTestResultsBasedOnAttitude(2));
  99. std::cout << m_Person2->GetName() << " is " << m_Person2->m_Age << " and Scored " << m_Person1->GetTestResults();
  100. return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement