Advertisement
JamesDamico

BCS 230 - Constructor

Feb 7th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class Student {
  7.  
  8. private:
  9. int id;
  10. string name;
  11. public:
  12. Student(int i = 0, string n = "none");
  13. Student(int i, string n);
  14.  
  15. int getId();
  16. string getName();
  17.  
  18. void setId(int newId);
  19. void setName(string newName);
  20.  
  21. void printData();
  22. };
  23.  
  24. Student::Student(int i = 0, string n = "none")
  25. {
  26. id = i;
  27. name = n;
  28. }
  29.  
  30. Student::Student(int i, string n)
  31. {
  32. id = i;
  33. name = n;
  34. }
  35.  
  36. int Student::getId()
  37. {
  38. return id;
  39. }
  40.  
  41. void Student::setId(int newId)
  42. {
  43. if (newId < 0)
  44. {
  45. id = 0;
  46. }
  47. else
  48. {
  49. id = newId;
  50. }
  51. }
  52.  
  53. string Student::getName()
  54. {
  55. return name;
  56. }
  57.  
  58.  
  59. void Student::setName(string newName)
  60. {
  61. name = newName;
  62. }
  63.  
  64. void Student::printData()
  65. {
  66. cout << "ID: " << id << endl;
  67. cout << "Name: " << name << endl;
  68. }
  69.  
  70. //////////////////////////
  71. // client code
  72. ////////////////////////
  73. int main() {
  74.  
  75. Student s1;
  76. Student s2(2, "Heather");
  77. Student s3;
  78.  
  79. s1.setName("James");
  80. s1.setId(1);
  81. s1.printData();
  82.  
  83. s2.printData();
  84.  
  85. s3.printData();
  86.  
  87. system("pause");
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement